js簡(jiǎn)易版滾動(dòng)條實(shí)例(適用于移動(dòng)端H5開(kāi)發(fā))

廢話不多說(shuō),直接上代碼

成都創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供蔚縣企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為蔚縣眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。

<!DOCTYPE html>
<html>
<head>
  <title>滑動(dòng)條</title>
  <meta charset="utf-8">
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
  <script type="text/javascript" src="./hScoll.js"></script>
</head>
<style>
  *{
    margin: 0;
    padding: 0;
  }

  #content{
    margin-top: 50px;
    width:100%;
    height: 200px;
    background: #eeeeee;
    overflow: hidden;
    position: relative;
    /**transform: translate(0px, -70px);*/
  }
  #scoll{
    overflow: hidden;
  }

  #content2{
    margin-top: 50px;
    width:100%;
    height: 200px;
    background: red;
    overflow: hidden;
    position: relative;
    /**transform: translate(0px, -70px);*/
  }
  #scoll2{
    overflow: hidden;
  }

  .scrollbars{
    position: absolute;
    height: 100%;
    right: 0;
    top: 0;
    width: 5px;
    border-radius: 5px;
  }
  .scollb{
    position: absolute;
    right: 0;
    top: 0;
    width: 100%;
    background: #999999;
    border-radius: 5px;
  }
</style>
<body>
  <div id="content">
    <div id="scoll">
      <p>1111</p>
      <p>2222</p>
      <p>3333</p>
      <p>4444</p>
      <p>5555</p>
      <p>6666</p>
      <p>7777</p>
      <p>8888</p>
      <p>9999</p>
      <p>0000</p>
      <p>aaaa</p>
      <p>bbbb</p>
      <p>cccc</p>
      <p>dddd</p>
      <p>eeee</p>
    </div>
  </div>
</body>
<script>
  var options ={
    interactiveScrollbars:true
  }
  window.hScoll.buildScoll('content',options);
</script>
</html>

js代碼:

/**
 * Created by hechao on 2017/6/25.
 */
(function(){

  /**添加window對(duì)象hScoll屬性*/
  window.hScoll = {


    buildScoll:function(el,options){
      App.init(el,options);
    }
  }

  var App = {

    /**初始化組件*/
    init:function(el,option){
      App.options = option;
      App.prevY = 0;
      App.el = document.getElementById(el);
      App.scoll = this.el.children[0];
      App.h = this.el.offsetHeight;//滑動(dòng)范圍高度
      App.ch = this.el.scrollHeight;//內(nèi)容的高度
      if(parseFloat(this.h)<=parseFloat(this.ch)){
        App.sdiv = document.createElement('div');
        App.scollb = document.createElement('div');
        App.sdiv.setAttribute('class','scrollbars');
        App.scollb.setAttribute('class','scollb');
        App.scollb.style.height = parseFloat(this.h)*parseFloat(this.h)/parseFloat(this.ch) + 'px';
        App.el.appendChild(this.sdiv);
        App.sdiv.appendChild(this.scollb);
        App.initevent();
      }
    },

    /**綁定事件*/
    initevent:function (){
      App.el.addEventListener('touchstart', App.touchstart, false);
      App.el.addEventListener('touchmove', App.touchmove, false);
      App.el.addEventListener('touchend', App.touchend, false);
    },

    /**記錄滑動(dòng)初始位置*/
    touchstart:function(e){
      var point = App.getPoint(e);
      App.startY = point.pageY;
    },

    /**手指移動(dòng)時(shí),滾動(dòng)條滾動(dòng)*/
    touchmove:function(e){
      e.preventDefault();//阻止默認(rèn)行為
      var point = App.getPoint(e);
      App.moveY = point.pageY;
      App.deltaY = App.startY - App.moveY;
      if((App.prevY - App.deltaY)<=0 && (App.prevY - App.deltaY)>= -(App.ch-App.h)){
        App.domove(App.prevY - App.deltaY);
      }
      if(App.options.interactiveScrollbars){
        App.domove2(App.prevY - App.deltaY);
      }else{
        if((App.prevY - App.deltaY)<=0 && (App.prevY - App.deltaY)>= -(App.ch-App.h)){
          App.domove2(App.prevY - App.deltaY);
        }
      }
    },

    /**手指離開(kāi)時(shí),判斷位置*/
    touchend:function(e){
      App.prevY = App.prevY - App.deltaY;
      if(App.prevY >= 0){
        App.prevY = 0;
        App.domove(App.prevY,true);
        App.domove2(App.prevY,true);
      }
      if(App.prevY <= -(App.ch-App.h)){
        App.prevY = -(App.ch-App.h);
        App.domove(App.prevY,true);
        App.domove2(App.prevY,true);
      }
    },

    getPoint:function (e) {
      return e.touches ? e.touches[0] : e;
    },

    /**內(nèi)容滑動(dòng)*/
    domove:function (y,t){
      if(t){
        App.scoll.setAttribute('style', 'transform: translate(0px, '+y+'px);transition:transform 300ms ease');
      }else{
        App.scoll.setAttribute('style', 'transform: translate(0px, '+y+'px);transition:transform 0ms ease');
      }
    },

    /**滾動(dòng)條滑動(dòng)*/
    domove2:function(y,t){
      if(t){
        App.scollb.setAttribute('style', 'transform: translate(0px, '+-parseFloat(y)*parseFloat(App.h)/parseFloat(App.ch)+'px);transition:transform 0ms ease;height:'+parseFloat(App.h)*parseFloat(App.h)/parseFloat(App.ch) + 'px'+'');
      }else{
        App.scollb.setAttribute('style', 'transform: translate(0px, '+-parseFloat(y)*parseFloat(App.h)/parseFloat(App.ch)+'px);transition:transform 0ms ease;height:'+parseFloat(App.h)*parseFloat(App.h)/parseFloat(App.ch) + 'px'+'');
      }
    }
  }
})();

以上這篇js 簡(jiǎn)易版滾動(dòng)條實(shí)例(適用于移動(dòng)端H5開(kāi)發(fā))就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前文章:js簡(jiǎn)易版滾動(dòng)條實(shí)例(適用于移動(dòng)端H5開(kāi)發(fā))
地址分享:http://bm7419.com/article8/giheop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶(hù)體驗(yàn)做網(wǎng)站、動(dòng)態(tài)網(wǎng)站建站公司、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作