【问题标题】:Timeout for mouseover and mouseout of drop down menu?鼠标悬停和鼠标悬停的下拉菜单超时?
【发布时间】:2013-07-16 11:18:30
【问题描述】:

我想制作一个类似http://www.huffingtonpost.com 站点的下拉菜单。我尝试了鼠标悬停和鼠标悬停的自定义超时以喜欢该网站,但我不能。这是我的jQuery:

var navTimers = [];
$('.mcats > ul > li').each(function(){
$(this).hover(function(){
        var $this = $( this );
        var id = jQuery.data( this );
        navTimers[id] = setTimeout( function() {
            $this.addClass('hover');
            navTimers[id] = "";  
        }, 250 );

    }, function(){
        var $this = $( this ); 
        var id = jQuery.data( this );
        if ( navTimers[id] != "" )
        {  
            clearTimeout( navTimers[id] );  
        } 
        else
        {
            navTimers[id] = setTimeout( function() {
                if(!$this.is(':hover'))
                {
                    $this.removeClass('hover');
                    navTimers[id] = "";
                }
            }, 600 );
        }

    });
});

这里是演示http://jsfiddle.net/rebeen/FSKbL/

子菜单显示后,我在它上面连续使用鼠标。 Jquery 不适合我。

有什么寻求帮助的想法吗?非常感谢!

【问题讨论】:

  • 如果您可以创建一个小的工作小提琴,那将很容易提供帮助。另外我认为你的逻辑必须颠倒......
  • 我也同意你的逻辑应该颠倒......
  • 谢谢,我更新了小提琴链接,请给我解决方案。再次感谢。
  • @BeenYoung.. 你认为你发布的小提琴中什么不起作用..
  • 我在大约 3 年前做过类似的事情,但会打开和关闭课程。 jsfiddle.net/4uguq 每次新菜单悬停时,它都会清除当前放置的菜单/计时器,否则将使用超时。

标签: javascript jquery drop-down-menu timeout settimeout


【解决方案1】:

对于此菜单,您只能使用 css 和 html:

http://jsfiddle.net/dimitardanailov/m6jVd/

HTML:

<div id="menuh-container">
<div id="menuh">
    <ul>
        <li><a href="#" class="top_parent">Item 1</a>
        <ul>
            <li><a href="#">Sub 1:1</a></li>
            <li><a href="#" class="parent">Sub 1:2</a>
                <ul>
                <li><a href="#">Sub 1:2:1</a></li>
                <li><a href="#">Sub 1:2:2</a></li>
                <li><a href="#">Sub 1:2:3</a></li>
                <li><a href="#">Sub 1:2:4</a></li>
                </ul>
            </li>
            <li><a href="#">Sub 1:3</a></li>
            <li><a href="#" class="parent">Sub 1:4</a>
                <ul>
                <li><a href="#">Sub 1:4:1</a></li>
                <li><a href="#">Sub 1:4:2</a></li>
                <li><a href="#">Sub 1:4:3</a></li>
                <li><a href="#">Sub 1:4:4</a></li>
                </ul>
            </li>
            <li><a href="#" class="parent">Sub 1:5</a>
                <ul>
                <li><a href="#">Sub 1:5:1</a></li>
                <li><a href="#">Sub 1:5:2</a></li>
                <li><a href="#">Sub 1:5:3</a></li>
                <li><a href="#">Sub 1:5:</a></li>
                <li><a href="#">Sub 1:5:5</a></li>
                </ul>
            </li>
        </ul>
        </li>
    </ul>

    <ul>    
        <li><a href="#" >Item 2</a></li>
    </ul>
</div>  <!-- end the menuh-container div -->  
</div>  <!-- end the menuh div --> 

CSS:

#menuh-container
    {
    position: absolute;     
    top: 1em;
    left: 1em;
    }

#menuh
    {
    font-size: small;
    font-family: arial, helvetica, sans-serif;
    width:100%;
    float:left;
    margin:2em;
    margin-top: 1em;
    }

#menuh a
    {
    text-align: center;
    display:block;
    border: 1px solid #0040FF;
    white-space:nowrap;
    margin:0;
    padding: 0.3em;
    }

#menuh a:link, #menuh a:visited, #menuh a:active    /* menu at rest */
    {
    color: white;
    background-color: #0040FF;      /* royal blue */
    text-decoration:none;
    }

#menuh a:hover                      /* menu on mouse-over  */
    {
    color: white;
    background-color: #668CFF;  /* cornflowerblue */
    text-decoration:none;
    }   

#menuh a.top_parent, #menuh a.top_parent:hover  /* attaches down-arrow to all top-parents */
    {
    background-image: url(http://sperling.com/examples/menuh/nav_white.gif);
    background-position: right center;
    background-repeat: no-repeat;
    }

#menuh a.parent, #menuh a.parent:hover  /* attaches side-arrow to all parents */
    {
    background-image: url(http://sperling.com/examples/menuh/nav_white.gif);
    background-position: right center;
    background-repeat: no-repeat;
    }

#menuh ul
    {
    list-style:none;
    margin:0;
    padding:0;
    float:left;
    width:9em;  /* width of all menu boxes */
    /* NOTE: For adjustable menu boxes you can comment out the above width rule.
    However, you will have to add padding in the "#menh a" rule so that the menu boxes
    will have space on either side of the text -- try it */
    }

#menuh li
    {
    position:relative;
    min-height: 1px;        /* Sophie Dennis contribution for IE7 */
    vertical-align: bottom;     /* Sophie Dennis contribution for IE7 */
    }

#menuh ul ul
    {
    position:absolute;
    z-index:500;
    top:auto;
    display:none;
    padding: 1em;
    margin:-1em 0 0 -1em;
    }

#menuh ul ul ul
    {
    top:0;
    left:100%;
    }

div#menuh li:hover
    {
    cursor:pointer;
    z-index:100;
    }

div#menuh li:hover ul ul,
div#menuh li li:hover ul ul,
div#menuh li li li:hover ul ul,
div#menuh li li li li:hover ul ul
{display:none;}

div#menuh li:hover ul,
div#menuh li li:hover ul,
div#menuh li li li:hover ul,
div#menuh li li li li:hover ul
{display:block;}

/* End CSS Drop Down Menu */

Article

【讨论】:

  • 感谢您的回答,但是当用户在菜单上悬停鼠标悬停时,我需要设置超时。
【解决方案2】:

为什么不用hoverIntent.js插件

【讨论】:

  • 感谢 Jon Fabian,但在您的解决方案中找不到我的需求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-02
  • 1970-01-01
  • 2013-03-14
  • 2011-02-06
  • 2022-08-22
相关资源
最近更新 更多