【问题标题】:How do you affect multiple child divs when hovering on the parent将鼠标悬停在父级上时如何影响多个子级 div
【发布时间】:2014-05-23 11:41:27
【问题描述】:

我正在尝试在我的网站上实现一些 CSS,但我遇到了一些困难。我目前有五个高度交错的“球”(只是圆形 div)。我一直在尝试让球在你悬停在球上时四处移动,这很棒,但目前它们只会继续移动,只要你在移动时保持指针对准球。

理想情况下,当您将鼠标悬停在一般区域时,我希望所有五个独立移动。我已将它们封装在一个包装 div 中,但我不确定当您将鼠标悬停在父 div 上时影响子 div 的代码。我也不确定我是否正确使用了父母和孩子这两个术语,因为我在过去 20 分钟内才遇到这个概念!

这里是 HTML:

<div id='demoStrip'><div id='ballWrapper'>
    <div id='bounce'>
        <div class='ball' id='ball1'><p>Professional</p></div>
        <div class='ball' id='ball2'><p>Copy</p></div>
        <div class='ball' id='ball3'><p>Just</p></div>
        <div class='ball' id='ball4'><p>For</p></div>
        <div class='ball' id='ball5'><p>You</p></div>
    </div>
</div></div> <!-- End of demoStrip div -->  

这是当前的 CSS:

#demoStrip {
width: 960px;
height: 410px;
margin: 20px auto 0 auto;
/*background: #00cccc;*/
border-radius: 20px;

}

#ballWrapper {
width: 900px;
height: 410px;
margin: 0 auto 0 auto;
}

.ball {
margin: 0 20px 0 20px;
/*width: 150px;
height: 150px;*/
border-radius: 200px;
background-image: radial-gradient(circle closest-corner at center, #FFFF99 0%, #FFFF00 100%);
float: left;
box-shadow: 5px 5px 5px #333333;

}

.ball p {
text-align: center;
text-transform: uppercase;
font-family: sans-serif;
margin: 0;
}


#ball1 {
width: 150px;
height: 150px;
margin-top: 245px;  
}

#ball2 {
width: 140px;
height: 140px;
margin-top: 185px;
}

#ball3 {
width: 130px;
height: 130px;
margin-top: 125px;
}

#ball4 {
width: 120px;
height: 120px;
margin-top: 65px;
}

#ball5 {
width: 110px;
height: 110px;
margin-top: 5px;
}

#ball1:hover {
margin-top: 5px;
transition: margin-top 3s;  
}

#bounce:hover ~ #ball2:hover {
margin-top: 65px;
transition: margin-top 3s;
}

最后一段代码是当我将鼠标悬停在'bounce' div 上时尝试让ball2 'bounce'。目前它不起作用,但我确信在语法上它都是错误的。任何建议将不胜感激!

【问题讨论】:

    标签: css


    【解决方案1】:

    编写您的 CSS,使其影响悬停 div 内的球:

    #bounce:hover .ball {
        ...
    }
    

    或者,如果您需要为每个球使用不同的 CSS:

    #bounce:hover #ball1 {
        ...
    }
    #bounce:hover #ball2 {
        ...
    }
    ...
    

    【讨论】:

      【解决方案2】:

      为悬停设置 100% 的宽度/高度

      #ball1:hover {
      width:100%;
      height:100%
      margin-top: 5px;
      transition: margin-top 3s;  
      }
      
      #bounce:hover ~ #ball2:hover {
      margin-top: 65px;
      transition: margin-top 3s;
      width:100%;
      height:100%
      

      }

      【讨论】:

      • 就是这样 - 我没有给我的反弹 div 任何尺寸,所以没有什么可以悬停!谢谢。
      【解决方案3】:

      理想情况下,我希望你五个人都独立移动 悬停在一般区域...

      所以做这样的事情:

      #demoStrip:hover #bounce .ball:nth-child(1) {
        //animation here
      }
      

      这应该可以解决您的第二个问题...

      目前,只要您保持指针不动,它们就会继续移动 在它移动时对其进行训练

      如果您仍然遇到该问题,请将它们都设置为 position: relative 并设置每个元素的 z-index,但请务必将 .ball 设置为更高的 z-index。有关原因的更多信息,请参阅Submenu does not stay open。

      【讨论】:

        【解决方案4】:

        你的最后一次尝试并不算太远,但不是

        #bounce:hover ~ #ball2:hover {...}
        

        使用

        #bounce:hover #ball2 {...}
        

        这会

        当鼠标悬停在'bounce' div上时让ball2'bounce'

        您不需要 #ball2 上的第二个 :hover,也不需要波浪号 (~)。 在 CSS 中,波浪号 (~) 符号是“通用兄弟”组合符。有关 CSS 选择器/组合器的精彩介绍,请参阅本文http://code.tutsplus.com/tutorials/the-30-css-selectors-you-must-memorize--net-16048

        您可以使用波浪号来悬停#ball1 并对#ball2 进行动画处理(假设#ball2 在DOM 中的#ball1 之后),如下所示:

        #ball1:hover ~ #ball2 {
            margin-top: 65px;
            transition: margin-top 3s;
        }
        

        【讨论】:

        • 太好了,谢谢你。父母/孩子看起来对我来说是一件有用的事情。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-24
        • 2013-11-14
        • 1970-01-01
        相关资源
        最近更新 更多