简体   繁体   English

JavaScript在滚动位置上动画

[英]Javascript animate on scroll position

I am trying to get a scrolling animation like here (notice the circle figure fading in when you scroll down): http://demo.atticthemes.com/skoty/ 我正在尝试获得一个像这样的滚动动画(注意,向下滚动时,圆圈图形会逐渐消失): http : //demo.atticthemes.com/skoty/

This is what I have sofar, but it keeps hanging somehow: http://jsfiddle.net/v4zjgwL6/ 这是我的沙发,但是它以某种方式一直挂着: http : //jsfiddle.net/v4zjgwL6/

var timer;
var triggerHeight = $("#bar").offset().top;
var headerAvatar = $(".header-avatar-wrapper");

$(window).scroll(function() {
    if(timer) {
        window.clearTimeout(timer);
    }

    timer = window.setTimeout(function() {
        var y = $(window).scrollTop();

        if(y > triggerHeight - 220) {
            headerAvatar.css("visibility", "visible");
            headerAvatar.animate({opacity: 1}, 200);
        } else {
            headerAvatar.animate({opacity: 0}, 200);
            headerAvatar.css("visibility", "hidden");
        }

    }, 10);
});

Looks like you're only handling the cases where you need to change state (shown or hide the element) and not the cases where nothing should change. 看起来您只在处理需要更改状态(显示或隐藏元素)的情况,而不在处理什么都不应更改的情况。 This causes you to continuously re-show (re-animate) the thing, which makes it flicker. 这使您不断重新显示(重新设置动画)该事物,从而使其闪烁。

It's early and I have not yet had coffee, but something like this should fix you up. 时间还早,我还没有喝咖啡,但是这样的事情应该可以解决您的问题。 :) :)

var timer;
var triggerHeight = $("#bar").offset().top;
var headerAvatar = $(".header-avatar-wrapper");
var shown; // NEW

$(window).scroll(function() {
    if(timer) {
        window.clearTimeout(timer);
    }

    timer = window.setTimeout(function() {
        var y = $(window).scrollTop();
        var shouldShow = y > triggerHeight - 220; // CHANGED

        if(!shown && shouldShow) { // CHANGED
            shown = true; // NEW
            headerAvatar.css("visibility", "visible");
            headerAvatar.animate({opacity: 1}, 200);
        } else if (shown && !shouldShow) { // CHANGED
            shown = false; // NEW
            headerAvatar.animate({opacity: 0}, 200);
            headerAvatar.css("visibility", "hidden");
        }

    }, 10); });

Proof: http://jsfiddle.net/bvaughn/oL85oj41/ 证明: http//jsfiddle.net/bvaughn/oL85oj41/

You don't need to use a timer, the way you have implemented it causes performance drops. 您不需要使用计时器,因为它的实现方式会导致性能下降。
I would suggest to use css classes instead: 我建议改用CSS类:

var triggerHeight = $("#bar").offset().top;
var headerAvatar = $(".header-avatar-wrapper");

$(window).scroll(function() {
    var y = $(window).scrollTop();

    if (y > triggerHeight - 220 && !headerAvatar.hasClass("visible")) {
        headerAvatar.addClass("visible");
    } else if(y <= triggerHeight - 220 && headerAvatar.hasClass("visible")) {
        headerAvatar.removeClass("visible");
    }
});

I have also added this class in CSS: 我还在CSS中添加了此类:

.header-avatar-wrapper.visible{
    opacity: 1;
    visibility: visible;
}

JSFiddle demo JSFiddle演示


Or alternatively, use jQuery's .fadeIn() and fadeOut() functions: 或者,使用jQuery的.fadeIn()fadeOut()函数:

var triggerHeight = $("#bar").offset().top;
var headerAvatar = $(".header-avatar-wrapper");

$(window).scroll(function() {
    var y = $(window).scrollTop();

    if (y > triggerHeight - 220 && headerAvatar.css("display") == "none") {
        headerAvatar.fadeIn();
    } else if(y <= triggerHeight - 220 && headerAvatar.css("display") == "block") {
        headerAvatar.fadeOut();
    }
});

In CSS I removed the opacity and visibility properties from .header-avatar-wrapper and added display: none; 在CSS中,我从.header-avatar-wrapper删除了opacityvisibility属性,并添加了display: none; instead. 代替。

JSFiddle demo JSFiddle演示

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

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