如何在单击链接时滚动到元素
How to scroll to an element when a link is clicked
所以我有以下网站:http://www.gameplay-universe.uphero.com/
你可以看到"跳到内容"的链接。我想当它点击页面滚动到div#content
。我使用的是最新版本的jQuery。我怎样才能做到这一点呢?
好的,这是我找到的解决方案:
$(document).ready(function() {
// Click event for any anchor tag that's href starts with #
$('a[href^="#"]').click(function(event) {
// The id of the section we want to go to.
var id = $(this).attr("href");
// An offset to push the content down from the top.
var offset = 60;
// Our scroll target : the top position of the
// section that has the id referenced by our href.
var target = $(id).offset().top - offset;
// The magic...smooth scrollin' goodness.
$('html, body').animate({scrollTop:target}, 500);
//prevent the page from jumping down to our section.
event.preventDefault();
});
});
$("li a").click(function(){
$('html, body').animate({
scrollTop: $( $.attr(this, 'href') ).offset().top
}, 1000);
return false;
});
对于li元素中的所有链接都有效
$(document).scrollTop($('div#content').offset().top)
当我想要启用"scroll to"类型的Jquery效果时,我喜欢使用这个脚本。
http://cferdinandi.github.io/smooth-scroll/相关文章:
- 使用webdriver io在可滚动元素内滚动
- 每次单击按钮时水平滚动元素
- 可滚动元素,如何在调整大小时更改顶部偏移量
- 如何使用javascript检测不可滚动元素中的滚动事件和方向
- 滚动元素的交叉溶解过渡
- 自动上下滚动元素
- 检测可滚动元素内的滚动位置
- 为滚动元素制作动画
- 如何自动滚动元素
- 使用鼠标滚轮滚动元素时传播的事件是什么
- 在鼠标滚动时使用 JQuery 水平滚动元素
- 在屏幕顶部附近滚动元素时触发的侦听器
- 如何在调整大小时更改滚动元素的行为?(HTML,CSS,JavaScript)
- 滚动元素上的Javascript触摸移动事件
- Poshy提示不随滚动元素移动
- 如何在JQuery中滚动元素
- jsPDFaddHTML获取滚动元素的全部内容
- 使用鼠标滚轮滚动页面时,避免滚动可滚动元素
- 滚动元素到视图中而不与页眉重叠
- 如何为自定义滚动元素添加限制