jQuery平滑滚动定位不起作用 [英] jQuery smooth scroll positioning doesn't work
本文介绍了jQuery平滑滚动定位不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对导航链接使用平滑的滚动,但是导航固定,因此必须稍微更改着陆位置. 现在,我在这个论坛中找到了答案,它看起来像这样:
I want to use a smooth scroll for my nav-links, but I am having a fixed nav, so I have to change the landing position a little bit. Now I found the answer in this forum and it looks like this:
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash;
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top-100
}, 900, 'swing', function () {
window.location.hash = target;
});
});
});
前100名"应该可以使用,但是不能... 有人有主意吗? 谢谢
The "top-100" should work, but it just doesn't... Does anybody have an idea? Thanks
推荐答案
使用 partseInt
在浏览器中始终使用 int
值.
Use partseInt
to always use the int
value in browsers.
'scrollTop': parseInt($target.offset().top,10);
parseInt()函数分析字符串参数,并返回指定基数或基数的整数.
parseInt() function parses a string argument and returns an integer of the specified radix or base.
语法
parseInt(string, radix);
- 字符串:要解析的值.如果string不是字符串,则将其转换为一个.
- 基数:2到36之间的整数,表示上述字符串的基数.
- string : The value to parse. If string is not a string, then it is converted to one.
- radix : An integer between 2 and 36 that represents the radix of the above mentioned string.
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash;
$target = $(target);
$('html, body').stop().animate({
'scrollTop': parseInt($target.offset().top,10)
}, 900, 'swing', function () {
window.location.hash = target;
});
});
});
这篇关于jQuery平滑滚动定位不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文