0
我使用最新評論here中的jQuery代碼片段。在用戶點擊選項卡時,如何避免第二種情況下的這種行爲?如何避免在某些內部鏈接上平滑滾動?
GOOD (it's working as it should):
<a href="#schedule">Schedule</a>
BAD (scrolls to the top of the page, which I dont want):
<a class="nav-link" data-toggle="tab" href="#tour" role="tab">TOUR</a>
<script>
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var $target = $(this.hash);
$target = $target.length ? $target : $('[name=' + this.hash.slice(1) +']');
if ($target.length) {
var baseMinScrollTime = 500,
baseMaxScrollTime = 900;
var docHeight = $(document).height(),
triggerTop = $(this).offset().top,
targetTop = $target.offset().top;
var scrollProportion = (targetTop - triggerTop)/docHeight,
relativeTime = ((baseMaxScrollTime - baseMinScrollTime) * scrollProportion) + baseMinScrollTime,
// Create inverse relationship (quicker the further we scroll)
scrollTime = -1 * (1 - relativeTime);
$('html, body').animate({
scrollTop: targetTop - 10
}, scrollTime);
return false;
}
}
});
</script>
謝謝!我如何傳遞多個鏈接:#home,#one,#two,#three? – Jake
更新了我的答案。它現在應該適合你 –