简体   繁体   English

JS下拉菜单动画

[英]dropdown menu animation with JS

There is my menu without animation: https://jsfiddle.net/sj0pycb7/5/ 我的菜单没有动画: https : //jsfiddle.net/sj0pycb7/5/

When I try add a code like that it doesn't work correct. 当我尝试添加类似的代码时,它无法正常工作。

;(function(){
var height = 1;
var intervalID;

var get = document.getElementsByClassName('dropdown');
var set = document.querySelector('.menu .dropdown ul');

get[0].addEventListener('mouseover', function(){
    startAnim();
}, false);
get[0].addEventListener('mouseout', function(){
    stopAnim();
    set.style.display = 'none';
}, false);

function startAnim(){
    intervalID = setInterval(increaseHeight, 10);
    set.style.overflow = 'hidden';
}
function stopAnim(){
    set.removeAttribute('style');
    set.style.display = 'none';
    clearInterval(intervalID);
    height = 1;
}
function increaseHeight(){
    if(height){
        height += 5;
        height++;
        set.style.height = height + 'px';
        set.style.display = 'inline-block';
    }
    if(height>210){
        height = 0;
        clearInterval(intervalID);
        set.removeAttribute('style');
        set.style.display = 'inline-block';
    }
}
})();

Problem is that event 'mouseout' executes(display:none) when I hovering between dropdown menu anchors. 问题是当我在下拉菜单锚之间悬停时,事件'mouseout'执行(display:none)。 Removing margin and borders doesn't help. 删除边距和边框无济于事。

Problem is solved with jQuery: https://jsfiddle.net/sj0pycb7/6/ 用jQuery解决了问题: https : //jsfiddle.net/sj0pycb7/6/

;(function(){
$(document).ready(function() {
    $( '.dropdown' ).hover(
        function(){
            $(this).children('.submenu').slideDown(200);
        },
        function(){
            $(this).children('.submenu').slideUp(200);
        }
    );
});
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM