【问题标题】:jQuery Slide-panel with Mouse-out and Off-click带有鼠标移出和单击的 jQuery 滑动面板
【发布时间】:2012-08-03 17:54:49
【问题描述】:

我正在尝试制作一个简单的 jQuery 面板滑块,并将它基本上放在我想要的地方,减去两个功能。您可以在以下位置查看它的版本:http://jsfiddle.net/7WArj/

当前面板在内容上向下滑动并在单击关闭按钮时关闭。按钮发生了变化,因为我希望最终成为按钮的图像在打开时指向上方,在关闭时指向下方。

我很想添加一个功能,当您不再将鼠标悬停在面板上时,它会超时并在 x 秒后滑回。

我还想添加一个功能,如果您单击滑动面板上的任意位置,它也会触发面板向上滑动。

我用.toggle().bind 尝试了其中的一些,但无法使其正常工作。菜单会随机上下滑动等。

如果还有更好的方法来编写我的代码,我将不胜感激。

【问题讨论】:

    标签: jquery jquery-animate toggle panel


    【解决方案1】:

    我很确定这就是你想要的,代码用我所做的注释:

    演示:http://jsfiddle.net/SO_AMK/jhYWk/

    jQuery:

    jQuery(document).ready(function() {
        jQuery("div#toppanel").attr('tabindex', -1); //Let the DIV have focus
    
        jQuery("div#toppanel").blur(function(){
            jQuery("div#panel").animate({height: "0px"}, "fast"); // On blur, hide it
        });
    
        timerRunning = false;
        jQuery("div#panel").hover(function(){ //on mouse over clear a timeout if it exists
            if (timerRunning) {
                clearTimeout(hoverTimeout);
                timerRunning = false;
            }
        },
        function(){ //on mouse out set the timeout
            hoverTimeout = setTimeout(function(){
                jQuery("div#panel").animate({height: "0px"}, "fast");
            }, 2000);
            timerRunning = true;
        });
    
        jQuery('#toppanel').click(function(event){
             event.stopPropagation();
         });
    
        jQuery("div.panel_button").click(function(){
            jQuery("div#panel").focus().animate({ height: "250px" }, "fast");
            jQuery("div.panel_button").toggle()
        }); 
    
        jQuery("div#hide_button").click(function(){
            jQuery("div#panel").animate({height: "0px"}, "fast"); 
        });
    });
    

    CSS:

    #toppanel {
        outline: 0; /* Remove yellow outline in Chrome */
        z-index: 25;
        text-align: center;
        position: absolute;
        top: 0px;
        left: 0px;
    }
    #panel {
        position: relative;
        height: 0px;
        margin-left: auto;
        margin-right: auto;
        z-index: 10;
        overflow: hidden;
        text-align: left;
        height: 0px;
        display: block;
        background-color: #efefef;
        border: 1px solid #000;
        width: 150px;
    }
    #panel_contents {
        height: 100%;
        width: 603px;
        position: absolute;
        z-index: -1;
    }
    .the_content {
        margin-top:40px;
    }
    

    ​ HTML:相同

    【讨论】:

    • 看起来正是我想要的。谢谢!
    • 似乎按钮向下/向上状态仍然存在一些问题,并且菜单弹出的速度非常快,具体取决于您执行的操作和执行时间
    • 没问题,但是,到目前为止,我在代码中发现了两个错误,所以你应该检查一下。附:你不需要在 jQuery 定义的函数中使用jQuery,你可以像往常一样使用$,即使你有另一个使用它的库。
    • 哦,哎呀。我认为,当您不小心双击时会发生这种情况。现在修复,焦点功能是从内容移动到整个滑块。
    • 如果我回答了您的问题,请点击我答案左侧的复选标记,谢谢。
    【解决方案2】:

    它工作得很好,但我发现了一个问题,如果你用“Button Down”打开它而不是 mouseout 并等待它关闭 => 很好。

    此时的问题是,如果您再次使用“Button Down”打开它,它会立即关闭。

    如果可以的话,我建议在 mouseleave 时关闭这样的解决方案

    $( "#toppanel" ).mouseleave(
    function( event ){
        var container = $( "#panel" );
        if (container.is( ":visible" )){
        $('#panel').delay(1800).slideUp(300);}
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多