js实现水平和竖直滑动条

2022-04-15 0 792

最近在做练手项目时候,需要用到滑动条,所以就稍微研究了一下。

首先来看水平滑动条,效果图如下:

js实现水平和竖直滑动条

代码如下:

<html>
 <head>
  <meta charset="UTF-8">
  <title>水平滑动条</title>
  <style>
   * {
    margin: 0;
    padding: 0;
   }
 
   .scroll {
    margin: 100px;
    width: 500px;
    height: 5px;
    background: #ccc;
    position: relative;
   }
 
   .bar {
    width: 10px;
    height: 20px;
    background: #369;
    position: absolute;
    top: -7px;
    left: 0;
    cursor: pointer;
   }
   p{
    margin-left: 100px;
   }
  </style>
 </head>
 <body>
  <div class="scroll" id="scroll">
   <div class="bar" id="bar">
   </div>
  </div>
  <p></p>
  <script>
   //获取元素
   var scroll = document.getElementById('scroll');
   var bar = document.getElementById('bar');
   var ptxt = document.getElementsByTagName('p')[0];
   bar.onmousedown = function(event) {
    var event = event || window.event;
    //页面事件的X减去当前相对于最近的祖先定位元素
    var x = event.clientX - this.offsetLeft;
    document.onmousemove = function(event) {
     var event = event || window.event;
     var left = event.clientX - x;
     if (left < 0)
      left = 0;
     else if (left > scroll.offsetWidth - bar.offsetWidth) {
      left = scroll.offsetWidth - bar.offsetWidth;
     }
     //改变滑块的left
     bar.style.left = left + "px";
     ptxt.innerHTML = "当前滑块的移动的百分比:" + parseInt(left / (scroll.offsetWidth - bar.offsetWidth) * 100) + "%";
     //防止选择内容
     window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
    }
 
   }
   //当鼠标弹起的时候,不做任何操作
   document.onmouseup = function() {
    document.onmousemove = null;
   }
  </script>
 </body>
</html>

竖直滑动条效果图如下:

js实现水平和竖直滑动条

代码如下:

<html>
 <head>
  <meta charset="UTF-8">
  <title>竖直滑动条</title>
  <style>
   * {
    margin: 0;
    padding: 0;
   }
   .scroll{
    margin: 100px;
    width: 5px;
    height: 320px;
    background: #ccc;
    position: relative;
   }
 
   .bar {
    width: 15px;
    height: 5px;
    background: #369;
    position: absolute;
    top: 0px;
    left: -5;
    cursor: pointer;
   }
   p{
    margin-left: 100px;
   }
  </style>
 </head>
 <body>
  <div class="scroll" id="scroll">
   <div class="bar" id="bar">
   </div>
  </div>
  <p></p>
  <script>
   //获取元素
   var scroll = document.getElementById("scroll");
   var bar = document.getElementById("bar");
   var ptxt = document.getElementsByTagName('p')[0];
   //添加事件
   bar.onmousedown = function(event) {
    var event = event || window.event;
    //页面事件的Y减去当前相对于最近的祖先定位元素
    var y = event.clientY - this.offsetTop;
    // 拖动需要写到down里面
    document.onmousemove = function(event) {
     var event = event || window.event;
     //获取移动的距离
     var top = event.clientY - y;
     if (top < 0){
      top = 0;
     }
     else if (top > scroll.offsetHeight - bar.offsetHeight){
      top = scroll.offsetHeight - bar.offsetHeight;
     }
     //改变滑块的top
     bar.style.top = top + "px";
     //按照百分比得到当前滑动的距离
     ptxt.innerHTML = "当前滑块的移动的百分比:" + parseInt(top/(scroll.offsetHeight - bar.offsetHeight) * 100) + "%";
     //防止选择内容
     window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
    }
   }
   //当鼠标弹起的时候,不做任何操作
   document.onmouseup = function() {
    document.onmousemove = null; 
   }
   
  </script>
 </body>
</html>

这里之所以加入移动百分比的展示效果,主要是考虑到后续如果需要对接后台的数据就可以达到动态控制的目的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持NICE源码。

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript js实现水平和竖直滑动条 https://www.niceym.com/30692.html