【问题标题】:Fade out of background page with JQuery dropdown menu使用 JQuery 下拉菜单淡出背景页面
【发布时间】:2015-07-06 13:31:03
【问题描述】:

我想创建一个 dropdwon 菜单,就像 Edinburgh Zoo 网站上的菜单一样,它在运行时会在后台淡出页面。

到目前为止。jsfiddle我不知道如何让背景褪色,有什么想法吗?

var stop = true;
var hovered;
var timeout;

$('.nav').hover(
    function(){
        clearTimeout(timeout);
        stop = true;
        hovered = this;
        timeout = setTimeout(function(){
        if($(hovered).hasClass('nav_menu_link_drop')){
            $('.content').css('z-index',0);
            $(hovered).next('.content').css('z-index',5);        
            $(hovered).next('.content').slideDown(350);
            timeout = setTimeout(function(){
                $('.content').not($(hovered).next('.content')).slideUp(350);  
            },200);
        }
        else
            $('.content').slideUp(350);    
        },400);
    },
    function(e){
        stop = false;
        clearTimeout(timeout);
        setTimeout(function(){
            if(!stop)
                $('.content').slideUp(350);
        },500);
    }
);

$('.content').hover(
    function(){
        stop = true;    
    },
    function(){

    }
);

$('#nav_menu').hover(
    function(){

    },
    function(){
        timeout = setTimeout(function(){
            $('.content').slideUp(350);
        },200);
    }
);

【问题讨论】:

    标签: javascript jquery css drop-down-menu fadeout


    【解决方案1】:

    您可以通过添加一个新的element 来实现这一点,它将充当back-drop 效果。

    例如

    将此代码放在body 标记的底部。

    <div class="back-drop"></div>
    

    然后给它赋值css properties。

    .back-drop {
        background: rgba(0,0,0,.5);
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        display: none;
    }
    

    另外,在你的 CSS 中添加 z-index: 9999; 到 #nav_menu。

    #nav_menu {
        position: absolute;
        display: block;
        height: 80px;
        width: 100%;
        background: orange;
        z-index: 9999;
    }
    


    将此代码添加到您的 jquery 代码中。

    $('.nav_menu_link_drop').hover(
            function() {
                $('.back-drop').css('display', 'block');
            },
            function() {
                $('.back-drop').css('display', 'none');
    
            }
    );
    

    另外,将$('.back-drop').css('display', 'block'); 和$('.back-drop').css('display', 'none'); 添加到您的$('.content').hover();。

    $('.content').hover(
            function() {
                $('.back-drop').css('display', 'block');
                stop = true;
            },
            function() {
                $('.back-drop').css('display', 'none');
            }
    );
    

    这是JsFiddle link。


    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-06-08
      • 2013-10-06
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多