简体   繁体   English

向下滚动时对div的CSS top属性进行动画处理

[英]Animate div's CSS top property when scrolling down

I have a notification that pops up during difference instances on my site, and I'd like that notification to follow the menu that animates when you scroll down. 我有一个通知,该通知会在网站上的差异实例期间弹出,并且希望该通知遵循您向下滚动时显示动画的菜单。

The code I have is as follows: 我的代码如下:

<script type='text/javascript'>

var messageFollow = $('.woocommerce-info').offset().top;

$(window).on( 'scroll', function(){
    if ($(window).scrollTop() >= messageFollow) {
        $('.woocommerce-info').css({top: "150px"});
    } else {
        $('.woocommerce-info').css({top: "74px"});
    }
});

</script>

The notification has a value right now of top: 150px which looks great when you're at the top of the screen but top: 74px looks great when scrolled down. 通知的值现在为top: 150px ,当您位于屏幕top: 74px时,它看起来不错,但top: 74px当向下滚动时,看起来不错。

I also want it to animate , but I have no idea how to implement that as well. 我也希望它动画 ,但是我也不知道如何实现它。

A clip to demonstrate: https://www.dropbox.com/s/p6i95f5gkbyn4nm/MessageNotification.mov?dl=0 演示片段: https : //www.dropbox.com/s/p6i95f5gkbyn4nm/MessageNotification.mov?dl=0

jQuery has a built-in animate function. jQuery具有内置的动画功能。

Just replace css with animate and make sure the value is integer. 只需将css替换为animate并确保该值为整数即可。 You can also give a duration in milliseconds as a second parameter to animate function. 您还可以指定持续时间(以毫秒为单位)作为动画功能的第二个参数。

$('.woocommerce-info').animate({top: 150},500);

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

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