Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 制作一个<;a>;元素在单击时滚动到a<;部门>;_Jquery_Html - Fatal编程技术网

Jquery 制作一个<;a>;元素在单击时滚动到a<;部门>;

Jquery 制作一个<;a>;元素在单击时滚动到a<;部门>;,jquery,html,Jquery,Html,我一直在寻找使用我在这里找到的一些jQuery来解决这个问题。 我有一个锚,当点击时,我希望它一直向下滚动到一个div。我设法使它滚动到页面底部,但这会在从移动设备查看网站时产生问题。这是我的密码: HTML 您不需要做很多jQuery。为你的div设置动画将起作用。你可以调整它的高度,以防你不想把它粘在顶部 $("#button").click(function() { $('html, body').animate({ scrollTop: $("#myDiv").o

我一直在寻找使用我在这里找到的一些jQuery来解决这个问题。 我有一个锚,当点击时,我希望它一直向下滚动到一个div。我设法使它滚动到页面底部,但这会在从移动设备查看网站时产生问题。这是我的密码:

HTML


您不需要做很多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);
});