五分钟教你使用js原生,实现百度词条楼层联动特效

五分钟教你使用js原生,实现百度词条楼层联动特效

html代码

<div class="box">
    <div class="left">
    </div>
    <div class="right">
    </div>
</div>

css代码

        * {
            margin: 0;
            padding: 0;
        }

        html, body {
            width: 100%;
            height: 100%;
        }

        .box {
            display: flex;
            width: 100%;
            height: 100%;
            justify-content: center;
        }

        .left {
            width: 20%;
            background: #333;
            position: relative;
        }

        .left p {
            height: 40px;
            line-height: 40px;
            padding-left: 60px;
            color: #fff;
        }

        .left p:hover {
            background: #ccc;
            cursor: pointer;
        }
        .left p a {
            display: inline-block;
            width: 100%;
            height: 100%;
            color:#fff;
        }
        .left span {
            display: inline-block;
            position: absolute;
            top: 15px;
            left: 20px;
            background: blue;
            height: 10px;
            width: 30px;
            transition: top 0.2s ease;
        }

        .right {
            width: 80%;
            background: #ccc;
            overflow: auto;
            transition: all 0.2s ease;
        }

        .right p {
            text-indent: 2em;
            margin: 15px 0
        }
        .other {
            height:1200px;
        }

js代码

 // 渲染左侧条目
    let leftContent = '';
    for (var i = 0; i < 10; i++) {
        leftContent += `<p name="${i}"><a href="#content_${i}">条目${i}</a></p>`;
    }
    leftContent += '<span></span>';
    document.getElementsByClassName('left')[0].innerHTML = leftContent;
    // 渲染右侧内容
    let rightContent = '<div class="other"></div>';
    for (let i = 0; i < 10; i++) {
        rightContent += `<p id="content_${i}" >`;
        for (let j = 0; j < 100; j++) {
            rightContent += `第${i}段落`;
        }
        rightContent += '</p>';
    }
    rightContent += '<div class="other"></div>';
    document.getElementsByClassName('right')[0].innerHTML = rightContent;
    // 内容列表个数
    let contentListLength = document.getElementsByClassName('right')[0].getElementsByTagName('p').length;
    // 内容的偏移量数组
    let contentListArr = [];
    for (let i = 0; i < contentListLength; i++) {
        contentListArr.push(document.getElementById(`content_${i}`).offsetTop)
    }
    // 滚动内容的索引
    let contentScrollIndex = 0;
    // 先移除滚动监听
    document.getElementsByClassName('right')[0].removeEventListener('scroll', function () {
    });
    // 添加滚动监听
    document.getElementsByClassName('right')[0].addEventListener('scroll', function (ev) {
        // 内容滚动距离
        let contentScrollTop = document.getElementsByClassName('right')[0].scrollTop;
        console.log('滚动距离--' + contentScrollTop);
        console.log('数组--' + contentListArr);
        for (let i = 0; i < contentListArr.length; i++) {
            let item = contentListArr[i];
            if (contentScrollTop >= item) {
                contentScrollIndex = i;
                // 左侧箭头的top值
                document.getElementsByClassName('left')[0].getElementsByTagName('span')[0].style.top = ((contentScrollIndex * 40) + 15) + 'px';
            } else {
                console.log('索引值--' + i);
                break;
            }
        }
    });

评论

暂无评论