I'm writing a website and I want to build some window-sized divs/sections. When the user stops scrolling between two of these divs, the div which takes more space in that moment should be scrolled at, so it's gonna be full-window-sized.
You can use window. scroll() with behavior: smooth and top set to the anchor tag's offset top which ensures that the anchor tag will be at the top of the viewport.
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 .
Enable Anchor Scrolling on Chrome Browser (Desktop)Navigate to Chrome://flags/#enable-scroll-anchoring. Select “Enabled” from the dropdown menu.
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);
There are jQuery plugins to do this but here's a good template to see how you can implement your own:
https://startbootstrap.com/template-overviews/scrolling-nav/
Here's the relevant scrolling JavaScript for easing function to make it nice and scrollable:
//jQuery to collapse the navbar on scroll
$(window).scroll(function() {
if ($(".navbar").offset().top > 50) {
$(".navbar-fixed-top").addClass("top-nav-collapse");
} else {
$(".navbar-fixed-top").removeClass("top-nav-collapse");
}
});
//jQuery for page scrolling feature - requires jQuery Easing plugin
$(function() {
$('a.page-scroll').bind('click', function(event) {
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top
}, 1500, 'easeInOutExpo');
event.preventDefault();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With