【问题标题】:Change 'Click' function to mouseover/mouseout将“单击”功能更改为鼠标悬停/鼠标悬停
【发布时间】:2011-07-05 23:18:57
【问题描述】:

我正在使用以下滑动 div 脚本:

http://www.webdesignerwall.com/demo/jquery/simple-slide-panel.html

目前,当点击 .btn-slide 按钮时,slidetoggle 功能被激活。这会向上滑动“面板”div。

第二次单击 .btn-slide 按钮后,面板 div 将关闭。

我是 js 的新手,因此我们将不胜感激。这是我想要做的:

1) 当鼠标移到(而不是单击).btn-slide 类时,我希望面板滑出。 2) 然后,当鼠标移出 .btn-slide 或 #panel 时,我希望面板关闭。 (但如果鼠标在其中一个上,面板应该保持打开状态)。

我能够让它工作到滑动切换功能会关闭其中一个或另一个,但不能同时关闭的位置。

提前感谢您的帮助。

此致,

苹果机


这里是 JS:

<script type="text/javascript"> 
jQuery(function($){
$(document).ready(function(){
    $('.btn-slide').click(function() {
        $("#panel").slideToggle("slow");
        $(this).toggleClass("active"); return false;

    });


});

 });

</script> 

here is the HTML currently being used:

<div id="prod_nav_tab">
<div id="panel"> 
    This is where stuff goes!
</div> 
<p class="slide"><a class="btn-slide">Table of Contents</a></p> 

</div>

我已经玩过CSS以适应我的特定网站,如下(原始js,html,css可以从上面的链接获得)。

div#prod_nav_tab {
width: 200px;
height: 31px;
overflow: hidden;
background-color:#F00;
float: left;
margin: 0px 0px 0px 75px;
}



a:focus {
    outline: none;
}
#panel {
    background-color:#F00;
    width: 200px;
    height: 200px; 
    display: none;
    position: absolute;
    bottom: 0px;
}
.slide {
    margin: 0;
    padding: 0;
    /* border-top: solid 4px #422410;  **Adds a line at top of slide button to distibuish it */
    background: url(images/btn-slide.gif) no-repeat center top;
}
.btn-slide {
    background: #d8d8d8;
    text-align: center;
    width: 200px;
    height: 31px;
    padding: 0px 0px 0 0;
    margin: 0 0 0 0;
    display: block;
    font: bold 12pt Arial, Helvetica, sans-serif;
    color: #666;
    text-decoration: none;
    position: relative;
cursor: pointer;
/*  background: url(images/white-arrow.gif) no-repeat right -50px;  ** Controls Arrow up/down */

}
.active {
    background-position: right 12px;
}

【问题讨论】:

    标签: javascript


    【解决方案1】:

    当您从 .btn-slide 移到 #panel 时,它现在会隐藏它,因为它会触发 .btn-slide 的 mouseleave 事件。

    为防止这种情况,您应该执行以下操作:

    HTML:

    <div id="trigger">
        <a href="" class="btn-slide">Slide down</a>
        <div id="panel">
            Content of your panel
        </div>
    </div>
    

    jQuery:

    jQuery(function($) { 
        $(document).ready(function() { 
            $("#trigger").mouseenter(function() {
                $("#panel").slideDown("slow"); 
                $(this).addClass("active"); 
            }).mouseleave(function() {
                $("#panel").slideUp("slow"); 
                $(this).removeClass("active"); 
            });
        }); 
    });
    

    确保在您的 CSS 中将面板设置为从一开始就隐藏...

    div#panel {
         display: none;
    }
    

    【讨论】:

    • 你好朱尔斯。感谢您的回复和您粘贴的代码。
    • 您好 Jules,感谢您发布的代码。我试过了,我遇到了同样的问题。当我向任何方向(甚至进入实际面板 div)移开 .btn-slide 按钮时,面板幻灯片仍会滑出视图。问题似乎是我没有将“面板” div 指定为整个元素的一部分,一旦通过鼠标进入 btn 幻灯片打开,应该包括 .btn 按钮和面板 div。再次感谢您的帮助——我想回到绘图板。 -mac
    • 哦,抱歉,我没有正确阅读。让我编辑答案。
    • 朱尔斯非常感谢!新的代码起作用了……我没有意识到我可以为 mouseenter/mouseleave 函数定义一个完整的父 div!我真诚地感谢你在这方面的帮助......两个小时,我无法让它工作 - 和你在一起 5 分钟,它很摇滚!谢谢。
    • 欢迎您。 :) 不要忘记通过单击答案左侧的大 V 来勾选我的答案是正确的。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 2018-09-03
    • 2019-02-16
    相关资源
    最近更新 更多