js实现自定义滚动条

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>自定义滚动条</title>
    <style type="text/css">
        *{
            padding: 0;
            margin: 0;
        }
        #wrap{
            width: 300px;
            height: 200px;
            overflow: hidden;
            border: 1px slid olive;
            padding-right: 15px;
            padding-bottom: 15px;
            margin: 100px auto;
            position: relative;
        }

        img{position: absolute; width: 730px;}

        #scroll-x{width: 315px;height: 15px;background: grey;position: absolute;left: 0;bottom: 0;}
        #scroll-y{width: 15px;height: 215px;background: grey;position: absolute;right: 0;top: 0;}

        #tag-x{width: 15px;height: 15px;background: orange;position: absolute;bottom: 0;left: 0;}
        #tag-y{width: 15px;height: 15px;background: orange;position: absolute;top: 0;right: 0;}

    </style>
</head>
<body>
    <div id="wrap">
        <img id="img" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1493991722334&di=39eead9f568bc2d45ad858d4f7fa3922&imgtype=0&src=http%3A%2F%2Fimg.tuku.cn%2Ffile_big%2F201403%2F5683a05250db40b983f24a4e52f630f5.jpg"/>
        <div id="scroll-x">
            <div id="tag-x"></div>
        </div>
        <div id="scroll-y">
            <div id="tag-y"></div>
        </div>
    </div>
</body>
<script type="text/javascript">
    var oWrap = document.getElementById('wrap');
    var oImg = document.getElementById('img');
    var oScrollX = document.getElementById('scroll-x');
    var oScrollY = document.getElementById('scroll-y');
    var oTagX = document.getElementById('tag-x');
    var oTagY = document.getElementById('tag-y');


    //左右
    //鼠标滚轮滚动
    if(oWrap.addEventListener){
        oWrap.addEventListener('mousewheel',oImgScroll,false);
        oWrap.addEventListener('DOMMouseScroll',oImgScroll,false);
    }else{
        oWrap.attachEvent('onmousewheel',oImgScroll,false);
    }

    function oImgScroll(e){
        e = e ||event;
        var speed = 10;
        var isUp;
        if(e.wheelDelta){ //IE Chrome
            isUp = e.wheelDelta > 0 ? true : false;
        } else{ //FF
            isUp = e.dedail < 0 ? true : false;
        }

        if(isUp){
            speed = -speed;
        }

        var left = oTagX.offsetLeft + speed;
        if(left < 0){
                left = 0;
            } else if(left > oScrollX.offsetWidth - 15 - oTagX.offsetWidth){
                left = oScrollX.offsetWidth - 15 - oTagX.offsetWidth;
            }
            oTagX.style.left = left + 'px';
            var scale = left/(oScrollX.offsetWidth - oTagX.offsetWidth-15);
            var nImgLeft = (oImg.offsetWidth - oWrap.offsetWidth-15+2)*scale;
            oImg.style.left = -nImgLeft + 'px';
    }

    //点击滚动
    oTagX.onmousedown = function(e){
        e = e ||event;
        var disX = e.clientX - oTagX.offsetLeft;
        document.onmousemove = function(e){
            e = e ||event;
            var left = e.clientX - disX;
            if(left < 0){
                left = 0;
            } else if(left > oScrollX.offsetWidth - 15 - oTagX.offsetWidth){
                left = oScrollX.offsetWidth - 15 - oTagX.offsetWidth;
            }
            oTagX.style.left = left + 'px';
            var scale = left/(oScrollX.offsetWidth - oTagX.offsetWidth-15);
            var nImgLeft = (oImg.offsetWidth - oWrap.offsetWidth-15+2)*scale;
            oImg.style.left = -nImgLeft + 'px';
            return false;//去除默认样式
        }
        document.onmouseup = function(){
            document.onmousemove = null;
            document.onmouseup = null;
        }
        if(e.preventDefault){
            e.preventDefault();
        }
    };

    //上下点击滚动
    oTagY.onmousedown = function(e){
        var e = e ||event;
        var disY = e.clientY - oTagY.offsetTop;
        oTagY.onmousemove = function(e){
            var e = e ||event;
            var top = e.clientY - disY;
            if (top < 0){
                top = 0;
            } else if(top > oScrollY.offsetHeight - oTagY.offsetHeight-15){
                top = oScrollY.offsetHeight - oTagY.offsetHeight -15;
            }
            oTagY.style.top = top + 'px';
            var scale = top/(oScrollY.offsetHeight - oTagY.offsetHeight-15);
            var oImgTop = scale *(oImg.offsetHeight - oWrap.offsetHeight-15+2);
            oImg.style.top = -oImgTop + 'px';
            return false;
        }
        oTagY.onmouseup = function(){
            document.onmousemove = null;
            document.onmouseup = null;
        }
        if(e.preventDefault){
            e.preventDefault();
        }
    }

</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,UniApp 中可以使用 `scroll-view` 组件来实现自定义滚动条。以下是一个简单的示例: ```html <scroll-view class="scroll-view" scroll-y> <div class="content"> <!-- 列表内容 --> </div> <div class="scroll-bar"></div> </scroll-view> ``` 其中,`.scroll-view` 为 `scroll-view` 组件的样式类,`.content` 为列表内容的容器,`.scroll-bar` 为自定义滚动条的容器。 接着,在样式文件中可以添加以下代码: ```css .scroll-view { position: relative; /* 父容器需要设置 position: relative; */ height: 100vh; /* 设置高度,这里使用了 viewport height 单位 */ } .content { height: 2000rpx; /* 列表内容高度 */ } .scroll-bar { position: absolute; top: 0; right: 0; width: 6px; height: 50px; /* 滚动条高度 */ background-color: #ccc; border-radius: 3px; opacity: 0; /* 初始透明度为 0,只有在滚动时才显示 */ transition: opacity 0.3s; } .scroll-view::-webkit-scrollbar { width: 0; /* 隐藏原生滚动条 */ } .scroll-view::-webkit-scrollbar-thumb { width: 0; height: 0; background-color: transparent; } ``` 这里使用了 `position: absolute;` 和 `right: 0;` 将自定义滚动条定位到父容器的右侧,使用 `opacity` 和 `transition` 实现滚动条的渐隐渐现效果。同时,使用了 `-webkit-scrollbar` 相关样式将原生滚动条隐藏。 最后,在 `scroll-view` 组件的 `scroll` 事件中可以添加以下代码: ```js onScroll(e) { const { scrollTop, scrollHeight, clientHeight } = e.detail; const bar = this.$refs.bar; // 计算滚动条高度和位置 const barHeight = clientHeight / scrollHeight * clientHeight; const barTop = scrollTop / scrollHeight * clientHeight; // 设置滚动条位置和高度 bar.style.height = barHeight + 'px'; bar.style.top = barTop + 'px'; // 显示滚动条 bar.style.opacity = 1; // 3 秒后隐藏滚动条 clearTimeout(this.timer); this.timer = setTimeout(() => { bar.style.opacity = 0; }, 3000); } ``` 在 `scroll` 事件中,通过 `scrollTop`、`scrollHeight` 和 `clientHeight` 计算出滚动条的高度和位置,并设置到滚动条容器的样式中。同时,通过设置 `opacity` 实现滚动条的渐隐渐现效果。这里还设置了一个 3 秒的计时器,在滚动结束后 3 秒自动隐藏滚动条。 希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值