1. 程式人生 > 程式設計 >vue移動端實現手指滑動效果

vue移動端實現手指滑動效果

本文例項為大家分享了移動端實現手指滑動效果的具體程式碼,供大家參考,具體內容如下

vue移動端實現手指滑動效果

滑動時候黃色塊寬度跟著變化
通過touch點選實現
目前感覺寬度變化有點問題,還在思考中

下面上程式碼:

<template lang="html">
  <div class="back" ref="back" @touchstart.prevent="touchStart" @touchmove.prevent="touchMove">
    <div class="back-r" 
 @touchstart.prevent="touchStart" @touchuaJQc
move.prevent="touchMove" @touchend="touchEnd" ref="right"></div> </div> </template> <script> export default { data() { },created() { this.touch = {} },methods: { touchStart(e) { uaJQc const touch = e.touches[0] //點選時的位子 this.touch.startX = touch.pageX this.touch.startY = touch.pageY console.log('----',this.$refs.right.clientWidth) },//開始點選 touchMove(e) { console.log("move"); const touch = e.touches[0] //滑動位置和初始位置差 const deltaX = touch.pageX - this.touch.startX console.log(deltaX) const deltaY = touch.pageY - this.touch.startY; //列印right寬度 consolehttp://www.cppcns.com
.log('----',this.$refs.right.clientWidth+'px') const rwidth = this.$refs.right.clientWidth //改變right的寬度 this.$refs.right.style.width = (this.$refs.right.clientWidth + Math.floor(deltaX/50)) +'px' //進行判斷,當寬度小於0或者大於400 if (rwidth<0) { this.$refs.right.style.width = 0 } else if(rwidth>400){ this.$refs.right.style.width = 400+'px' } console.log('----',this.$
refs.right.clientWidth+'px') },touchEnd() { console.log("end"); // console.log(this.percent); } uaJQc } } </script> <style scoped lang="stylus" rel="stylesheet/stylus"> body{ margin:0; padding: 0; overflow: hidden; touch-action:none; } .back{ width: 100% height: 100px border 10px solid #0000FF overflow: hidden; } .back-r{ // display: inline-block // vertical-align: top position: relative width: 400px height: 100% overflow: hidden; background-color: yellow } </style>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。