移动端刻度尺

移动端刻度尺

democss

  • 解决滑动后惯性中止选择刻度
  • 兼容ios滚动条消失术

解决滑动后惯性中止选择刻度

若是搜如何监测滚动条是否中止,那前几条答案都是以下代码,一个字都不带改变的:vue

var topValue = 0,// 上次滚动条到顶部的距离  
        interval = null;// 定时器  
    document.onscroll = function() {  
        if(interval == null)// 未发起时,启动定时器,1秒1执行  
            interval = setInterval("test()", 1000);  
        topValue = document.documentElement.scrollTop;  
    }  
  
    function test() {  
        // 判断此刻到顶部的距离是否和1秒前的距离相等  
        if(document.documentElement.scrollTop == topValue) {  
            alert("scroll bar is stopping!");  
            clearInterval(interval);  
            interval = null;  
        }  
    } 
复制代码

这个方法确实能够监测到滚动条是否中止,可是在ios中你会发现,在滑动后惯性阶段明显能够看到抖动,产生的缘由是,我将1s执行一次改为200ms执行一次,那基本是每200ms都在执行一次 setInterval(document.documentElement.scrollTop == topValue一直会是true)。ios

防抖

看到这里你们应该会想到用防抖来解决,
触发高频事件后n秒内函数只会执行一次,若是n秒内高频事件再次被触发,则从新计算时间git

demo:github

function debounce(fn) {
      let timeout = null; // 建立一个标记用来存放定时器的返回值
      return function () {
        clearTimeout(timeout); // 每当用户输入的时候把前一个 setTimeout clear 掉
        timeout = setTimeout(() => { // 而后又建立一个新的 setTimeout, 这样就能保证输入字符后的 interval 间隔内若是还有字符输入的话,就不会执行 fn 函数
          fn.apply(this, arguments);
        }, 500);
      };
    }
    function sayHi() {
      console.log('防抖成功');
    }

    var inp = document.getElementById('inp');
    inp.addEventListener('input', debounce(sayHi)); // 防抖
复制代码

兼容ios滚动条消失术

.ageComponent__calibration::-webkit-scrollbar {
    display: none;
    width: 0 !important;
}
复制代码

css方案只能解决在安卓机的问题,ios上依然会出现,只能经过父元素遮盖滚动条的原理来解决web

ageComponent .OverflowEle {
    width: 100%;
    height: 65px;
    overflow: hidden;
}
.ageComponent__calibration {
    display: flex;
    width: 261px;
    /*width: 100%;*/
    height: 75px;
    overflow-y: hidden;
    overflow-x: scroll;
}
复制代码

源码地址

GitHub
若是对您有用,请给个star~bash

相关文章
相关标签/搜索