自定义滚动条

使用原生JS写的自定义滚动条
在这里插入图片描述

css代码部分

  <style>
    * {
   
      margin:0;
      padding:0;
    }
    #box {
   
      width:500px;
      height:600px;
      border:1px solid #000;
      margin:100px auto 0;
      position: relative;
      overflow: hidden;
    }
    #box .content {
   
      width:480px;
      padding-left:5px;
      box-sizing: border-box;
      position:absolute;
    }
    #box .scroll {
   
      width:20px;
      height:600px;
      border:1px solid rgb(216, 35, 35);
      box-sizing: border-box;
      position:absolute;
      top:0;
      right:0;
    }
    #box .scroll a {
   
      display:block;
      width:20px;
      height:25px;
      text-decoration: none;
      background-color: #ccc;
    }
    #box .scroll .oBar {
   
      width:20px;
      height:550px;
      position: relative;
    }
    /* 要记得给滑块设置position属性,滑块才可以移动 */
    #box .scroll .oBar .slider {
   
      width:20px;
      height:50px;
      background-color: rgb(154, 212, 205);
      position:absolute;
    }
  </style>

js部分
此部分要特别注意滚动的文本内容高度和滚动条滚动的距离成比例。
步骤:
1.slider拖拽,滑块在oBar中滑动
2.点击👆,👇键,滑块会滚动,文本也会随之变化————结合间歇定时器实现运动
3.在oBox区域触发鼠标滚轮事件,文本跟滚动条一起滚动

<script>
    window.onload = function(){
   
      var oBox = document.getElementById('box');
      var oCon = oBox.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值