How to display and hide a div with CSS?

后端 未结 4 1324
夕颜
夕颜 2020-12-01 00:48

In my script there are three divs. I want to display div with class=\"ab\" when I hover on first line and display div with class=\"abc\", when hove

相关标签:
4条回答
  • 2020-12-01 01:07

    Html Code :

        <a id="f">Show First content!</a>
        <br/>
        <a id="s">Show Second content!!</a>
        <div class="a">Default Content</div>
        <div class="ab hideDiv">First content</div>
        <div class="abc hideDiv">Second content</div>
    

    Script code:

    $(document).ready(function() {
        $("#f").mouseover(function(){
            $('.a,.abc').addClass('hideDiv');
            $('.ab').removeClass('hideDiv');
        }).mouseout(function() {
            $('.a').removeClass('hideDiv');
            $('.ab,.abc').addClass('hideDiv');
        });
    
        $("#s").mouseover(function(){
            $('.a,.ab').addClass('hideDiv');
            $('.abc').removeClass('hideDiv');
        }).mouseout(function() {
            $('.a').removeClass('hideDiv');
            $('.ab,.abc').addClass('hideDiv');
        });
    });
    

    css code:

    .hideDiv
    {
        display:none;
    }
    
    0 讨论(0)
  • 2020-12-01 01:09

    To hide an element, use:

    display: none;
    visibility: hidden;
    

    To show an element, use:

    display: block;
    visibility: visible;
    

    The difference is:

    Visibility handles the visibility of the tag, the display handles space it occupies on the page.

    If you set the visibility and do not change the display, even if the tags are not seen, it still occupies space.

    0 讨论(0)
  • 2020-12-01 01:20

    you can use any of the following five ways to hide element, depends upon your requirements.

    Opacity

    .hide {
      opacity: 0;
    }
    

    Visibility

    .hide {
       visibility: hidden;
    }
    

    Display

    .hide {
       display: none;
    }
    

    Position

    .hide {
       position: absolute;
       top: -9999px;
       left: -9999px;
    }
    

    clip-path

    .hide {
      clip-path: polygon(0px 0px,0px 0px,0px 0px,0px 0px);
    }
    

    To show use any of the following: opacity: 1; visibility: visible; display: block;

    Source : https://www.sitepoint.com/five-ways-to-hide-elements-in-css/

    0 讨论(0)
  • 2020-12-01 01:29

    You need

    .abc,.ab {
        display: none;
    }
    
    #f:hover ~ .ab {
        display: block;
    }
    
    #s:hover ~ .abc {
        display: block;
    }
    
    #s:hover ~ .a,
    #f:hover ~ .a{
        display: none;
    }
    

    Updated demo at http://jsfiddle.net/gaby/n5fzB/2/


    The problem in your original CSS was that the , in css selectors starts a completely new selector. it is not combined.. so #f:hover ~ .abc,.a means #f:hover ~ .abc and .a. You set that to display:none so it was always set to be hidden for all .a elements.

    0 讨论(0)
提交回复
热议问题