我正在尝试使单击按钮时滚动平滑。它适用于本地 apache 服务器和 IE10+。但是当部署在“centos”上的项目中时,它在同一个浏览器上不起作用。不会产生错误。谁能帮我找出这背后的原因?
$(document).ready(function() {
//+ Scroll smoothly to the target href.
$("#btnGenerate").click(function(e){
e.preventDefault();
var offset = $( $.attr(this, 'href') ).offset().top;
if (offset == 0)
{
offset = 230;
}
$("html, body").animate({
scrollTop: offset
},
1000,
function () {
alert('animation complete.');
}
);
});
//-
});
HTML:
<input id="btnGenerate" href="#labelFirstChart" type="button" value="Generate"/>
最佳答案
这应该适用于具有 anchor 链接的元素:
$('a[href^=#]').on('click', function(e){
e.preventDefault();
var href = $(this).attr('href');
$('html, body').animate({
scrollTop:$(href).offset().top
},'slow');
});
这是一个例子:
http://codepen.io/acki/pen/wMGaPB
关于javascript - scrolltop 在 centos 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34326604/