javascript - 如果完全在 View 中(大于视口(viewport)),intersectionObserver 获得比率 1

标签 javascript intersection-observer

我有一个页面分为多个部分。现在在滚动时,我尝试使用导航项底部边框的宽度来显示进度( View 中有多少部分)。在下面,您将找到我如何实现它的示例。

现在你可以看到,如果一个部分高于视口(viewport),intersectionRatio 将永远不会达到 1(这反过来不会绘制全宽边框)。我可以使用任何选项或替代方法来实际实现全宽度吗?

(function() {
    var sections = [],
        links = {},
        thresholdSet = [[]],
        observerOptions = {
            root: null,
            rootMargin: "0px",
            threshold: _buildThresholdList()
        };

    function _init() {
        if (!'IntersectionObserver' in window) {
            return;
        }

        sections = document.getElementsByClassName('section');

        for(var i = 0; i < sections.length; i++) {
            var observer = new IntersectionObserver(_intersectionCallback, observerOptions),
                section = sections[i],
                link = document.getElementById('js-' + section.id);

            links[section.id] = {
                node: link,
                initialWidth: link.offsetWidth
            };

            observer.observe(section);
        }
    }

    function _buildThresholdList() {
        for(var i = 0; i <= 1.0; i += 0.01) {
            thresholdSet[0].push(i.toFixed(2));
        }

        console.table(thresholdSet[0]);
        return thresholdSet[0];
    }

    function _intersectionCallback(entries) {
        entries.forEach(function(entry) {
            var section = entry.target;
            var link = links[section.id];
            var width = link.initialWidth * (entry.intersectionRatio || 0) + "px";

            link.node.style.width = width;

            console.log(section.id, entry.intersectionRatio);
        });
    }
    
    _init();
}());
body {
  display: flex;
}

.aside {
  flex: 0 0 150px;
}

.nav {
  position: sticky;
  top: 0;
}

.nav li {
  margin-bottom: 20px;
}

.nav li span {
 display: inline-block;
 padding-bottom: 10px;
 border-bottom: 2px solid red;
}

.sections {
  flex: 1;
}

#page1 {
  height: 350px;
  background-color: #cc8136;
}

#page2 {
  height: 1500px;
  background-color: #6538c4;
}

#page3 {
  height: 1300px;
  background-color: #9e7f9b;
}

#page4 {
  height: 300px;
  background-color: #568574;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div class="aside">
      <ul class="nav">
        <li><span id="js-page1">page1</span></li>
        <li><span id="js-page2">page2</span></li>
        <li><span id="js-page3">page3</span></li>
        <li><span id="js-page4">page4</span></li>
      </ul>
    </div>
    <div class="sections">
      <div class="section" id="page1"></div>
      <div class="section" id="page2"></div>
      <div class="section" id="page3"></div>
      <div class="section" id="page4"></div>
    </div>
</body>
</html>

最佳答案

K 所以而不是使用 entry.intersectionRatio , 我正在使用 entry.intersectionRect.height / viewportHeight .

关于javascript - 如果完全在 View 中(大于视口(viewport)),intersectionObserver 获得比率 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59895216/

相关文章:

javascript - 在 Internet Explorer 中使用 IntersectionObserver polyfill 延迟加载 img 失败

javascript - 如何停止清除顺风组件

javascript - 使用 HTMLElement.click() 方法时防止 JavaScript 冒泡 DOM

javascript - 可以使用交叉观察器在目标的特定偏移顶部计算/触发吗?

javascript - 路口观察者 polyfill 不工作 Safari

javascript - 如何使用 IntersectionObserver 使 React 组件在滚动时淡入,但只有一次?

reactjs - 滚动后如何检查元素是否可见? (跟踪展示次数和点击次数)

javascript - Unity——JavaScript 汽车走错方向

javascript - 试图返回最长的单词但消除了某个字符

javascript - JSON 到 CSV 转换 (JavaScript) : How to properly format CSV conversion