当内容绝对放置在屏幕外时,如何检查多个DIV

发布于 2025-01-21 06:27:18 字数 1638 浏览 0 评论 0 原文

我试图通过检查项目是否在视口内的滑块来添加样式,这效果很好,直到我使用left:-500px将parent parent parent parent parent parent parent parter容器的定位;例如。

这是我最初试图使用的代码。

document.addEventListener("DOMContentLoaded", function() {
var customSlides = document.getElementsByClassName("slide");
function checkForScroll(){
    var customVisibleFraction = 0.75;
    for(var i = 0; i < customSlides.length; i++) {
      var customSlide = customSlides[i];
      var csX = customSlide.offsetLeft, csY = customSlide.offsetTop, csW = customSlide.offsetWidth, csH = customSlide.offsetHeight, csR = csX + csW, //right
          csB = csY + csH, //bottom
          visibleX, visibleY, visible;

      visibleX = Math.max(0, Math.min(csW, window.pageXOffset + window.innerWidth - csX, csR - window.pageXOffset));
      visibleY = Math.max(0, Math.min(csH, window.pageYOffset + window.innerHeight - csY, csB - window.pageYOffset));
      visible = visibleX * visibleY / (csW * csH);
        if (visible > customVisibleFraction) {
            customSlide.style.opacity = '1';
        } else {
            customSlide.style.opacity = '0.5';
        }
    }
}
window.addEventListener('scroll', checkForScroll, false);
window.addEventListener('resize', checkForScroll, false);
});

最初,这起作用很棒,左侧和右侧divs在从寡妇中滚出时都具有不透明的变化,但是一旦我绝对位置所有这些div元素的容器,脚本的所有这些div元素的容器都会停止工作,如以下codepen中所示:

https://codepen.io/digitaldesigner/pen/pen/pen/bgamrrg

“ https://codepen.io/digitaldesigner/pen/bgamrrg 我试图与使用这种绝对定位的滑块,因此正在寻找一种使这项工作的方法。

在使用-LEFT绝对定位时,如何使此脚本正常运行? 我想坚持使用JavaScript,并避免使用jQuery。

I am trying to add styling to a slider by checking if the item is within the viewport, this works great until I absolute position the items parent container offscreen using left:-500px; for example.

Here is the code I initially was trying to use.

document.addEventListener("DOMContentLoaded", function() {
var customSlides = document.getElementsByClassName("slide");
function checkForScroll(){
    var customVisibleFraction = 0.75;
    for(var i = 0; i < customSlides.length; i++) {
      var customSlide = customSlides[i];
      var csX = customSlide.offsetLeft, csY = customSlide.offsetTop, csW = customSlide.offsetWidth, csH = customSlide.offsetHeight, csR = csX + csW, //right
          csB = csY + csH, //bottom
          visibleX, visibleY, visible;

      visibleX = Math.max(0, Math.min(csW, window.pageXOffset + window.innerWidth - csX, csR - window.pageXOffset));
      visibleY = Math.max(0, Math.min(csH, window.pageYOffset + window.innerHeight - csY, csB - window.pageYOffset));
      visible = visibleX * visibleY / (csW * csH);
        if (visible > customVisibleFraction) {
            customSlide.style.opacity = '1';
        } else {
            customSlide.style.opacity = '0.5';
        }
    }
}
window.addEventListener('scroll', checkForScroll, false);
window.addEventListener('resize', checkForScroll, false);
});

This works great initially and both left and right side divs have the opacity change as they scroll out of the widow however once I absolute position the container of all these div elements left the script stops working as seen in the following codepen:

https://codepen.io/DigitalDesigner/pen/bGamRRG

If I remove the absolute positioning all works as expected however the slider I am trying to integrate with uses this absolute positioning so looking for a way to make this work.

How can I get this script to function correctly when using -left absolute positioning?
I would like to stick with javascript and avoid jQuery as well.

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

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

发布评论

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

评论(1

野生奥特曼 2025-01-28 06:27:18

正确的方法是使用

var rect = element.getBoundingClientRect();
console.log(rect.top, rect.right, rect.bottom, rect.left);

替换为:

function checkForScroll(){
  for(var i = 0; i < customSlides.length; i++) {
    const customSlide = customSlides[i];
    const customSlidePos = customSlide.getBoundingClientRect();
    if (customSlidePos.x > 0 && customSlidePos.y > 0 && customSlidePos.x < window.innerWidth && customSlidePos.y < window.innerHeight) {
        customSlide.style.opacity = '1';
    } else {
        customSlide.style.opacity = '0.5';
    }
  }
}

The correct approach is to use element.getBoundingClientRect():

var rect = element.getBoundingClientRect();
console.log(rect.top, rect.right, rect.bottom, rect.left);

Replace with this:

function checkForScroll(){
  for(var i = 0; i < customSlides.length; i++) {
    const customSlide = customSlides[i];
    const customSlidePos = customSlide.getBoundingClientRect();
    if (customSlidePos.x > 0 && customSlidePos.y > 0 && customSlidePos.x < window.innerWidth && customSlidePos.y < window.innerHeight) {
        customSlide.style.opacity = '1';
    } else {
        customSlide.style.opacity = '0.5';
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文