国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vue + any-touch實現(xiàn)一個iscroll 實現(xiàn)拖拽和滑動動畫效果

2019-11-19 11:51:56
字體:
來源:轉載
供稿:網友

https://github.com/383514580/any-touch

先看demo

demo

說點濕的

 

iscroll其實代碼量挺大的(近2100行, 還有另一個類似的庫 betterScroll 他的代碼量和iscroll差不多, 因為原理都是一樣的), 閱讀他們的代碼

發(fā)現(xiàn)里面很多邏輯 其實都是在做手勢判斷 , 比如拖拽(pan), 和劃(swipe), 還有部分元素(表單元素等)需要單獨判斷點擊(tap), 這部分代碼接近1/3, 所以我決定用自己開發(fā)的手勢庫(any-touch)實現(xiàn)一個iscroll, 同時配合文字讓大家 最終都可以以最少的代碼實現(xiàn)一個iscroll .

vue

觀察了一段時間推薦排行, 發(fā)現(xiàn)大家都對 vue 感興趣, 所以本次的"iscroll"將以vue組件的形式實現(xiàn), 同時我也希望借助vue強大的抽象能力, 讓最終代碼控制在500行以內 , 希望大家喜歡.

本文是個系列文章

本文先實現(xiàn)拖拽和滑動動畫, 因為這2部分都依賴 手勢 , 借此用最少的代碼先實現(xiàn)最核心的功能, 也讓大家對后續(xù)的內容有信心.

簡單說下iscroll原理

添加2個div, 最內的div(子div)通過設置css的transform的translate的值來模擬系統(tǒng)滾動效果.

說完邏輯再說代碼

拖拽的時候通過panstart/panmove手勢返回的 位移增量 (deltaX/Y)進行位置變化, 同時關閉動畫效果.
發(fā)生快速劃(swipe)的時候, 開啟動畫, 同時通過計算 目標位置 和 動畫時間 來觸發(fā)滑動動畫.
代碼

<div class="any-scroll-view">  <div ref="body" :style="bodyStyle" class="any-scroll-view__body"><slot></slot></div></div>.any-scroll-view {  position: relative;  width: 100%;  height: 90vh;   overflow: hidden;  &__body {    transition-timing-function: cubic-bezier(0.1, 0.57, 0.1, 1);    background: #eee;    position: absolute;    width: 100%;    height: 100%;  }}import AnyTouch from 'any-touch';export default {  name: 'any-scroll-view',  props: {    // 減速度, 單位px/s²    acceleration: {      type: Number,      default: 3600    }  },  data() {    return {      scrollTop: 0,      scrollLeft: 0,      transitionDuration: 300    };  },  computed: {    bodyStyle() {      return {        transitionDuration: `${this.transitionDuration}ms`,        transform: `translate(${this.scrollLeft}px, ${          this.scrollTop        }px)`      };    }  },  mounted() {    const at = new AnyTouch(this.$el);    // 第一次觸碰    at.on('inputstart', (ev) => {      this.stopRoll();    });    // 拖拽開始    at.on('panstart', (ev) => {      this.move(ev);    });    // 拖拽中    at.on('panmove', (ev) => {      this.move(ev);    });    // 快速滑動    at.on('swipe', (ev) => {      this.decelerate(ev);    });    this.$on('hook:destroy', () => {      at.destroy();    });  },  methods: {    // https://github.com/nolimits4web/swiper/blob/master/dist/js/swiper.esm.js#L87    // https://github.com/nolimits4web/Swiper/blob/master/src/utils/utils.js#L25    getCurrentTranslate() {      const style = getComputedStyle(this.$refs.body, null);      const { transform } = style;      const array = transform.match(/(/-?)(/d)+(/./d{0,})?/g);      return { x: Math.round(array[4]), y: Math.round(array[5]) };    },    stopRoll() {      const { x, y } = this.getCurrentTranslate();      this.moveTo({ scrollTop: y, scrollLeft: x });    },    /**     * 移動body     * @param {Object} 拖拽產生的數(shù)據     * @param {Number} deltaX: x軸位移變化     * @param {Number} deltaY: y軸位移變化     */    move({ deltaX, deltaY }, transitionDuration = 0) {      this.transitionDuration = transitionDuration;      this.scrollLeft += deltaX;      this.scrollTop += deltaY;    },    /**     * 移動到     */    moveTo({ scrollTop, scrollLeft }, transitionDuration = 0) {      this.transitionDuration = transitionDuration;      this.scrollLeft = scrollLeft;      this.scrollTop = scrollTop;    },    /**     * 拖拽松手后減速移動至停止     * velocityX/Y的單位是px/ms     */    decelerate(ev) {      const directionSign = { up: -1, right: 1, down: 1, left: -1 }[        ev.direction      ];      // Top? | Left?      let SCROLL_SUFFIX = 'Top';      // x ? | y?      let AXIS_SUFFIX = 'Y';      if (ev.velocityX > ev.velocityY) {        SCROLL_SUFFIX = 'Left';        AXIS_SUFFIX = 'X';      }      // 減速時間, 單位ms      // t = (v₂ - v₁) / a      const velocity = ev[`velocity${AXIS_SUFFIX}`];      this.transitionDuration = Math.round(        ((velocity * 1000) / this.acceleration) * 1000      );      // 滑動距離      // s = (v₂² - v₁²) / (2 * a)      const scrollAxis = `scroll${SCROLL_SUFFIX}`;      this[scrollAxis] +=        directionSign *        Math.round(          Math.pow(velocity * 1000, 2) / (2 * this.acceleration)        );    }  }};

總結

以上所述是小編給大家介紹的vue + any-touch實現(xiàn)一個iscroll 實現(xiàn)拖拽和滑動動畫效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 会理县| 陈巴尔虎旗| 长治县| 南雄市| 巢湖市| 华阴市| 临潭县| 石河子市| 兴仁县| 原阳县| 内丘县| 吉安市| 明溪县| 朔州市| 南皮县| 浦北县| 锡林郭勒盟| 民县| 大港区| 南木林县| 荥阳市| 红河县| 锡林浩特市| 安塞县| 屏南县| 宣武区| 华安县| 德安县| 宜黄县| 中阳县| 徐水县| 彭山县| 南丰县| 平山县| 临武县| 同仁县| 天峻县| 宁武县| 大城县| 尼木县| 麦盖提县|