简体   繁体   English

jQuery平滑滚动到顶部并按ID锚定(带有偏移)

[英]jQuery Smooth Scroll to Top AND to Anchor by ID (with Offset)

this is an extension to a questions that was previously answered here: See Prevoius Question 这是先前在此处回答的问题的扩展: 请参阅Prevoius问题

I have a bit of jQuery that is adding BOTH a smooth scroll to top function, AND a smooth scroll to any anchor found on a page. 我有一些jQuery,它既向顶部函数添加了平滑滚动,又向页面上找到的所有锚添加了平滑滚动。

Now, I just need to add an offset to the anchor scroll (110px) to account for fixed headers, WITHOUT messing up the scroll to top function. 现在,我只需要向固定滚动添加一个偏移量(110px)即可解决固定的标题,而不会弄乱滚动到顶部的功能。

Here's the existing code: 这是现有的代码:

// Add To Top Button + Smooth Scroll to Anchors functionality
jQuery(document).ready(function($){
    // Scroll (in pixels) after which the "To Top" link is shown
    var offset = 700,
    //Scroll (in pixels) after which the "back to top" link opacity is reduced
    offset_opacity = 1200,
    //Duration of the top scrolling animation (in ms)
    scroll_top_duration = 700,
    //Get the "To Top" link
    $back_to_top = $('.to-top');

//Visible or not "To Top" link
$(window).scroll(function(){
    ( $(this).scrollTop() > offset ) ? $back_to_top.addClass('top-is-visible') : $back_to_top.removeClass('top-is-visible top-fade-out');
    if( $(this).scrollTop() > offset_opacity ) { 
        $back_to_top.addClass('top-fade-out');
    }
});

//Smooth scroll to top
$back_to_top.on('click', function(event) {
    event.preventDefault();
    targetedScroll();
});

// example of smooth scroll to h2#anchor-name
$('#some-button').on('click', function(event) {
    event.preventDefault();
    targetedScroll('anchor-name');
});

// bind smooth scroll to any anchor on the page
$('a[href^="#"]').on('click', function(event) {
    event.preventDefault();
    targetedScroll($(this).attr('href').substr(1));
});

// scrolling function
function targetedScroll(id) {
    // scrollTop is either the top offset of the element whose id is passed, or 0
    var scrollTop = id ? $('#' + id).offset().top : 0;

    $('body,html').animate({
        scrollTop: scrollTop,
    }, scroll_top_duration);
}

});

Any time your're scrolling to an element, you have to scroll lower than the element by the height of the fixed navbar. 每次滚动到某个元素时,都必须向下滚动该元素,使其比固定导航栏的高度低。 Similarly, the first element on the page needs a margin or padding to offset by the height of the navbar. 类似地,页面上的第一个元素需要一个边距或填充以抵消导航栏的高度。

Since both of these offsets are the same, you can set the offset for scrolling at the same time you set the offset for the first element. 由于这两个偏移量都相同,因此可以在设置第一个元素的偏移量的同时设置滚动偏移量。 When you're scrolling to an element, subtract the offset from the height from the top. 滚动到某个元素时,请从顶部高度减去偏移量。 This will still work even when scrolling to the top. 即使滚动到顶部,此操作仍然有效。

jQuery(document).ready(function($) {
    var offset = $("nav").height();
    $("body").css("padding-top", offset + "px");
    $('a[href^="#"]').click(function(event) {
        event.preventDefault();
        var scrollY = $(this.href).offset().top;
        $('html, body').animate({
            scrollTop: scrollY - offset
        }, 2000);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM