所以这是我目前的代码
$(document).ready(function() {
$('.abouta').click(function(){
$('html, body').animate({scrollTop:308}, 'slow');
return false;
});
$('.portfolioa').click(function(){
$('html, body').animate({scrollTop:708}, 'slow');
return false;
});
$('.contacta').click(function(){
$('html, body').animate({scrollTop:1108}, 'slow');
return false;
});
});
点击链接时,例如'abouta'它滚动到页面的特定部分。我宁愿做一个scrollTo然后是一个div的id,这样如果我改变填充等我就不必继续改变scrollTo的位置。有什么办法吗? 感谢
答案 0 :(得分:102)
而不是
$('html, body').animate({scrollTop:xxx}, 'slow');
使用
$('html, body').animate({scrollTop:$('#div_id').position().top}, 'slow');
这将返回您选择的任何元素的绝对顶部位置#div_id
答案 1 :(得分:1)
我的解决方法如下:
document.getElementById("agent_details").scrollIntoView();
答案 2 :(得分:0)
试试这个:
$('html, body').animate({scrollTop:$('#xxx').position().top}, 'slow');
$('#xxx').focus();
答案 3 :(得分:-2)
如果你只想滚动一些div,可以使用div id代替'html,body'
$('html, body').animate(...
使用
$('#mydivid').animate(...
答案 4 :(得分:-2)
试试这个
$('#div_id').animate({scrollTop:0}, '500', 'swing');