【问题标题】:CSS hover on divs will show or hide other divsCSS 悬停在 div 上将显示或隐藏其他 div
【发布时间】:2014-12-11 09:22:04
【问题描述】:

我确信这是可能的,但我无法让它发挥作用,我想创建一个横幅,其下方有 4 个幻灯片和 4 个按钮。

第一张幻灯片最初应该显示,直到您将鼠标悬停在“按钮 2”上,它会隐藏幻灯片 1、3 和 4,并显示幻灯片 2 并将该幻灯片保持在那里(鼠标关闭),直到另一个按钮悬停在上面,然后显示相应的幻灯片并隐藏其余部分。

如果可能的话,我想在不需要 jQuery 的情况下尝试在 HTML/CSS 中完成所有这些工作。

我的代码:

<style type="text/css">
.banners {width:100%; max-width:1004px; height:100%; max-height:546px; overflow: hidden;}
#slide1 {width:100%; height:100%; background:yellow; display:block;}
#slide2 {width:100%; height:100%; background:blue; display:none;}
#slide3 {width:100%; height:100%; background:green; display:none;}
#slide4 {width:100%; height:100%; background:red; display:none;}
#bannerbutton1 {width:100%; max-width: 251px; height:100%; max-height: 40px; float:left; background:yellow;}
#bannerbutton2 {width:100%; max-width: 251px; height:100%; max-height: 40px; float:left; background:blue;}
#bannerbutton3 {width:100%; max-width: 251px; height:100%; max-height: 40px; float:left; background:green;}
#bannerbutton4 {width:100%; max-width: 251px; height:100%; max-height: 40px; float:left; background:red;}
</style>


<div class="banners">
    <div id="slide1">
    </div>
    <div id="slide2">
    </div>
    <div id="slide3">
    </div>
    <div id="slide4">
    </div>
</div>

<a id="bannerbutton1" href="">
BANNER 1
</a>
<a id="bannerbutton2" href="">
BANNER 2
</a>
<a id="bannerbutton3" href="">
BANNER 3
</a>
<a id="bannerbutton4" href="">
BANNER 4
</a>

这可能很简单,我可能会遗漏一些东西,但我们将不胜感激。

【问题讨论】:

  • 你好@chobbit 请检查此链接jsfiddle.net/MBLZx
  • 多次询问和回答。在这里更努力地寻找答案。

标签: html css hover hide show


【解决方案1】:

这样的事情必须使用 JavaScript 来完成。只有当应该显示/隐藏的元素是您用鼠标悬停的那些元素的子元素时,才可以使用纯 CSS 悬停和隐藏元素。例如:

<div class="hover-to-hide-sub-div">
  <div class="hide-me"></div>
</div>

在 CSS 中:

div.hover-to-hide-sub-div:hover div.hide-me { display: none; }

如果没有给出这个层次结构,则需要JS。

【讨论】:

    【解决方案2】:

    是的,您可以不使用 JavaScript 或 JQuery,但只需要 hover。如果您想在图像未悬停时停留在图像上,则需要使用 JavaScript 或 JQuery。这是因为当您的鼠标离开按钮时,css 选择器是不活动的。

    不管怎样,这是我给你的代码,纯 css3。它适用于悬停并在您将鼠标悬停在按钮上时显示图像。

    HTML

    <div class="slider"> 
            <input type="radio" id="control1" name="controls" checked="checked"/>
            <label for="control1"></label>
            <input type="radio" id="control2" name="controls"/>
            <label for="control2"></label>
            <input type="radio" id="control3" name="controls"/>
            <label for="control3"></label>
            <input type="radio" id="control4" name="controls"/>
            <label for="control4"></label>
            <input type="radio" id="control5" name="controls"/>
            <label for="control5"></label>
            <div class="sliderinner">
                <ul>
                    <li>
                        <img src="bea.jpg" />
                        <div class="description">
                            <div class="description-text">
                                <h2>Title 1</h2>
                                <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut</p>
                            </div>
                        </div>
                    </li>
                    <li>
                        <img src="bea.jpg" />
                        <div class="description">
                            <div class="description-text">
                                <h2>Title 2</h2>
                                <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut</p>
                            </div>
                        </div>
                    </li>
                    <li>
                        <img src="bea.jpg" />
                        <div class="description">
                            <div class="description-text">
                                <h2>Title 3</h2>
                                <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut</p>
                            </div>
                        </div>
                    </li>
                    <li>
                        <img src="bea.jpg" />
                        <div class="description">
                            <div class="description-text">
                                <h2>Title 4</h2>
                                <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut</p>
                            </div>
                        </div>
                    </li>
                    <li>
                        <img src="bea.jpg" />
                        <div class="description">
                            <div class="description-text">
                                <h2>Title 5</h2>
                                <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut</p>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    

    CSS

    h1 {color:#333; text-shadow:1px 1px #999; font-size:40px; font-family:Archivo Narrow; margin:40px; text-align:center;}
    .slider {
        display: block;
        height: 320px;
        min-width: 260px;
        max-width: 640px;
        margin: auto;
        margin-top: 20px;
        position: relative;
    }
    
    .sliderinner {
        width: 100%;
        height: 100%;
        overflow: hidden;
        position: relative;
    }
    
    .sliderinner>ul {
        list-style: none;
        height: 100%;
        width: 500%;
        overflow: hidden;
        position: relative;
        left: 0px;
        -webkit-transition: left .8s cubic-bezier(0.77, 0, 0.175, 1);
        -moz-transition: left .8s cubic-bezier(0.77, 0, 0.175, 1);
        -o-transition: left .8s cubic-bezier(0.77, 0, 0.175, 1);
        transition: left .8s cubic-bezier(0.77, 0, 0.175, 1);
    }
    
    .sliderinner>ul>li {
        width: 20%;
        height: 320px;
        float: left;
        position: relative;
    }
    
    .sliderinner>ul>li>img {
        margin: auto;
        height: 100%;
    }
    
    .slider input[type=radio] {
        position: absolute;
        left: 50%;
        bottom: 15px;
        z-index: 100;
        visibility: hidden;
    }
    
    .slider label {
        position: absolute;
        left: 50%;
        bottom: -45px;
        z-index: 100;
        width: 12px;
        height: 12px;
        background-color:#ccc;
        -webkit-border-radius: 50%;
        -moz-border-radius: 50%;
        border-radius: 50%;
        cursor: pointer;
        -webkit-box-shadow: 0px 0px 3px rgba(0,0,0,.8);
        -moz-box-shadow: 0px 0px 3px rgba(0,0,0,.8);
        box-shadow: 0px 0px 3px rgba(0,0,0,.8);
        -webkit-transition: background-color .2s;
        -moz-transition: background-color .2s;
        -o-transition: background-color .2s;
        transition: background-color .2s;
    }
    
    .slider input[type=radio]#control1:hover~label[for=control1] { background-color: #333; }
    .slider input[type=radio]#control2:hover~label[for=control2] { background-color: #333; }
    .slider input[type=radio]#control3:hover~label[for=control3] { background-color: #333; }
    .slider input[type=radio]#control4:hover~label[for=control4] { background-color: #333; }
    .slider input[type=radio]#control5:hover~label[for=control5] { background-color: #333; }
    .slider label[for=control1] { margin-left: -36px }
    .slider label[for=control2] { margin-left: -18px }
    .slider label[for=control4] { margin-left: 18px }
    .slider label[for=control5] { margin-left: 36px }
    .slider input[type=radio]#control1:hover~.sliderinner>ul { left: 0 }
    .slider input[type=radio]#control2:hover~.sliderinner>ul { left: -100% }
    .slider input[type=radio]#control3:hover~.sliderinner>ul { left: -200% }
    .slider input[type=radio]#control4:hover~.sliderinner>ul { left: -300% }
    .slider input[type=radio]#control5:hover~.sliderinner>ul { left: -400% }
    
    .description {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        font-family:Archivo Narrow;
        z-index: 1000;
    }
    .description-text {
        background-color: rgba(0,0,0,.8);
        padding:10px;
        top: 0;
        z-index: 4;
        -webkit-transition: opacity .2s;
        -moz-transition: opacity .2s;
        -o-transition: opacity .2s;
        transition: opacity .2s;
        color: #fff;
    }
    

    【讨论】:

      【解决方案3】:

      使用 CSS,您可以使用子选择器和同级选择器隐藏/显示,这是真的。但即使使用同级选择器,:hover 元素也必须位于受影响的元素之前。

      如果您更改了结构,您可以使用:hover 伪类和通用兄弟选择器~ 实现按钮悬停状态的悬停开关。这是一个例子:

      http://jsfiddle.net/xxgLzgax/

      现在使用智能定位,您可能可以根据需要重新定位它,但是当您想要保持最后一个悬停元素的状态时,问题就来了。 CSS 纯粹是为了样式,即使它确实迎合了元素的伪状态(就它们的样式而言),但它不能做这样的事情。 :hover 仅用于悬停样式。

      正如其他海报所说,您需要使用 JS/JQuery 来执行此操作。大概是这样的:

      $( "button" ).on("mouseenter", function() {
             $('class/ID to show').addClass( "hover" ).siblings().removeClass( "hover" );
      });
      

      但是,无论 JS 解决方案如何保持状态相同并使此特定设置正常工作,您都必须使用 JS。此外,如果您确实使用 JS 来执行此操作,我建议使用一个类来切换它,保持 CSS 和 JS 分开。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-22
        • 1970-01-01
        • 2011-02-16
        • 2015-02-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多