[英]Smooth scrolling to anchor on different page
我在Google上搜索了如何平滑滾動到其他頁面上的錨點。 我嘗試了幾種方法,但是,它們似乎無法與我已經編寫的可在一頁上平滑滾動的代碼一起使用。
基本上,我已經將我的聯系方式部分固定在索引頁面上,並希望當我單擊導航欄上的聯系方式鏈接時,其他頁面可以平滑滾動到該部分。 但目前,它所做的只是直接跳到錨點,而無需從頂部滾動。
這是我的代碼:
$(document).ready(function() {
$('a[href^="#"]').not("#quote-carousel a, ul.nav-pills a, div#accordian a").on("click", function(a) {
a.preventDefault();
var b = this.hash,
c = $(b);
$("html, body").stop().animate({
scrollTop: c.offset().top - 50
}, 900, "swing", function() {
window.location.hash = b
})
})
});
您可以在哈希更改以及頁面加載時開始滾動。 因此,您可以滾動到目標元素並創建一個深層鏈接。
因此,您的方法和我的方法之間的主要區別是,首先更改哈希值(或不阻止哈希值),然后監聽哈希值更改事件。
這是我的方法:
JavaScript:
$(document).ready(function() {
// check for hash when page has loaded
if (getHash() != null) {
checkForScrolling();
}
});
// listen for hash change event here
window.onhashchange = function() {
checkForScrolling();
};
// return hash if so or null if hash is empty
function getHash() {
var hash = window.location.hash.replace('#', '');
if (hash != '') {
return hash;
} else {
return null;
}
}
// this function handles your scrolling
function checkForScrolling() {
// first get your element by attribute selector
var elem = $('[data-anchor="' + getHash() + '"]');
// cheeck if element exists
if (elem.length > 0) {
$('html, body').stop().animate({
scrollTop: elem.offset().top
}, 300);
}
}
有效的HTML示例:
<!DOCTYPE html>
<html>
<head>
<title>Smooth Scrolling</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
// check for hash when page has loaded
if (getHash() != null) {
checkForScrolling();
}
});
// check for hash when hash has changed
window.onhashchange = function() {
checkForScrolling();
};
// return hash if so or null if hash is empty
function getHash() {
var hash = window.location.hash.replace('#', '');
if (hash != '') {
return hash;
} else {
return null;
}
}
// this function handles your scrolling
function checkForScrolling() {
// first get your element by attribute selector
var elem = $('[data-anchor="' + getHash() + '"]');
// cheeck if element exists
if (elem.length > 0) {
$('html, body').stop().animate({
scrollTop: elem.offset().top
}, 300);
}
}
</script>
<style type="text/css">
body { font-family: Helvetica }
section { position: relative; margin-bottom:10px; padding:20px; height: 300px; background-color: #eee; }
section a { display:block; position: absolute; bottom: 10px; }
</style>
</head>
<body>
<div>
<h1 data-anchor="start">Smooth Scrolling</h1>
<ul>
<li><a href="#1">Scroll to Anchor 1</a></li>
<li><a href="#2">Scroll to Anchor 2</a></li>
<li><a href="#3">Scroll to Anchor 3</a></li>
</ul>
<section>
<h2 data-anchor="1">First Anchor</h2>
<a href="#start">Back to top</a>
</section>
<section>
<h2 data-anchor="2">Second Anchor</h2>
<a href="#start">Back to top</a>
</section>
<section>
<h2 data-anchor="3">Third Anchor</h2>
<a href="#start">Back to top</a>
</section>
</div>
</body>
</html>
在點擊錨標簽時添加以下代碼,考慮以下html
<div class="class_name"><a href="#contact">Contact</a></div>
為上面的html添加代碼
$(document).on('click', '.class_name a', function(e) {
e.preventDefault();
var target = $(this).attr('href'), $target = $(target); {
$('html, body').stop().animate({
'scrollTop' : $target.offset().top - 50
}, 900, 'swing', function() {
window.location.hash = target;
});
}
});
上面的-50值根據標題高度而變化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.