Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laggy when applied to multiple elements #617

Open
iamdriz opened this issue Dec 21, 2020 · 0 comments
Open

Laggy when applied to multiple elements #617

iamdriz opened this issue Dec 21, 2020 · 0 comments

Comments

@iamdriz
Copy link

iamdriz commented Dec 21, 2020

I was using Waypoints and Inview to add animations to several elements on a website when they became in view...

function setAnimations() {
    $('.animation').each(function () {
        new Waypoint.Inview({
            element: $(this),
            enter: function () {
                if(!$(this.element).hasClass('animation--completed'))
                    $(this.element).addClass('animation--animated');
            }
        });
        $(this).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
            $(this).removeClass('animation--animated').addClass('animation--completed');
        });
    });
}

However I found severe lag happening during the code adding the Waypoint instance to each element... and was able to actually write a jQuery function that does exactly the same thing I wanted and it runs with no lag at all...

$.fn.visible = function () {
    let $this = $(this),
        $window = $(window),
        top = $window.scrollTop(),
        bottom = top + $window.height(),
        offsetTop = $this.offset().top,
        offsetBottom = offsetTop + $this.height(),
    return ((offsetTop <= bottom) && (offsetBottom >= top));
};

function setAnimations() {
    function animations() {
        $('.animation').each(function (i, el) {
            if ($(el).visible(true)) {
                if (!$(el).hasClass('animation--completed'))
                    $(el).addClass('animation--animated');
            }
            $(el).one('webkitAnimationEnd animationend', function () {
                $(this).removeClass('animation--animated').addClass('animation--completed');
            });
        });
    }
    $(window).scroll(function () {
        animations();
    });
    $(window).resize(function () {
        animations();
    });
    animations();
}

Any ideas why Waypoints creates such substantial lag in comparison to what I used instead? I understand Waypoints adds additional functionality... but the lag was causing delays on the page load of over 3-4 seconds...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant