any-touchjavascript
democss
本次文章先實現內容拖拽和滑動動畫, 後續文章一步一步增長功能, 好比滾動條/ 下拉加載等功能.html
iscroll其實代碼量挺大的(近2100行, 還有另外一個相似的庫betterScroll他的代碼量和iscroll差很少, 由於原理都是同樣的), 閱讀他們的代碼
發現裏面不少邏輯其實都是在作手勢判斷, 好比拖拽(pan), 和劃(swipe), 還有部分元素(表單元素等)須要單獨判斷點擊(tap), 這部分代碼接近1/3, 因此我決定用本身開發的手勢庫(any-touch)實現一個iscroll, 同時配合文字讓你們最終均可以以最少的代碼實現一個iscroll.vue
觀察了一段時間推薦排行, 發現你們都對vue感興趣, 因此本次的"iscroll"將以vue組件的形式實現, 同時我也但願藉助vue強大的抽象能力, 讓最終代碼控制在500行之內, 但願你們喜歡.java
本文先實現拖拽和滑動動畫, 由於這2部分都依賴手勢, 藉此用最少的代碼先實現最核心的功能, 也讓你們對後續的內容有信心.git
添加2個div, 最內的div(子div)經過設置css的transform的translate的值來模擬系統滾動效果.github
<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} 拖拽產生的數據 * @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) ); } } };
你們也發現了, 只有頁面在滾動, 沒有滾動條, 因此下期咱們講如何給scroll-view加上滾動條.web
請留言, 知無不言, 言無不盡. 如以爲本文對您有幫助, 就請給any-touch一個star吧, 謝謝.動畫