【问题标题】:CSS hover over two different elements and affect one same elementCSS 悬停在两个不同的元素上并影响一个相同的元素
【发布时间】:2017-07-26 10:38:13
【问题描述】:

有没有办法在悬停两个 div 时以不同的方式影响一个 div? 最好以 CSS 方式了解解决方案,但如果没有 CSS 方式,那么我对 JQuery 或 Javascript 方式持开放态度。

例如,当我将鼠标悬停在 div myData1 上时,我会影响分隔符以具有某种样式。当我将鼠标悬停在 div myData2 上时,我会以任何其他独特的方式影响分隔符。

.myData1{
  width: 50px;
  height: 50px;
  background-color: #444;
}

.myData1:hover + #separator{
  width: 50px;
  heightL 100px;
  background-color: #cba;
}

.myData2{
  width: 50px;
  height: 50px;
  background-color: #888;
}

.myData2:hover + #separator{
  width: 50px;
  height: 100px;
  background-color: #dba;
}

#separator{
  width: 50px;
  height: 100px;
  background-color: #666;
}
<div>
 <div class="myData1">
 </div>
 <div id="separator">
 </div>
 <div class="myData2">
 </div>
</div>

【问题讨论】:

  • 我们可以改变div的顺序吗?如果可以的话,CSS 本身就有解决方案,或者我们必须使用 jQuery。

标签: javascript jquery css hover elements


【解决方案1】:

使用 jQuery,这将是一个解决方案:

$('.myData1').mouseover(function() {
$('#separator').css('background-color', '#cba');
});
$('.myData1').mouseout(function() {
$('#separator').css('background-color', '#666');
});
$('.myData2').mouseover(function() {
$('#separator').css('background-color', '#dba');
});
$('.myData2').mouseout(function() {
$('#separator').css('background-color', '#666');
});
.myData1{
  width: 50px;
  height: 50px;
  background-color: #444;
}

.myData2{
  width: 50px;
  height: 50px;
  background-color: #888;
}

#separator{
  width: 50px;
  height: 100px;
  background-color: #666;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
 <div class="myData1">
 </div>
 <div id="separator">
 </div>
 <div class="myData2">
 </div>
</div>

【讨论】:

  • 看起来是满足我所有需求的唯一正确解决方案。代码响应屏幕变化,解决方案不仅针对严格的元素大小,不改变元素顺序,还可以将解决方案扩展到多个分隔符。
【解决方案2】:

只需使用 css:

.wrapper {
  position: relative;
}

.myData1 {
  width: 50px;
  height: 50px;
  background-color: #444;
}

#separator {
  width: 50px;
  height: 100px;
  background-color: #666;
  position: relative;
  top: -50px;
}

.myData2 {
  width: 50px;
  height: 50px;
  background-color: #888;
  position: relative;
  top: 100px;
}  

.myData1:hover ~ #separator {
  background-color: #cba;
}  

.myData2:hover ~ #separator {
  background-color: #cba;
} 
<div class="wrapper">
  <div class="myData1"></div>
  <div class="myData2"></div>
  <div id="separator"></div>
</div> 

【讨论】:

  • 这可能有利于静态元素大小。不幸的是,我的元素必须始终负责屏幕更改。所以我不能接受使用相对定位作为我正在寻找的解决方案。也许使用具有静态宽度和高度的普通 DIV 来简单地说明我的问题是个坏主意,但是谢谢。
【解决方案3】:

.data{
position : relative;
height:200px;
width:50px;
display:inline-block
}
.myData1{
  width: 50px;
  height: 50px;
  background-color: #444;
  display:inline-block;
  position:absolute;
  top:0px;
}

.myData1:hover + #separator2{
  width: 50px;
  height: 100px;
  background-color: blue;
  display:inline-block;
}

.myData2{
  width: 50px;
  height: 50px;
  background-color: #888;
  display:inline-block;
  position:absolute;
  bottom:0px;
}

.myData2:hover + #separator{
  width: 50px;
  height: 100px;
  background-color: #dba;
}

#separator{
  width: 50px;
  height: 100px;
  background-color: #666;
  display:inline-block;
  position:absolute;
  top:50px;
}
#separator2{
  width: 50px;
  height: 100px;
  background-color: #666;
  display:none;
  z-index:99999;
  position:absolute;
  top:50px;
}
<div class="data">
 <div class="myData1">
 </div>
 <div id="separator2"></div>
 <div class="myData2">
 </div>
 <div id="separator"></div>
  
</div>

试试这个

【讨论】:

  • @ehsan 仅 css ^^
  • 这可能行得通,但遗憾的是,我将在 HTML/CSS 中生成更多分隔符,并且此解决方案就像严格为一个分隔符制作的那样,因为您将数据严格定位到顶部和底部。不过谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-01-21
  • 2013-08-01
  • 1970-01-01
  • 2013-03-29
  • 2021-03-20
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
相关资源
最近更新 更多