Created
June 19, 2015 23:50
-
-
Save jorgechavz/1015467773de5e008bbf to your computer and use it in GitHub Desktop.
Scrolling to HTML element
This file contains 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
<a href="#" data-go="#node-element" class="go">Go to</a> | |
<script type="text/javascript"> | |
$(".go").click(function(e){ | |
//We prevent the default scroll | |
e.preventDefault(); | |
var $this = $(this), | |
to = $this.attr("data-go"), | |
topSlider = $(to).position().top; | |
//We scroll to to the DOM element | |
$("body, html").animate({ | |
scrollTop : topSlider | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment