如果我的网页的用户在其上达到某个点,是否可以运行一个函数?

发布于 2025-02-04 21:54:45 字数 393 浏览 2 评论 0原文

我尝试与不同的divs进行网站,或者对我来说是部分。如果我到达其中之一的顶部,则应该安装本学期日志。如果您问,则滚动量等于设备的ScreenHeight的1%。

let Point1 = false;
document.addEventListener("scroll", e=> {
if (document.documentElement.scrollTop >= 150*ScrollHeight) {
    if (Point1 == false){
        Point1 = true;
        Point1F();
    };
}
})
function Point1F() {
console.log("U've done it');
}

但这对我来说并不折腾。

I try do a webite with different divs or for me they are sections. If I reached the top of one of these it should console log this term. If u ask, ScrollHeight is equal to 1% of the devices' screenheight.

let Point1 = false;
document.addEventListener("scroll", e=> {
if (document.documentElement.scrollTop >= 150*ScrollHeight) {
    if (Point1 == false){
        Point1 = true;
        Point1F();
    };
}
})
function Point1F() {
console.log("U've done it');
}

But its not woking for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情 2025-02-11 21:54:45

您的代码有效,因为我认为为什么您看不到.log()的问题是因为您没有达到它。

如果卷轴是(如您所说)“设备的1%ScreenHeight”,则需要HTML高度〜3倍您的屏幕高度;

document.documentElement.style.height = "300vh";

// getting 1% of screen height
const scrollHeight = screen.height / 100; 
const scrollTriggerPoint = scrollHeight * 150;

let point1 = false;

document.addEventListener("scroll", (e) => {
  if (document.documentElement.scrollTop >= scrollTriggerPoint) {
    if (point1 == false){
        point1 = true;
        point1F();
    };
  }
});

function point1F() {
  console.log("u've done it");
}

PS

不要使用以大写字母开头的变量/函数的名称,请在; y上用于构造函数函数或类。

Your code works, as i think the problem why you don't see your .log() is because you didn't reach it.

If scrollHeight is (as you said) "1% of the devices' screenheight", then you need html height to be ~ 3x your screen height;

document.documentElement.style.height = "300vh";

// getting 1% of screen height
const scrollHeight = screen.height / 100; 
const scrollTriggerPoint = scrollHeight * 150;

let point1 = false;

document.addEventListener("scroll", (e) => {
  if (document.documentElement.scrollTop >= scrollTriggerPoint) {
    if (point1 == false){
        point1 = true;
        point1F();
    };
  }
});

function point1F() {
  console.log("u've done it");
}

P.S.

Don't use variable's/function's names starting with a capital letter, use it on;y for constructor functions or classes.

一场信仰旅途 2025-02-11 21:54:45

交叉点观察者API

使用滚动位置时,当您具有单个触发点时,可以使用滚动位置。但是,当有多个触发点(如问题所示)并且它们在不同设备上的位置不一致时,则相交观察者API 是一个有用的解决方案。

mdn:

过去涉及的事件实施相交检测
处理程序和循环呼叫方法类似
element.getBoundingClientRect()来构建所需的信息
每个元素都受到影响。由于所有这些代码都在主线程上运行,因此
甚至其中之一也会引起性能问题。当一个网站是
有了这些测试,事情可能会变得彻头彻尾。

您可以在文档或容器元素上创建一个观察者,然后添加要观看的元素。当元素达到阈值设置时,触发回调。

演示片段

摘要显示了如何在滚动进出时观察到不同的部分。

// create an observer on the document or container element

let observer = new IntersectionObserver(([entry]) => {
  if (entry.isIntersecting) {

    // code to execute when the section becomes visible

    console.log("is visible: " + entry.target.id);
    
    // uncomment to trigger only once per section
    // observer.unobserve(entry.target); 
  }
}, {
  root: document,  // or container element or null
  rootMargin: "0px",
  threshold: 0.1
});

// add each section to the observer

document.querySelectorAll("section").forEach(target => {
  observer.observe(target);
});
section {
  height: 5em;
  margin: 1em;
  margin-bottom: 20em;
  background-color: lightblue;
}
Scroll down the page to trigger the observer

<section id="section1">Section 1</section>
<section id="section2">Sectopm 2</section>
<section id="section3">Section 3</section>
<section id="section4">Section 4</section>
<section id="section5">Section 5</section>

Intersection Observer API

Using scroll position is fine when you have a single trigger point. However, when there are multiple trigger points (as the question suggests) and they are not in a consistent position on different devices, then the Intersection Observer API is a useful solution.

MDN:

Implementing intersection detection in the past involved event
handlers and loops calling methods like
Element.getBoundingClientRect() to build up the needed information for
every element affected. Since all this code runs on the main thread,
even one of these can cause performance problems. When a site is
loaded with these tests, things can get downright ugly.

You create an observer on the document or a container element and then add the elements you want to watch. And the callback is triggered when an element reaches the threshold setting.

Demo Snippet

The snippet shows how to observe different sections as they scroll in and out of view.

// create an observer on the document or container element

let observer = new IntersectionObserver(([entry]) => {
  if (entry.isIntersecting) {

    // code to execute when the section becomes visible

    console.log("is visible: " + entry.target.id);
    
    // uncomment to trigger only once per section
    // observer.unobserve(entry.target); 
  }
}, {
  root: document,  // or container element or null
  rootMargin: "0px",
  threshold: 0.1
});

// add each section to the observer

document.querySelectorAll("section").forEach(target => {
  observer.observe(target);
});
section {
  height: 5em;
  margin: 1em;
  margin-bottom: 20em;
  background-color: lightblue;
}
Scroll down the page to trigger the observer

<section id="section1">Section 1</section>
<section id="section2">Sectopm 2</section>
<section id="section3">Section 3</section>
<section id="section4">Section 4</section>
<section id="section5">Section 5</section>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文