繁体   English   中英

jQuery悬停动画高度(切换)

[英]jquery hover animate height (toggle)

我确定这是一个常见问题,并且我已经在此站点上尝试了许多线程来尝试解决我的问题,但是我似乎无法使其正常运行。 基本上,我有一个子菜单,当父菜单悬停在上面时,我需要显示该菜单,但是如果在完成加载之前将鼠标从菜单项上移开,则当再次将鼠标悬停在菜单上时,新的切换高度是错误的。 那有道理吗? 我正在尝试使其运行的网站在这里:

http://annawhitlam.website.2011.360southclients.com/

(关于我们菜单中有孩子)

我的代码是:

$("#menu .content ul li.parent").hover(function(){
  $(this).find("ul").stop().animate({ height: "toggle" }, 150, "easeInSine");
}, function(){
  $(this).find("ul").stop().animate({ height: "toggle" }, 350, "easeInSine");
});

任何帮助,将不胜感激 :)

或者,您可以尝试此操作而不是停止动画。

$("#menu .content ul li.parent").hover(function(){
  if($(this).find("ul:not(:animated)").length)
     $(this).find("ul").animate({ height: "toggle" }, 150, "easeInSine");
  else
     $(this).find("ul").show();
}, function(){
  if($(this).find("ul:not(:animated)").length)
      $(this).find("ul").animate({ height: "toggle" }, 350, "easeInSine");
  else
      $(this).find("ul").hide();
});

您需要将高度设置为特定值,而不是使用toggle 当有人在完成动画之前将鼠标移开/移到对象上时,它将切换的高度保存为动画中期的百分比。

要使其动态,请尝试以下操作:

$(document).ready(function() {

    var hoverObject = "#menu .content ul li.parent";

    var originalHeight = $(hoverObject).height();

    $(hoverObject).hover(function(){

        $(this).find("ul").stop().animate({ height: 0 }, 150, "easeInSine");
    }, function(){

        $(this).find("ul").stop().animate({ height: originalHeight }, 350, "easeInSine");
    });
});

我和你有同样的问题,这就是我解决它的方法:

$("#menu .content ul li.parent").hover(function(){
    $(this).find("ul").slideDown(150, "easeInSine");
}, function(){
    $(this).find("ul").slideUp(350, "easeInSine");
});

暂无
暂无

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

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