【问题标题】:Delaying a jQuery Dropdown Menu延迟 jQuery 下拉菜单
【发布时间】:2011-10-14 00:39:19
【问题描述】:

我构建了一个 jQuery 下拉菜单,作为通常使用的 CSS3 版本的后备。我想让它像在 CSS 中一样延迟下拉菜单,但我不知道该怎么做。

这是脚本:

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});
        }
    );
});

它目前所做的是同时双向扩展,但我希望它先向右扩展,然后向下扩展,然后在收缩时,先向上收缩,然后向左。

像这样:

Hover: 
-->
|
v

Unhover:
^
|
<--

所以基本上,分步进行,而不是同时进行。有人可以告诉我如何修改我的脚本以使其正常工作吗?

另外,如何根据导航中 li 的数量而不是设置的高度使其下拉?

编辑:这是一些 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>

【问题讨论】:

  • 有一些 html 和 css 来配合吗?
  • 您将每个次要动画的选项哈希设置为queue:false。这意味着动画是同步运行的,而不是排队运行的。
  • 将其更改为 queue:true 似乎没有任何作用。

标签: javascript jquery css drop-down-menu


【解决方案1】:

希望我正确理解了您的问题:

$(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});
        }
    );
});

【讨论】:

  • 嗯,我希望它先向右扩展,然后向下扩展。然后当它崩溃时,首先向上,然后离开。您的回答给了我与我的第一个脚本相同的结果,所有内容都同时扩展。
  • @Rev 啊,您还需要在下拉列表中删除 queue:false。如果 queue 设置为 false 它会忽略延迟。我更新我的答案。另请参阅我的测试套件:jsfiddle.net/7tuZk :)
  • 这几乎可以工作,但是有没有办法在折叠时反转动画?
  • @Rev 哈哈终于!很高兴我能帮上忙。也许你想接受答案呢? :))
  • 我之前以为我做了,但我猜出于某种原因没有坚持下去。
猜你喜欢
  • 2012-01-25
  • 2014-12-15
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
相关资源
最近更新 更多