平滑滚动以锚定在不同的页面上
Smooth scrolling to anchor on different page
>我已经在谷歌上搜索了如何平滑滚动到不同页面上的锚点。我已经尝试了一些方法,但是,它们似乎不适用于我已经编写的代码,这些代码可以在一页上平滑滚动。
基本上,我已经将我的联系人部分锚定在索引页面上,并希望我的其他页面在单击导航栏上的联系人链接时平滑滚动到该部分。但目前它所做的只是直接跳到锚点,而无需从顶部滚动。
这是我的代码:
$(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 值根据标题高度而变化。
相关文章:
- Safari(Mac OS)上的jQuery平滑滚动问题
- jquery平滑滚动问题
- javascript mootools平滑滚动'x'像素数量
- jQuery平滑滚动
- jQuery平滑滚动未滚动
- 更新Wordpress和我的平滑滚动停止工作
- 具有子菜单和平滑滚动的粘性导航
- 平滑滚动&懒惰
- 平滑滚动时更改 ID 的颜色
- 使用 jQuery 在滚动时平滑过渡图像
- 使用我的基于标签的平滑滚动脚本而不带“a”的解决方案
- 调整平滑滚动代码
- 使用 window.scrollBy) 平滑滚动
- 平滑滚动代码
- 如何在ng视图中进行页面渲染后的平滑滚动
- 如何使用requestAnimationFrame实现平滑滚动
- 如何由于浏览器的平滑滚动而忽略大量滚动事件
- Javascript-使用鼠标滚轮进行平滑视差滚动
- 使用普通JavaScript平滑滚动到元素
- 平滑滚动并突出显示导航问题