show/hide a div on hover and hover out

后端 未结 6 631
梦谈多话
梦谈多话 2020-12-08 10:27

I would like to show and hide a div during hover and hover out.

here\'s what I\'ve done lately.

css

相关标签:
6条回答
  • 2020-12-08 10:30

    I found using css opacity is better for a simple show/hide hover, and you can add css3 transitions to make a nice finished hover effect. The transitions will just be dropped by older IE browsers, so it degrades gracefully to.

    JS fiddle Demo

    CSS

    #stuff {
        opacity: 0.0;
        -webkit-transition: all 500ms ease-in-out;
        -moz-transition: all 500ms ease-in-out;
        -ms-transition: all 500ms ease-in-out;
        -o-transition: all 500ms ease-in-out;
        transition: all 500ms ease-in-out;
    }
    #hover {
        width:80px;
        height:20px;
        background-color:green;
        margin-bottom:15px;
    }
    #hover:hover + #stuff {
        opacity: 1.0;
    }
    

    HTML

    <div id="hover">Hover</div>
    <div id="stuff">stuff</div>
    
    0 讨论(0)
  • 2020-12-08 10:31

    You could use jQuery to show the div, and set it at wherever your mouse is:

    html:

    <!DOCTYPE html>
    <html>
    
      <head>
        <link href="style.css" rel="stylesheet" />
        <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
      </head>
    
      <body>
        <div id="trigger">
          <h1>Hover me!</h1>
          <p>Ill show you wonderful things</p>
        </div>
        <div id="secret">
         shhhh
        </div>
        <script src="script.js"></script>
      </body>
    
    </html>
    

    styles:

    #trigger {
      border: 1px solid black;
    }
    #secret {
      display:none;
      top:0;  
      position:absolute;
      background: grey;
      color:white;
      width: 50%;
    }
    

    js:

    $("#trigger").hover(function(e){
        $("#secret").show().css('top', e.pageY + "px").css('left', e.pageX + "px");
      },function(e){
        $("#secret").hide()
    })
    

    You can find the example here Cheers! http://plnkr.co/edit/LAhs8X9F8N3ft7qFvjzy?p=preview

    0 讨论(0)
  • 2020-12-08 10:34

    Why not just use .show()/.hide() instead?

    $("#menu").hover(function(){
        $('.flyout').show();
    },function(){
        $('.flyout').hide();
    });
    
    0 讨论(0)
  • 2020-12-08 10:37

    May be there no need for JS. You can achieve this with css also. Write like this:

    .flyout {
        position: absolute;
        width: 1000px;
        height: 450px;
        background: red;
        overflow: hidden;
        z-index: 10000;
        display: none;
    }
    #menu:hover + .flyout {
        display: block;
    }
    
    0 讨论(0)
  • 2020-12-08 10:39

    Here are different method of doing this. And i found your code is even working fine.

    Your code: http://jsfiddle.net/NKC2j/

    Jquery toggle class demo: http://jsfiddle.net/NKC2j/2/

    Jquery fade toggle: http://jsfiddle.net/NKC2j/3/

    Jquery slide toggle: http://jsfiddle.net/NKC2j/4/

    And you can do this with CSS as answered by Sandeep

    0 讨论(0)
  • 2020-12-08 10:55
    <script type="text/javascript">
        var IdAry=['reports1'];
        window.onload=function() {
         for (var zxc0=0;zxc0<IdAry.length;zxc0++){
          var el=document.getElementById(IdAry[zxc0]);
          if (el){
           el.onmouseover=function() {
             changeText(this,'hide','show')
            }
           el.onmouseout=function() {
             changeText(this,'show','hide');
            }
          }
         }
        }
        function changeText(obj,cl1,cl2) {
           obj.getElementsByTagName('SPAN')[0].className=cl1;
           obj.getElementsByTagName('SPAN')[1].className=cl2;
        }
    </script>
    

    ur html should look like this

    <p id="reports1">
                    <span id="span1">Test Content</span>
                    <span class="hide">
    
                        <br /> <br /> This is the content that appears when u hover on the it
                    </span>
                </p>
    
    0 讨论(0)
提交回复
热议问题