当前位置:首页 > JavaScript

js实现滚动条

2026-04-04 01:21:01JavaScript

使用原生JavaScript实现滚动条

通过监听滚动事件获取滚动位置

window.addEventListener('scroll', function() {
    let scrollPosition = window.scrollY;
    console.log('当前滚动位置:', scrollPosition);
});

平滑滚动到指定位置

使用scrollTo方法实现平滑滚动效果

js实现滚动条

function smoothScrollTo(targetPosition, duration = 1000) {
    const startPosition = window.scrollY;
    const distance = targetPosition - startPosition;
    let startTime = null;

    function animation(currentTime) {
        if (!startTime) startTime = currentTime;
        const timeElapsed = currentTime - startTime;
        const run = easeInOutQuad(timeElapsed, startPosition, distance, duration);
        window.scrollTo(0, run);
        if (timeElapsed < duration) requestAnimationFrame(animation);
    }

    function easeInOutQuad(t, b, c, d) {
        t /= d/2;
        if (t < 1) return c/2*t*t + b;
        t--;
        return -c/2 * (t*(t-2) - 1) + b;
    }

    requestAnimationFrame(animation);
}

自定义滚动条样式

通过CSS隐藏默认滚动条并创建自定义样式

body {
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE/Edge */
}

body::-webkit-scrollbar {
    display: none; /* Chrome/Safari */
}

.custom-scrollbar {
    position: fixed;
    right: 0;
    top: 0;
    width: 10px;
    height: 100vh;
    background: #f1f1f1;
}

.scroll-thumb {
    width: 100%;
    background: #888;
    border-radius: 5px;
}

同步自定义滚动条

将自定义滚动条与实际滚动位置同步

js实现滚动条

const scrollThumb = document.querySelector('.scroll-thumb');
const scrollableHeight = document.documentElement.scrollHeight - window.innerHeight;

window.addEventListener('scroll', function() {
    const scrollPercentage = (window.scrollY / scrollableHeight) * 100;
    const thumbHeight = (window.innerHeight / document.documentElement.scrollHeight) * 100;

    scrollThumb.style.height = `${thumbHeight}%`;
    scrollThumb.style.transform = `translateY(${scrollPercentage}%)`;
});

滚动到元素位置

实现滚动到页面特定元素的功能

function scrollToElement(elementId) {
    const element = document.getElementById(elementId);
    if (element) {
        const elementPosition = element.getBoundingClientRect().top + window.scrollY;
        smoothScrollTo(elementPosition - 20); // 减去20px作为顶部边距
    }
}

滚动事件节流优化

使用节流函数优化滚动事件性能

function throttle(func, limit) {
    let lastFunc;
    let lastRan;
    return function() {
        const context = this;
        const args = arguments;
        if (!lastRan) {
            func.apply(context, args);
            lastRan = Date.now();
        } else {
            clearTimeout(lastFunc);
            lastFunc = setTimeout(function() {
                if ((Date.now() - lastRan) >= limit) {
                    func.apply(context, args);
                    lastRan = Date.now();
                }
            }, limit - (Date.now() - lastRan));
        }
    };
}

window.addEventListener('scroll', throttle(function() {
    // 滚动处理逻辑
}, 100));

标签: 滚动条js
分享给朋友:

相关文章

js实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.j…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

js实现换肤

js实现换肤

使用CSS变量实现换肤 通过CSS变量可以轻松实现主题切换功能。CSS变量在根元素中定义,通过JavaScript动态修改这些变量值。 :root { --primary-color: #349…

js实现文字滚动

js实现文字滚动

实现文字滚动的几种方法 使用CSS动画实现滚动 通过CSS的@keyframes和transform属性可以实现平滑的文字滚动效果。 <style> .scroll-text { w…

js实现上传文件

js实现上传文件

文件上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现文件上传功能。 <input type="file"…

js计算器的实现

js计算器的实现

实现基础结构 HTML部分需要创建计算器的基本框架,包含显示区域和按钮。使用<div>容器包裹整个计算器,内部设置一个显示屏和多个按钮。 <div class="calculato…