[英]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.