简体   繁体   中英

javascript smooth scrolling down a page

Just wondering if there was any way which you can use javascript in order to have smooth scrolling down a page. I don't mean href's like #section1 ect. I mean actual mouse wheel scrolling. I have looked all over this site and the internet but all I can find is the smooth href scrolling, not smooth mouse wheel scrolling.

Try this: Smooth scrolling demo

You can try something like this:

  1. Get the current top location using self.pageYOffset
  2. Get the position of element till where you want to scroll to: element.offsetTop
  3. Do a for loop to reach there, which will be quite fast or use a timer to do smooth scroll till that position using window.scrollTo

NOTE: Solution is just rough idea, not crossbrowser at all.

This is surely possible.

I assume you don't want to build it from scratch, so here's a jQuery plugin .

What this plugin does is to listen to mousewheel and DOMMouseScroll events, and executes a custom, smooth scroll when they're triggered.

You can easily do it with jquery without any plugin, and you can even decide how much time the scrolling should take.

For instance, if you have an element with id scrollhere in the point you want to reach, and attach a click listener that will start the animation to a button or a link with id scroll :

$("#scroll").click(function() {
    $('html, body').animate({
        scrollTop: $("#scrollHere").offset().top
    }, 2000);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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