2016-07-28 117 views
0

任何人都知道我可以滚动到页面上的某个位置时,点击使用JS的链接?例如,点击时,滚动到窗口顶部500px的位置。滚动到窗口顶部点击某个位置

我使用的ScrollMagic插件和我的网站内容被滚动位置激活,所以我不可能只使用锚链接。它也不能从当前位置偏移,因为这也不起作用。

任何想法?

+1

只需使用[scrollTop的(Y)](https://api.jquery.com/scrollTop/# scrollTop2) –

回答

1

我reccomand的jQuery做:)在这里工作的所有设备版本

$(document).ready(function() //When the page is ready, load function 
{ 
    $("#some_id").click(function() // When arrow is clicked 
    { 
     $("body,html").animate(
     { 
      scrollTop : 500      // Scroll 500px from top of body 
     }, 400); //how fast the scrolling animation will be in miliseconds 
    }); 
}); 
3

这应该做的伎俩在纯JS:

document.body.scrollTop = 500; 
1

会是这样的工作?它会给你一个平滑的滚动到该位置为你连接它的任何链接。

$('a[href*=#]').click(function() { 
    $('html, body').animate({scrollTop: 500}, 500); 
}