平滑滚动到顶部不工作顺利
smooth scroll to top not working smoothly
我试图使滚动到顶部平滑,但我提供的代码似乎不起作用。滚动就是不流畅…
$(function() {
$('a[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 targetOffset = $target.offset().top;
$('html,body').animate({
scrollTop: targetOffset
}, 1000);
return false;
}
}
});
});
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<a name="top" id="top"></a>
<div style="height:2000px">
a hell lot of content here
</div>
<a href="#top">back to top</a>
为什么不工作?
您必须使用||
而不是&amp;&amp;
并进行一些小的更改纠正:
$(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;
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="top">Some things here</div> .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here .Some things here ..<a href="#top">TOP</a>
这里小提琴
相关文章:
- jQuery偏移量顶部没有'工作不正常
- 如何停止两个按钮执行相同的工作?在这种情况下,它们都会滚动到顶部
- 固定顶部导航的impress.js无法工作
- 滚动到不与溢出一起工作的顶部脚本:滚动
- 当设置为导航栏固定顶部时,一页引导导航栏切换在移动模式下无法正常工作
- 滚动顶部不工作
- 滚动到顶部不工作的safari
- 平滑滚动到顶部不工作
- 平滑滚动到顶部不工作顺利
- 滚动顶部突然不工作的粘性菜单
- 页眉内的Div /靠近页面顶部/在2次点击事件后不显示,而它的's页脚双胞胎工作- jquery
- jQuery JS滚动到顶部的点击只工作一次
- 当想要滚动到顶部时,scrollTo不工作
- 滚动到页面的顶部(不工作在chrome)
- 回到顶部"元素,它与jQuery弹出窗口一起工作
- 滚动回顶部,在IE6中不工作
- 回到顶部按钮不工作在我的网站
- 文本框到页面顶部的过渡在IOS的Safari和Chrome上不能正常工作
- JavaScript在PHP页面的顶部不工作
- 只有顶部按钮向下滑动工作CSS jquery