有没有办法知道用户是否在特定的标题、段落或 div 等
Is there a way to know if the user is on a particular heading, paragraph or a div etc
我试图找到一种仅在用户在动画上时才启动动画的方法,起初我尝试获取屏幕尺寸及其效果,但这不是最佳解决方案。
谷歌搜索后,我发现了一些可以在滚动时获取 ID(通过 ID 标签)的东西,如果我可以通过某种方式在向下滚动时获取 ID,我可以使用简单的 JavaScript 函数来播放动画。
有什么办法吗?
好的,我自己找到的。
我想做什么?我只想在用户看到 it/on it/have 传递先前信息时才启动动画(技能栏)。
我通过使用 JavaScript 部分来做到这一点,然后在用户使用它时触发动画。
一个例子(我很抱歉代码混乱,我不知道在这里对齐代码)
<section id="name">
<div class="hello">
<h1>Hello world</h1>
</div>
</section>
<section id="aboutMe">
<div class="hello">
<h1>Hello world</h1>
</div>
</section>
<section id="skills">
<div class="hello">
<h1>these are my skills</h1>
</div>
</section>
JAVASCRIPT:
var element = document.querySelector("#skills");
function isInViewPort(element) {
// Get the bounding client rectangle position in the viewport
var bounding = element.getBoundingClientRect();
// Checking part. Here the code checks if it's *fully* visible
// Edit this part if you just want a partial visibility
if (
bounding.top >= 0 &&
bounding.left >= 0 &&
bounding.right <= (window.innerWidth || document.documentElement.clientWidth) &&
bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight)
) {
return true;
} else {
return false;
}
}
var element = document.querySelector("#skills");
if (isInViewPort(element)) {
alert('true')
}
希望这对以后的人有所帮助
我试图找到一种仅在用户在动画上时才启动动画的方法,起初我尝试获取屏幕尺寸及其效果,但这不是最佳解决方案。 谷歌搜索后,我发现了一些可以在滚动时获取 ID(通过 ID 标签)的东西,如果我可以通过某种方式在向下滚动时获取 ID,我可以使用简单的 JavaScript 函数来播放动画。 有什么办法吗?
好的,我自己找到的。 我想做什么?我只想在用户看到 it/on it/have 传递先前信息时才启动动画(技能栏)。 我通过使用 JavaScript 部分来做到这一点,然后在用户使用它时触发动画。
一个例子(我很抱歉代码混乱,我不知道在这里对齐代码)
<section id="name">
<div class="hello">
<h1>Hello world</h1>
</div>
</section>
<section id="aboutMe">
<div class="hello">
<h1>Hello world</h1>
</div>
</section>
<section id="skills">
<div class="hello">
<h1>these are my skills</h1>
</div>
</section>
JAVASCRIPT:
var element = document.querySelector("#skills");
function isInViewPort(element) {
// Get the bounding client rectangle position in the viewport
var bounding = element.getBoundingClientRect();
// Checking part. Here the code checks if it's *fully* visible
// Edit this part if you just want a partial visibility
if (
bounding.top >= 0 &&
bounding.left >= 0 &&
bounding.right <= (window.innerWidth || document.documentElement.clientWidth) &&
bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight)
) {
return true;
} else {
return false;
}
}
var element = document.querySelector("#skills");
if (isInViewPort(element)) {
alert('true')
}
希望这对以后的人有所帮助