Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to next anchor after scrolling

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.

like image 995
Malte Avatar asked Dec 30 '16 15:12

Malte


People also ask

How do I smooth scroll to anchor?

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.

How do I scroll an HTML page to an anchor?

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 .

How do I turn on scroll anchoring?

Enable Anchor Scrolling on Chrome Browser (Desktop)Navigate to Chrome://flags/#enable-scroll-anchoring. Select “Enabled” from the dropdown menu.

How do you navigate to another page with a smooth scroll on a specific ID?

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);


1 Answers

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();
    });
});
like image 194
jojo Avatar answered Oct 26 '22 16:10

jojo