Jquery 制作一个<;a>;元素在单击时滚动到a<;部门>;
我一直在寻找使用我在这里找到的一些jQuery来解决这个问题。 我有一个锚,当点击时,我希望它一直向下滚动到一个div。我设法使它滚动到页面底部,但这会在从移动设备查看网站时产生问题。这是我的密码: HTMLJquery 制作一个<;a>;元素在单击时滚动到a<;部门>;,jquery,html,Jquery,Html,我一直在寻找使用我在这里找到的一些jQuery来解决这个问题。 我有一个锚,当点击时,我希望它一直向下滚动到一个div。我设法使它滚动到页面底部,但这会在从移动设备查看网站时产生问题。这是我的密码: HTML 您不需要做很多jQuery。为你的div设置动画将起作用。你可以调整它的高度,以防你不想把它粘在顶部 $("#button").click(function() { $('html, body').animate({ scrollTop: $("#myDiv").o
您不需要做很多jQuery。为你的div设置动画将起作用。你可以调整它的高度,以防你不想把它粘在顶部
$("#button").click(function() {
$('html, body').animate({
scrollTop: $("#myDiv").offset().top - 150 /*(150) is height to leave from top*/
}, 2000);
});
$('html,body').animate({scrollTop:$(“#locations”).offset().top-150},'slow');})代码>。调整滚动位置,使其不会到达顶部您的locHeight
设置为$('main-button-section')。outerHeight()代码>。您缺少一个类选择器的
。@Mufi非常感谢,这就成功了!
//this is what I used to scroll to the bottom
//it does what it needs to but not good for mobile devices
$('.right-side-button').click(function(){
$('html, body').animate({ scrollTop: $(document).height()}, 'slow');});
//this is the code I am using in order to scroll to div
//for some reason it is not working when I click the anchor
var locHeight = $('main-button-section').outerHeight();
$('.main-button-section a').click(function(e){
e.preventDefault();
var myHref = $(this).attr('href');
var newPos = $(myHref).offset().top;
$('html, body').stop().animate({scrollTop: newPos-locHeight}, 1300);
});
$("#button").click(function() {
$('html, body').animate({
scrollTop: $("#myDiv").offset().top - 150 /*(150) is height to leave from top*/
}, 2000);
});