Created
April 6, 2018 05:52
-
-
Save rahuls360/a787038c6a7f35877f4fce24862d61de to your computer and use it in GitHub Desktop.
Href Smooth Scroll to #id (responsive)
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='#id']").click(function(event){ | |
$('html, body').animate({ | |
scrollTop: $( $.attr(this, 'href') ).offset().top | |
}, 500); | |
event.preventDefault(); | |
}); |
<script type="text/javascript">
$("a[href]").click(function(event){
const windowWidth = $(window).width();
const mobileOffset = 140;
const desktopOffset = 85;
var offset;
if(windowWidth <= 991){
offset = mobileOffset;
}else{
offset = desktopOffset;
}
$('html, body').animate({
scrollTop: $( $.attr(this, 'href').slice(1) ).offset().top - offset
}, 500);
});
</script>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add +ve or -ve offset
$(".scroll").click(function(event){$( $ .attr(this, 'href') ).offset().top - 100
$('html, body').animate({
scrollTop:
}, 500);
event.preventDefault();
});