javascript - 将悬停与几个独特的元素一起使用

标签 javascript jquery html css tumblr

<分区>


想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post .

关闭 7 年前

我正在尝试创建一个 Tumblr 页面。这个想法是在左侧列出几个书名,当鼠标悬停在这些书名上时,会在右侧显示该书的信息。

Example

此处,“Example 2”正悬停在蓝色框中,因此其各自的信息显示在右侧的红色框中。如果我从那里将鼠标悬停在“示例 3”上,“示例 2”的信息框将淡出,而“示例 3”的信息框将淡入。我希望我在这里有某种意义。

现在,我知道我可以使用纯 CSS 来实现这一点,但我想这将涉及为列表中的每个标题创建一个自定义 CSS 类。有没有其他方法可以在避免 CSS 舞蹈的同时做到这一点?

最佳答案

Pure CSS, one class for all titles - Codepen

HTML

<div class="menu">
  <a href="#">Example 1</a>
  <div class="show">
    <h1>EXAMPLE 1</h1>
    <hr>
    <p>text here</p>
  </div>
  <a href="#">Example 2</a>
  <div class="show">
    <h1>EXAMPLE 2</h1>
    <hr>
    <p>text here</p>
  </div>
  <a href="#">Example 3</a>
  <div class="show">
    <h1>EXAMPLE 3</h1>
    <hr>
    <p>text here</p>
  </div>
  <a href="#">Example 4</a>
  <div class="show">
    <h1>EXAMPLE 4</h1>
    <hr>
    <p>text here</p>
  </div>
</div>

CSS

body, html {
  margin: 0;
  padding: 0;
  font-family: 'Roboto', sans-serif;
}

.menu {
  width: 120px;
  height: 300px;
  background-color: #2F43B7;
}

.menu a {
  color: #fff;
  text-decoration: none;
  padding: 10px 15px;
  display: block;
}

.menu a:hover + .show { /* Select .show that is immediately after a */
  opacity: 1;
}
.show {
  transition: 500ms;
  opacity: 0;
  background-color: #B72F2F;
  position: absolute;
  top: 0;
  left: 130px;
  width: 400px;
  height: 300px;
  text-align: center;
}

.show h1 {
  font-size: 46px;
  margin-bottom: 0;
}

.show h1,
.show p {
  color: #fff;
}

.show hr {
  width: 90%;
  border: 2px solid #000;
}

jQuery 效果相同:

$(".show").css("opacity", 0);
$("a").hover(
function(){
    $(this).next(".show").stop().fadeTo("slow",1);
},
function(){
    $(this).next(".show").stop().fadeTo("slow",0);
});

关于javascript - 将悬停与几个独特的元素一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31930876/

上一篇:html - PHP消息中的随机空格

下一篇:html - 垂直对齐 div - 未知高度

相关文章:

IE 中的 JavaScript Math.round 错误

javascript - 不滚动时隐藏滚动条(类似 Mac 的行为)

javascript - 为 longtap 创建一个自定义事件,例如 'click'

javascript - 单击同一复选框本身时的复选框添加和删除

c# - 在 ASP.Net 中的运行时向 HTML <Select> 添加行

javascript - 网页源代码不完整: hidden code?

javascript - 如何在选项卡更改时显示不同的 JointJS block

javascript - 在 setTimeout 函数方法中使用具有变化值的变量?

jquery - 无法在 Bootstrap 表中使用 jQuery 设置数据 url 值

jquery - 动态创建的 DOM 元素不增加 div 大小