简体   繁体   English

如何在不使用.animate()的情况下平稳地移动div?

[英]How can I move a div smoothly, without using .animate()?

I'm trying to set up a div that moves out of the way on scroll down. 我正在尝试建立一个向下滚动的div。 But only by -150px, so it moves out of the way on scroll, and when the page comes back to the top, the div comes back into full view. 但是只有-150像素,因此它在滚动时不会移动,并且当页面返回顶部时,div会返回完整视图。

I'm doing this by using the css TOP property, and setting it to -150px and then back to 0px. 我这样做是通过使用css TOP属性,并将其设置为-150px,然后又回到0px。 This works fine, but looks very choppy when it's done. 效果很好,但完成后看起来非常不连贯。 Here is the code: 这是代码:

$(document).ready(function () {
    var $mydiv = $('#movablediv');

    $(window).scroll(function () {
        if($(window).width() >= 641){
            if ($(this).scrollTop() > 100) {
                $mydiv.css({
                    'top': '-150px'
                });
            } else {
                $mydiv.css({
                    'top': '0px'
                });
            }                

        } else {
            $mydiv.css({
                'top': '0px'
            });
        }
    });
});

Fiddle here: http://jsfiddle.net/Lh42w94f/ 在这里提琴: http : //jsfiddle.net/Lh42w94f/

I can't use animate, because animate seems to fire on every scroll event, so if you scroll down, then back up, the scroll down animation will fire 100x before, the scroll up function fires and then returns the div back into position. 我不能使用animate,因为在每个滚动事件上似乎都会激活动画,因此,如果您向下滚动然后向上备份,则向下滚动动画将先触发100x,然后向上滚动功能将触发,然后将div返回原位。 I can't come up with a good solution to use animate in this case. 在这种情况下,我无法提出一个好的解决方案来使用动画。

I've also found solutions that use percent calculation to move the div based on scroll, but all of those keep moving the div infinitely (Example: Move div horizontally when scroll vertically ( jquery or CSS3) ). 我还发现了使用百分比计算来基于滚动移动div的解决方案,但是所有这些解决方案都使div无限移动 (示例: 垂直滚动(jquery或CSS3)时,水平移动div )。 But, I only want to move it -150px and then stop. 但是,我只想将其移动-150px然后停止。

Any ideas would be greatly appreciated! 任何想法将不胜感激!

To prevent the .animate to trigger multiple times, you have to put a flag. 为了防止.animate触发多次,您必须放置一个标志。 Just like this : 像这样 :

var $mydiv = $('#movablediv');
var flag = true;
$(window).scroll(function () {
    if($(window).width() >= 641){
        if ($(this).scrollTop() > 100) {
            if(flag === true)
                $mydiv.stop().animate({
                    'top': '-150px'
                });

            flag = false;
        } else if(flag === false){
            $mydiv.stop().animate({
                'top': '0px'
            });
            flag = true;
        }                

    } else {
        $mydiv.css({
            'top': '0px'
        });
    }
});

Fiddle : http://jsfiddle.net/Lh42w94f/2/ 小提琴: http : //jsfiddle.net/Lh42w94f/2/

Alternatively, you could simply use the CSS transition and then change your value with .css() . 或者,您可以简单地使用CSS过渡,然后使用.css()更改值。

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

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