简体   繁体   English

延迟jQuery下拉菜单

[英]Delaying a jQuery Dropdown Menu

I've built a jQuery dropdown menu as a fallback from the CSS3 version that's normally used. 我已经构建了一个jQuery下拉菜单,作为通常使用的CSS3版本的后备选项。 I'd like to have it delay the dropdown the same way as in the CSS, but I'm not sure how to do it. 我希望它像CSS中一样延迟下拉菜单,但是我不确定该怎么做。

Here's the script: 这是脚本:

var iconWidth = 34; // default width of navigation <li>'s
var slideWidth = 200; // width of the longest slider
var slideTime = 500; // duration of the sliding animation
var dropHeight = 160; // height of tallest dropdown, should be number of <li>'s x 40
var dropTime = 500; // duration of the dropdown animation

$(function() {

    // expanding

    $("#nav li").not("#logo, .parent li").hover(
        function(){
            $(this).animate({width:slideWidth + "px"},{queue:false,duration:slideTime});
        }, function(){
            $(this).animate({width:iconWidth + "px"},{queue:false,duration:slideTime});
        }
    );

    // dropdown

    $("#nav li.parent").hover(
        function(){
            $(this).children("ul").animate({height:dropHeight + "px"},{queue:false,duration:dropTime});
        }, function(){
            $(this).children("ul").animate({height:"0px"},{queue:false,duration:dropTime});
        }
    );
});

What it currently does is expand both ways at the same time, but I would like it to expand first to the right, then down, then when contracting, first contract up, then left. 它当前所做的是同时同时扩展两种方式,但是我希望它先向右扩展,然后向下扩展,然后收缩,然后先收缩,然后向左扩展。

So like this: 像这样:

Hover: 
-->
|
v

Unhover:
^
|
<--

So basically, in steps, not at the same time. 因此,基本上是分步进行,而不是同时进行。 Can someone show me how to modify my script to make this work? 有人可以告诉我如何修改脚本以使其工作吗?

Also, how do I make it drop down based on the number of li's in the navigation, rather than a set height? 另外,如何根据导航中li的数量而不是设置的高度将其下拉?

EDIT: Here's some example HTML: 编辑:这是一些示例HTML:

<ul id="nav">
                <li id="logo">
                    <p>
                        <img src="images/logo.png" />
                    </p>
                </li>
                <li>
                    <p>
                        <a href="#"><img src="images/dashboard.png" /> Go to Dashboard</a>
                    </p>
                </li>
                <li class="parent">
                    <p>
                        <a href="#"><img src="images/nav-item.png" /> Nav-Item</a>
                    </p>
                    <ul>
                        <li>
                            <p>
                                <a href="#">Create a page</a>
                            </p>
                        </li>
                        <li>
                            <p>
                                <a href="#">View All Pages</a>
                            </p>
                        </li>
                    </ul>
                </li>
</ul>

I hope i understood your question correctly: 希望我能正确理解您的问题:

$(function() {
    // expanding

    $("#nav li").not("#logo, .parent li").hover(
        function(){
            $(this).animate({width:slideWidth + "px"},{duration:slideTime});
        }, function(){
            $(this).delay(slideTime).animate({width:iconWidth + "px"},{duration:slideTime});
        }
    );

    // dropdown

    $("#nav li.parent").hover(
        function(){
            $(this).children("ul").delay(slideTime).animate({height:dropHeight + "px"},{duration:dropTime});
        }, function(){
            $(this).children("ul").animate({height:"0px"},{duration:dropTime});
        }
    );
});

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

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