使用js滚动到锚点 [英] Scroll to anchor point using js

查看:94
本文介绍了使用js滚动到锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码来打开和切换不同的div.我需要使用它来滚动到一个锚点(它在打开的 div 内).

I use this code to open and switch between different divs. I would need use it to make scroll to an anchor point (it is inside the open div).

有可能吗?

非常感谢.

推荐答案

滚动到特定数量的像素:jsFiddle

Scroll to a particular number of pixels: jsFiddle

var body = document.body; // Safari
var html = document.documentElement; // Chrome, Firefox, IE and Opera places the overflow at the <html> level, unless else is specified. Therefore, we use the documentElement property for these browsers
body.scrollTop += 100;
html.scrollTop += 100;

滚动到一个元素:jsFiddle

var elmnt = document.getElementById("scroll");
elmnt.scrollIntoView();

以您的 jsFiddle 为例,在函数外部声明变量,以便是全球性的.

Using your jsFiddle as the example, declare the variable outside of the functions so it is global.

这篇关于使用js滚动到锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆