1scroll function
2
3window.scroll({
4 top: 0,
5 left: 0,
6 behavior: 'smooth'
7});
8
9window.scroll(x-coord, y-coord)
10window.scroll(options)
1// HTML:
2<element onscroll="myScript">
3// JavaScript:
4object.addEventListener("scroll", myScript)
5// or
6object.onscroll = function() { /*...*/ }
1// handle links with @href started with '#' only
2$(document).on('click', 'a[href^="#"]', function(e) {
3 // target element id
4 var id = $(this).attr('href');
5
6 // target element
7 var $id = $(id);
8 if ($id.length === 0) {
9 return;
10 }
11
12 // prevent standard hash navigation (avoid blinking in IE)
13 e.preventDefault();
14
15 // top position relative to the document
16 var pos = $id.offset().top;
17
18 // animated top scrolling
19 $('body, html').animate({scrollTop: pos});
20});