【问题标题】:Scroll to anchor point using js使用js滚动到锚点
【发布时间】:2020-06-09 22:49:28
【问题描述】:

我使用this code打开和切换不同的divs。我需要用它来滚动到一个锚点(它在打开的div 内)。

有可能吗?

非常感谢。

【问题讨论】:

  • 我使用你的 jsfiddle 实现了 scrollIntoView() 方法来扩展我的答案。

标签: javascript html css wordpress


【解决方案1】:

滚动到特定数量的像素: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 为例,在函数之外声明变量,使其具有全局性。

【讨论】:

  • 酷,它有效。我正在使用您的第二个选项jsfiddle。可以在here 网站上看到它的工作原理。 - 可以制作流畅的卷轴吗?对于普通链接,我使用了一个制作它的类,但我不能以这种方式做到这一点。 - 是否可以在滚动中添加时间延迟?谢谢克里斯!
  • 你在我的 jsFiddle 的 css 中注意到了吗:html {scroll-behavior: smooth;}
猜你喜欢
  • 1970-01-01
  • 2014-12-08
  • 2014-09-21
  • 1970-01-01
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
相关资源
最近更新 更多