简体   繁体   English

jquery .animate()-这种代码有点我想要的

[英]jquery .animate() - this code sort of does what i want

I am trying to make a div slide down when the mouse moves over another div just above it. 当鼠标移至其上方的另一个div时,我正在尝试使div向下滑动。 Basically the div above it is just the trigger that makes the div slide down. 基本上,上方的div只是使div向下滑动的触发器。 Mouseover of .trigger makes .slidedown expand, and mouseout of .slidedown makes itself slide back up. .trigger的鼠标悬停使.slidedown展开,而.slidedown的悬停鼠标使其自身向上滑动。 Here's the code i have so far: 这是我到目前为止的代码:

$(document).ready(function() {
 $('.slidedown').hide(); 
 //When mouse rolls over
  $('.trigger').mouseover(function(){ 
   $('.slidedown').stop().animate({
   height: ['toggle', 'swing'],
  }, 600, function() {
    // Animation complete.
  });
});

    //When mouse is removed
   $('.slidedown').mouseout(function(){  
     $('.slidedown').stop().animate({
   height:'0px'
   }, 600, function() {
    // Animation complete.
  });
});
});

This works, but there are just two teaks i need help with. 这行得通,但是我需要帮助的只有两个柚木。 Firstly, after mouseout and the .slidedown div slides up and disappears, if i then mouse over the .trigger div again, nothing happens. 首先,将鼠标移出并且.slidedown div向上滑动并消失后,如果我再将鼠标移到.trigger div上,则什么也没有发生。 It should make the .slidedown move down again. 它应该使.slidedown再次向下移动。 I need it to every time keep working. 每次继续工作都需要它。 I tried removing the .stop() but it still doesn't work. 我尝试删除.stop(),但仍然无法正常工作。

Also can i make it also slide back up if the mouse moves out of .trigger but only if it isn't moving out of .trigger into .slidedown? 如果鼠标移出.trigger但仅当它没有移出.trigger进入.slidedown时,我还可以使其也向上滑动吗? This is so incase the user doesn't move the mouse into .slidedown, it would remain forever which isn't good. 如此一来,如果用户没有将鼠标移到.slidedown,它将永远保留下去,那是不好的。 Or just have a time limit that it can remain expanded if the mouse doesn't move over .slidedown. 或者只是有一个时间限制,如果鼠标不移到.slidedown上,它可以保持展开。

Second, is there a way to make a delay of around 1 second between mouseout and the div sliding back up? 第二,是否有办法在mouseout和div向上滑动之间延迟大约1秒? Thanks for your help! 谢谢你的帮助!

You might try using the jQuery hover event. 您可以尝试使用jQuery hover事件。 For the delay, you can put the closing animation in setTimeout : 对于延迟,可以将结束动画放在setTimeout

$(document).ready( function(){
  $('.trigger').hover( function(){ // enter animation

    $('.slidedown').stop(true,true).animate({
        height: ['toggle', 'swing'],
        }, 600, function() { /* animation done */ });

    }, function(){ // leave animation

    setTimeout( function(){
      $('.slidedown').stop(true,true).animate({
        height: '0px',
        }, 600, function() { /* animation done */ });
    }, 1000 );

  });
});

You might also look into the hoverIntent plug-in for more nuanced control over the mouseenter/mouseleave behavior, including timing. 您还可以查看hoverIntent插件,以更精细地控制mouseenter / mouseleave行为,包括定时。

I think you'll find that setting a numerical height in $('.trigger').mouseover() may help the animation be repeatable. 我想您会发现,在$('.trigger').mouseover()中设置数字高度可能有助于动画的可重复性。 FYI, you can set an integer number for something like height or width in jQuery and it will automatically set the unit to px for you. 仅供参考,您可以为jQuery中的高度或宽度等设置整数,它将自动为您设置单位为px。

As Ken pointed out, setTimeout is useful for a delay in code, but keep it in your $('.slidedown').mouseout() event or the slideown div will hide after you mouseout of the trigger div instead of when you leave the slidedown div as you specified. 正如Ken所指出的那样,setTimeout对于延迟代码很有用,但请将其保留在$('.slidedown').mouseout()事件中,否则将slideown trigger div后, slideown div将隐藏,而不是在离开时指定的slidedown

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

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