【问题标题】:Affect div by hovering other div通过悬停其他 div 来影响 div
【发布时间】:2015-09-26 05:02:54
【问题描述】:

我试图通过悬停另一个 div 来影响一个 div,并且我已经检查了 许多 这些问题已经在 Stackoverflow 上得到了回答,例如 this onethis one,我成功地做到了一种方式,其中#a 影响#b,但我试图让#b 也影响#a - 所以反过来,似乎没有一种方法奏效......

这是有效的;

.plane1:hover ~ .plane2 {
    opacity: 0.2;
    transform: scale(0.9);
}

但所有这些都不起作用;

#plane2:hover #plane1 { background-color: yellow; }
#plane2:hover + #plane1 { background-color: yellow; }
#plane2:hover > #plane1 { background-color: yellow; }
.plane2:hover #.plane1 { background-color: yellow; }
.plane2:hover + .plane1 { background-color: yellow; }
.plane2:hover > .plane1 { background-color: yellow; }

所以我尝试这样做的任何方式,它似乎都不起作用。 (是的,也尝试了~

编辑;我的 HTML

<div id="plane1" class="plane1 col-md-6 left scale-1 trans-6 opacity-7 hover-opacity"> 
  <img src="img/specs.jpg" class="img-thumbnail center" width="250" height="250" />
  <h4 class="center">That plane</h3>
</div>

<div id="plane2" class="plane2 col-md-6 left scale-1 trans-6 opacity-7 hover-opacity"> 
  <img src="img/specs.jpg" class="img-thumbnail center" width="250" height="250" />
  <h4 class="center">That other plane</h3>
</div>

我在这里要完成的工作的 GIF; http://i.imgur.com/s7atNDr.gif(太大无法上传)

【问题讨论】:

  • 您可以尝试使用嵌套的position: absolute 元素。
  • 您是否只允许使用 css 解决方案?还是 jquery 是一种选择?
  • @indubitablee 我对 jquery 一无所知,所以在这种情况下我会很无助 - 首选 CSS,但如果 jquery 是唯一的方法,那么肯定。跨度>
  • 这个问题是,CSS 是级联的,所以你不能只用 CSS 影响早期的元素。 Flexbox 是一种解决方案,但您会发现 JQuery 是最好的解决方案。
  • 离题:你的标题标签搞砸了。您正在混合使用 H3h4 标签。

标签: html css


【解决方案1】:

您可以使用 jQuery 执行此操作,如下所示。仅使用 css 无法做到这一点,因为选择器只能看到悬停元素之后的元素,在这种情况下,您还需要 before。这就是为什么在你的例子中,#a 可以影响#b#b 不会影响#a - 因为#b#a 之后

$(document).ready(function() {
    $(document).on('mouseenter mouseleave', '.plane1', function() {
    	$('.plane2').toggleClass('change');
    });
    $(document).on('mouseenter mouseleave', '.plane2', function() {
    	$('.plane1').toggleClass('change');
    });
});
.change {
    transform: scale(0.9); 
    opacity: 0.2
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="plane1" class="plane1 col-md-6 left scale-1 trans-6 opacity-7 hover-opacity">
    <img src="img/specs.jpg" class="img-thumbnail center" width="250" height="250"/>
    <h4 class="center">That plane</h4>
</div>
<div id="plane2" class="plane2 col-md-6 left scale-1 trans-6 opacity-7 hover-opacity">
    <img src="img/specs.jpg" class="img-thumbnail center" width="250" height="250"/>
    <h4 class="center">That other plane</h4>
</div>

更新 3 架飞机

$(document).ready(function() {   
    $(document).on('mouseenter', '.plane1, .plane2, .plane3', function() {
        $(this).removeClass('change');
    	if($(this).hasClass('plane1')) {
        	$('.plane3').addClass('change');
            $('.plane2').addClass('change');
        }
        if($(this).hasClass('plane2')) {
        	$('.plane3').addClass('change');
            $('.plane1').addClass('change');
        }
        if($(this).hasClass('plane3')) {
        	$('.plane1').addClass('change');
            $('.plane2').addClass('change');
        }
    });
    
    $(document).on('mouseleave', '.plane1, .plane2, .plane3', function() {
        $('.plane1').removeClass('change');
        $('.plane2').removeClass('change');
        $('.plane3').removeClass('change');
    });
});
.change {
    transform: scale(0.9); 
    opacity: 0.2
}
div {
    display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="plane1" class="plane1 col-md-6 left scale-1 trans-6 opacity-7 hover-opacity">
    <img src="img/specs.jpg" class="img-thumbnail center" width="250" height="250"/>
    <h4 class="center">That plane</h4>
</div>
<div id="plane2" class="plane2 col-md-6 left scale-1 trans-6 opacity-7 hover-opacity">
    <img src="img/specs.jpg" class="img-thumbnail center" width="250" height="250"/>
    <h4 class="center">That other plane</h4>
</div>
<div id="plane3" class="plane3 col-md-6 left scale-1 trans-6 opacity-7 hover-opacity">
    <img src="img/specs.jpg" class="img-thumbnail center" width="250" height="250"/>
    <h4 class="center">That plane 3</h4>
</div>

【讨论】:

  • @indubiablee 我已经添加了第三个 div,一个“plane3”,现在,如果我想让 jQuery 影响两个 div,我该怎么做? :) 也非常感谢您的回答!完美无瑕!
  • @iversen:是的。看看我对 3 架飞机场景的更新
【解决方案2】:

简单地说,there is no previous sibling selector

这是一个使用 jQuery 的解决方案(非常容易使用并且有很棒的文档):

$('.plane').hover(function () { // what happens on hover
    $(this).siblings().css({
        transform: 'scale(0.9)',
        opacity: '0.2'
    });
}, function () { // what happens after hover
    $(this).siblings().css({
        transform: 'scale(1)',
        opacity: '1'
    });
});

Demo

要使用它,只需将它包含在一个 script 标记中,就在您的 HTML 结束标记之前。在它之前添加对jQuery 的引用。

【讨论】:

  • 正如评论中所说,我不知道如何处理这段代码 - 我也只是通过添加“背景颜色黄色”来演示它,我真的想添加 transform: scale(0.9); opacity: 0.2 - 我也不知道在哪里插入这个,以及是否需要任何标签......不过谢谢!
  • jQuery 添加到现有站点非常简单。在您的脑海中包含一个指向 jQuery cdn 的链接。然后像链接样式表一样链接一个 js 文件。在您的 js 文件中包含提供的代码。全部完成。 @iversen
  • @AdamBuchananSmith 我想我有点明白了,但还不完全;在这里,我必须将他的 jQuery 放在 &lt;script&gt; 中,然后我有点迷路了......我之前尝试过,它变得非常大......我正在添加一个 GIF 来展示我想要的东西在这里完成。
  • @Tacticus,你是对的,但是 jQuery 开辟了一个新的可能性世界,在纯 JavaScript 中不容易。我们中的一些人喜欢向新开发者展示这个世界。
【解决方案3】:

很遗憾,您想要完成的事情是纯 CSS 无法实现的。

您需要一个 CSS 选择器来选择前面不存在的同级(尚不存在)。见here

最好的选择是使用 javascript(最好是 jQuery)

【讨论】:

    【解决方案4】:

    :hover 属性仅适用于另一个元素,如果它是被悬停元素的子元素。

    <div class="plane1">
      <div class="plane2">
      </div>
    </div>
    
    .plane1:hover > plane2 {
      somestyle
    }
    

    【讨论】:

      【解决方案5】:

      这里使用了一点 javascript:https://jsfiddle.net/DIRTY_SMITH/6p5eLffv/

      HTML

      <div id="div1" class="normal" onmouseover="change()" onmouseout="changeBack()"></div>
      <div id="div2"></div>
      

      CSS

      #div1, #div2{width: 50%; height: 200px; float: left;}
      #div1{background-color: red;}
      #div2{background-color: blue;}
      #div2.normal{background-color: blue;}
      #div2.active{background-color: green;}
      

      javascript

      function change(){
          document.getElementById("div2").className='active';
          }
      function changeBack(){
          document.getElementById("div2").className='normal';
          }
      

      【讨论】:

        【解决方案6】:

        唯一可行的纯 CSS 解决方案是使用嵌套的、绝对定位的元素:

        <div id="plane1" class="plane1 col-md-6 left scale-1 trans-6 opacity-7 hover-opacity" style="position:absolute">
            <img src="img/specs.jpg" class="img-thumbnail center" width="250" height="250">
            <h4 class="center">That plane</h4>
        
            <div id="plane2" class="plane2 col-md-6 left scale-1 trans-6 opacity-7 hover-opacity" style="position:absolute">
                <img src="img/specs.jpg" class="img-thumbnail center" width="250" height="250">
                <h4 class="center">That other plane</h4>
            </div>
        </div>
        

        和下面的css一起

            #plane1:hover > #plane2 {
                /* You may add your :hover styles here */
            }
        

        但是,我绝不会推荐这样的标记,而是建议使用 JavaScript(jQuery 对这个来说太过分了)。请注意,此标记不允许轻松扩展,并且可能仅适用于少数 div。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-21
          • 2013-08-25
          • 1970-01-01
          • 2016-01-23
          • 2019-10-17
          相关资源
          最近更新 更多