平滑滚动使用jquery
Smooth scroll using jquery
我有html页面和菜单
菜单:
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<li>
<a href="#info">info</a>
</li>
...
</ul>
</div>
部分内容:
<div id="info" class="row">
<div class="col-lg-12">
<h1>Title</h1>
<p>Lorem Ipsum</p>
</div>
</div>
我想给锚添加平滑的页面滚动。所以我试了:
$(document).ready(function($) {
$('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) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
但是它不起作用。滚动并不平滑。
将jQuery函数替换为:
$(function() {
$('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) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
相关文章:
- jQuery滚动功能只工作一次
- jQuery滚动器插件修改
- 当元素可见时,jQuery滚动函数会触发一次
- jQuery滚动到聊天框底部
- 如何发现用户是否使用jQuery滚动到HTML容器的末尾
- 使用 jQuery 滚动窗格
- jQuery 滚动到锚点适用于移动设备,但不适用于桌面
- JQuery 滚动列表框模仿键盘导航
- JQuery 滚动到锚点行为奇怪
- jquery滚动功能无法正常工作
- JQuery滚动到底部
- 使用jQuery滚动获取距窗口顶部的距离
- jQuery滚动&锚初始位置
- 如何使用jQuery滚动到页面顶部
- 核心滚动标题面板+jQuery滚动
- iOS jquery滚动视图插件不会在iframe上滚动y
- 取消弹出jquery滚动事件
- jQuery滚动力不会滚动超过450px
- jQuery滚动出现问题
- Jquery滚动到基于数据属性的部分