[英]scrollTop jQuery, scrolling to div with id?
So this is the current code I have所以这是我目前的代码
$(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;
});
});
When you click a link eg 'abouta' it scrolls to that specific section of the page.当您单击链接(例如“abouta”)时,它会滚动到页面的特定部分。 I'd rather do a
scrollTo
and then the id of a div so that I don't have to keep changing the scrollTo
position if I change padding etc. Is there any way?我宁愿先做一个
scrollTo
,然后再做一个 div 的 id,这样如果我更改 padding 等,我就不必继续更改scrollTo
position。有什么办法吗?
instead of而不是
$('html, body').animate({scrollTop:xxx}, 'slow');
use使用
$('html, body').animate({scrollTop:$('#div_id').position().top}, 'slow');
this will return the absolute top position of whatever element you select as #div_id
这将返回您选择为
#div_id
的任何元素的绝对顶部位置
我的解决方案如下:
document.getElementById("agent_details").scrollIntoView();
try this:试试这个:
$('html, body').animate({scrollTop:$('#xxx').position().top}, 'slow');
$('#xxx').focus();
试试这个
$('#div_id').animate({scrollTop:0}, '500', 'swing');
Use this:用这个:
$('html').animate({ scrollTop: $("#yourDiv").offset().top-100 }, "slow");
if you want to scroll just only some div, can use the div id instead of 'html, body'如果你只想滚动一些 div,可以使用 div id 而不是 'html, body'
$('html, body').animate(...
use使用
$('#mydivid').animate(...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.