Redirect to a div on a different page with smooth scrolling?

后端 未结 8 1185
悲哀的现实
悲哀的现实 2021-01-01 15:33

Question Background:

I have a 2 page website. Both pages use the same masterlayout.cshtml page which features a Navbar. Within the

8条回答
  •  醉酒成梦
    2021-01-01 16:27

    You can track hash of page url and scroll page to particular div

    Welcome
    Welcome 
    

    Jquery

    $(document).ready(function(){
        var speed = 1000;
    
        // check for hash and if div exist... scroll to div
        var hash = window.location.hash;
        if($(hash).length) scrollToID(hash, speed); 
    
        // scroll to div on nav click
        $('.scroll-link').click(function (e) {
            e.preventDefault();
            var id = $(this).attr('href');
            if($(id ).length) scrollToID(id, speed);
        });
    })
    
    function scrollToID(id, speed) {
        var offSet = 70;
        var obj = $(id).offset();
        var targetOffset = obj.top - offSet;
        $('html,body').animate({ scrollTop: targetOffset }, speed);
    }
    

提交回复
热议问题