Created
February 20, 2015 12:09
-
-
Save nessthehero/3a965faa99f64adae944 to your computer and use it in GitHub Desktop.
goToByScroll.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function goToByScroll(id) { | |
'use strict'; | |
// Assign the HTML, Body as a variable... | |
var $viewport = $('html, body'); | |
$viewport.animate({ | |
scrollTop: $("#" + id).offset().top // set scrollTarget to your desired position | |
}, 1000); | |
// Stop the animation if the user scrolls. Defaults on .stop() should be fine | |
$viewport.bind("scroll mousedown DOMMouseScroll mousewheel keyup touchstart", function(e) { | |
if (e.which > 0 || e.type === "mousedown" || e.type === "mousewheel") { | |
$viewport.stop().unbind('scroll mousedown DOMMouseScroll mousewheel keyup touchstart'); // This identifies the scroll as a user action, stops the animation, then unbinds the event straight after (optional) | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment