簡體   English   中英

jQuery滾動到ID腳本僅向下滾動

[英]jQuery scroll to ID script only scrolling down

我正在使用以下內容將.jump-link類的所有錨點滾動到href屬性中引用的ID:

// Scroll to ID
$('.jump-link').click(function () {
    var el = $(this).attr('href'),
        elWrapped = $(el);
    scrollToId(elWrapped, 40);
    return false;
});
function scrollToId(element, navheight) {
    var offset = element.offset(),
        offsetTop = offset.top,
        totalScroll = offsetTop - navheight;
    $('body,html').animate({
        scrollTop: totalScroll
    }, 500);
}

不幸的是,盡管它似乎只能向下滾動頁面。 我也希望腳本能夠向上滾動頁面。

試試這個。。會起作用的。

$(".jump-link").click(function(){
 var id = $(this).attr('href');
        $('html, body').animate({scrollTop: $("#"+id).offset().top}, 2000);
});

這是代碼試試這個

$(function() {
    $('ul.nav a').bind('click',function(event){
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top
        }, 1200);
        event.preventDefault();
    });
}); 
<script type="text/javascript">
        jQuery(document).ready(function($) {
            $(".scroll").click(function(event){     
                event.preventDefault();
                $('html,body').animate({scrollTop:$(this.hash).offset().top},1200);
            });
        });
    </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM