Search code examples
javascripthtmlcssonmouseover

How to keep div visible after showing it with "onmouseover" and putting mouse over it


I'm trying to do a menu, which will be expanded when mouse will be over a button (like in attached jsfiddle)

HTML:

<div class="container">
    <div class="row">
        <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12" id="menu">
            <ul>
                <li>
                    <div id="hover" onmouseover="document.getElementById('stuff').style.opacity = 1.0;" onmouseout="setTimeout(function(){document.getElementById('stuff').style.opacity = 0.0;}, 3000);">
                        <a href="">Button</a>
                    </div>
                </li>
            </ul>

        </div>

        <div class="col-lg-9 col-md-9 col-sm-6 col-xs-12" id="main">
            <div id="stuff">stuff</div>
        </div>
    </div>
</div>

CSS:

    #stuff {
    height: 600px;
    width: 100px;
    background-color: red;
    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;
}

https://jsfiddle.net/bfkxmghd/2/

The menu appears like it should and I'm delaying the closing of it for 3 seconds with setTimeOut, but how can I keep it open if I move the mouse to the stuff div? I've tried to use onmouseover="document.getElementById('stuff').style.opacity = 1.0;, but in this case, it appears even without putting mouse over hover div first.


Solution

  • Just need to call clearTimeout on the timeout you save. No need for jQuery.

    var timeout
    
    function showMenu () {
      clearTimeout(timeout)
      document.getElementById('stuff').style.opacity = 1.0;
      timeout = setTimeout(function(){document.getElementById('stuff').style.opacity = 0.0;}, 3000);
    }
    
    function hideMenu () {
      clearTimeout(timeout)
      document.getElementById('stuff').style.opacity = 0.0;
    }
    
    function keepMenu () {
      clearTimeout(timeout)
    }
    
    var hover = document.getElementById('hover')
    hover.onmouseover = showMenu
    
    var stuff = document.getElementById('stuff')
    stuff.onmouseover = keepMenu
    stuff.onmouseout = hideMenu
        #stuff {
            height: 600px;
            width: 100px;
            background-color: red;
            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;
        }
    <div class="container">
            <div class="row">
                <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12" id="menu">
                    <ul>
                        <li>
                            <div id="hover">
                                <a href="">Button</a>
                            </div>
                        </li>
                    </ul>
    
                </div>
    
                <div class="col-lg-9 col-md-9 col-sm-6 col-xs-12" id="main">
                    <div id="stuff">stuff</div>
                </div>
            </div>
        </div>