Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - onscroll moves smoothly to next anchor? How-To?

I'm building a single-page website with a few sections that each fill out the user's window; I've got that working.

What I want to do is:

When the user scrolls up or down he/she would ease-up or down (accordingly) to the section before or after. My sections each have a anchor at the top of them.

Here is an example of what I'm trying to achieve. Each article fills the page. Once you scroll you jump to the next article.

http://www.made-my-day.com/

like image 903
Leopold Kristjansson Avatar asked Dec 05 '12 13:12

Leopold Kristjansson


People also ask

How do you navigate to another page with a smooth scroll on a specific ID?

So, if you want to scroll smoothly to that part instead, you first need to reset the scroll position to 0 and then add smooth scrolling. // direct browser to top right away if (window. location. hash) scroll(0,0); // takes care of some browsers issue setTimeout(function(){scroll(0,0);},1);

How do I scroll using an anchor?

The easiest way to to make the browser to scroll the page to a given anchor is to add *{scroll-behavior: smooth;} in your style. css file and in your HTML navigation use #NameOfTheSection .

How do you scroll up in Javascript?

Method 1: Using window.scrollTo() The scrollTo() method of the window Interface can be used to scroll to a specified location on the page. It accepts 2 parameters the x and y coordinate of the page to scroll to. Passing both the parameters as 0 will scroll the page to the topmost and leftmost point.


1 Answers

I think you could get the job done using this plugin: https://github.com/alvarotrigo/fullPage.js.

Also, it seems to be actively updated. The last commit to the repo was made 3 days ago.

Cheers!

like image 130
Noahdecoco Avatar answered Sep 28 '22 03:09

Noahdecoco