Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

$(window).scroll in vanilla JavaScript

  1. What's the equivalent of the following in plain JS?

    $(window).scroll(function() { });

  2. I'm also looking to animate scroll, e.g.:

    $('html, body').animate({scrollTop:1750}, 'slow');

Should I be using requestAnimationFrame?

http://paulirish.com/2011/requestanimationframe-for-smart-animating/

Are there any examples that trigger an animation once on click and not continuous renders?

question from:https://stackoverflow.com/questions/14389687/window-scroll-in-vanilla-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Question 1

window.onscroll = function() {
    console.log('scrolling');
};

or if your targeted browsers support addEventListener :

window.addEventListener('scroll', function() {
        console.log('scrolling');
});

Question 2

In my opinion, if you're just scrolling from one section to a another section of your page, and not having some sort of constantly running scrolling movement, you're fine doing this without using requestAnimationFrame.

You can find good implementations of scrolling to a particular part of the window in pure javascript, I suggest checking out their source(or even using them).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...