h5滚动时侧滑出现_HTML5滑动(swipe)事件

1 /**2 * @author accountwcx@qq.com3 * http://git.oschina.net/accountwcx/rhui4 *5 * swipe事件,包括swipeLeft、swipeRight、swipeUp、swipeDown。6 * 调用方法7 * Rhui.mobile.swipeLeft(el, callback, options)8 * Rhui.mobile.swipeRight(el, callback, options)9 * Rhui.mobile.swipeUp(el, callback, options)10 * Rhui.mobile.swipeDown(el, callback, options)11 * 如果使用jQuery,调用方法12 * $(el).rhuiSwipe('swipeLeft', callback, options);13 * $(el).rhuiSwipe('swipeRight', callback, options);14 * $(el).rhuiSwipe('swipeUp', callback, options);15 * $(el).rhuiSwipe('swipeDown', callback, options);16 */17 (function(window, $){18 var Rhui = window.Rhui || {};19 window.Rhui = Rhui;20 Rhui.mobile = (function(){21 var touch = {22 distance: 30, //滑动距离,超过该距离触发swipe事件,单位像素。23 duration: 1000 //滑动时长,超过该时间不触发swipe,单位毫秒。24 };25

26 /**27 * 绑定事件28 * @param el 触发事件的元素29 * @param swipe 事件名称,可选值为swipeLeft,swipeRight,swipeUp,swipeDown30 * @param callback 事件回调函数31 * @param isStopPropagation 是否停止冒泡,true为停止冒泡32 * @param isPreventDefault 是否阻止默认事件,true为阻止默认事件33 * @param triggerOnMove swipe事件有两种触发方式,一种是在touchmove过程中,只要满足滑动距离条件即触发。34 * 一种是在touchend中,进入滑动距离判断,如果满足滑动距离触发。35 * 默认是在touchend中触发。36 */37 function bindSwipe(el, swipe, callback, triggerOnMove, isStopPropagation, isPreventDefault){38 var startPoint, endPoint, timer;39

40 /**41 * 计算滑动方向42 * 首先根据x方向和y方向滑动的长度决定触发x方向还是y方向的事件。43 * 然后再判断具体的滑动方向。44 * 如果滑动距离不够长,不判断方向。45 */46 function swipeDirection(x1, y1, x2, y2){47 var diffX = x1 - x2,48 diffY = y1 - y2,49 absX = Math.abs(diffX),50 absY = Math.abs(diffY),51 swipe;52

53 if(absX >= absY){54 if(absX >= touch.distance){55 swipe = diffX > 0 ? 'swipeLeft' : 'swipeRight';56 }57 }else{58 if(absY >= touch.distance){59 swipe = diffY > 0 ? 'swipeUp' : 'swipeDown';60 }61 }62

63 return swipe;64 }65

66 // 清除本次滑动数据67 function clearSwipe(){68 startPoint = undefined;69 endPoint = undefined;70

71 if(timer !== undefined){72 clearTimeout(timer);73 timer = undefined;74 }75 }76

77 /**78 * 判断是否符合条件,如果符合条件就执行swipe事件79 * @param el {HTMLElement} 元素80 * @param event {Event} Touch原始事件81 * @param return 如果执行了事件,就返回true。82 */83 function execSwipe(el, event){84 if(startPoint && endPoint && swipeDirection(startPoint.x, startPoint.y, endPoint.x, endPoint.y) === swipe){85 callback.call(el, event);86 return true;87 }88 }89

90 el.addEventListener('touchstart', function(event){91 var self = this, touchPoint = event.touches[0];92

93 if(isStopPropagation){94 event.stopPropagation();95 }96

97 if(isPreventDefault){98 event.preventDefault();99 }100

101 startPoint = {102 x: Math.floor(touchPoint.clientX),103 y: Math.floor(touchPoint.clientY)104 };105

106 timer = setTimeout(function(){107 //如果超时,清空本次touch数据108 clearSwipe();109 }, touch.duration);110 });111

112 el.addEventListener('touchmove', function(event){113 var self = this, touchPoint = event.touches[0];114

115 if(isStopPropagation){116 event.stopPropagation();117 }118

119 if(isPreventDefault){120 event.preventDefault();121 }122

123 if(startPoint){124 endPoint = {125 x: Math.floor(touchPoint.clientX),126 y: Math.floor(touchPoint.clientY)127 };128

129 //执行swipe事件判断,是否符合触发事件130 if(triggerOnMove){131 if(execSwipe(self, event)){132 clearSwipe();133 }134 }135 }136 });137

138 el.addEventListener('touchend', function(event){139 if(isStopPropagation){140 event.stopPropagation();141 }142

143 if(isPreventDefault){144 event.preventDefault();145 }146

147 execSwipe(self, event);148 //清除本次touch数据149 clearSwipe();150 });151 }152

153 /**154 * @param el {HTMLElement} HTML元素155 * @param callback {Function} 事件回调函数156 * @param options {Object} 可选参数157 * isStopPropagation {Boolean} 是否停止冒泡,true为停止冒泡158 * isPreventDefault {Boolean} 是否阻止默认事件,true为阻止默认事件159 * triggerOnMove {Boolean}160 * swipe事件有两种触发方式,一种是在touchmove过程中,只要满足滑动距离条件即触发。161 * 一种是在touchend中,进入滑动距离判断,如果满足滑动距离触发。162 * 默认值为false,在touchend中触发。163 */164 touch.swipeLeft = function(el, callback, options){165 if(options){166 bindSwipe(el, 'swipeLeft', callback, options.triggerOnMove, options.isStopPropagation, options.isPreventDefault);167 }else{168 bindSwipe(el, 'swipeLeft', callback);169 }170

171 };172

173 touch.swipeRight = function(el, callback, options){174 if(options){175 bindSwipe(el, 'swipeRight', callback, options.triggerOnMove, options.isStopPropagation, options.isPreventDefault);176 }else{177 bindSwipe(el, 'swipeRight', callback);178 }179 };180

181 touch.swipeUp = function(el, callback, options){182 if(options){183 bindSwipe(el, 'swipeUp', callback, options.triggerOnMove, options.isStopPropagation, options.isPreventDefault);184 }else{185 bindSwipe(el, 'swipeUp', callback);186 }187 };188

189 touch.swipeDown = function(el, callback, options){190 if(options){191 bindSwipe(el, 'swipeDown', callback, options.triggerOnMove, options.isStopPropagation, options.isPreventDefault);192 }else{193 bindSwipe(el, 'swipeDown', callback);194 }195 };196

197 return touch;198 })();199

200 // 注册jquery方法201 if($ && $.fn){202 $.fn.extend({203 /**204 * 模拟touch swipe事件,支持链式调用。205 * @param name {String} swipe事件名称,值有swipLeft、swipeRight、swipeUp、swipeDown。206 * @param callback {Function} swipe事件回调函数207 * @param opts {Object} 可选参数208 * isStopPropagation {Boolean} 是否停止冒泡,true为停止冒泡209 * isPreventDefault {Boolean} 是否阻止默认事件,true为阻止默认事件210 * triggerOnMove {Boolean} swipe事件有两种触发方式,一种是在touchmove过程中,只要满足滑动距离条件即触发。211 * 一种是在touchend中,进入滑动距离判断,如果满足滑动距离触发。212 * 默认值为false,在touchend中触发。213 */214 rhuiSwipe: function(name, callback, opts){215 var fnSwipe = Rhui.mobile[name];216

217 if(this.length > 0 && fnSwipe){218 this.each(function(){219 fnSwipe(this, callback, opts);220 });221 }222

223 return this;224 }225 });226 }227 })(window, $);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 Vue H5 滑动删除,可以使用与上面类似的方法,但是需要在 Vue 中添加一些逻辑和事件处理,以下是一个简单的实现步骤: 1. 在 Vue 的组件中使用 `v-for` 指令渲染列表,并且为每个列表项绑定 `touchstart`、`touchmove` 和 `touchend` 事件监听器。 ```html <template> <div class="list"> <div v-for="(item, index) in items" :key="index" class="item" @touchstart="handleTouchStart(index, $event)" @touchmove="handleTouchMove(index, $event)" @touchend="handleTouchEnd(index)"> <div class="left">Delete</div> <div class="content">{{ item }}</div> </div> </div> </template> ``` 2. 在组件的 `data` 选项中添加一个 `swipeIndex` 属性,用于标记当前正在滑动的列表项的索引。 ```javascript export default { data() { return { items: ['Item 1', 'Item 2', 'Item 3', 'Item 4'], swipeIndex: -1 } }, methods: { handleTouchStart(index, event) { // ... }, handleTouchMove(index, event) { // ... }, handleTouchEnd(index) { // ... } } } ``` 3. 在 `handleTouchStart` 方法中记录滑动的起始位置,并将当前列表项的 `swipeIndex` 属性设置为其索引。 ```javascript handleTouchStart(index, event) { this.startX = event.touches[0].pageX; this.startY = event.touches[0].pageY; this.swipeIndex = index; } ``` 4. 在 `handleTouchMove` 方法中计算当前列表项的滑动距离,并根据滑动距离调整左滑区域和内容区域的位置。同,如果用户向右滑动距离超过左滑区域的宽度,将当前列表项标记为“滑动”。 ```javascript handleTouchMove(index, event) { if (index !== this.swipeIndex) { return } var deltaX = event.touches[0].pageX - this.startX; var deltaY = event.touches[0].pageY - this.startY; if (Math.abs(deltaX) < Math.abs(deltaY)) { return } event.preventDefault(); var left = Math.max(-this.leftWidth, Math.min(deltaX, 0)); var content = Math.max(0, Math.min(deltaX + this.leftWidth, this.contentWidth)); this.items[index].left = left; this.items[index].content = content; if (deltaX < -this.leftWidth) { this.items[index].swipe = true; } } ``` 5. 在 `handleTouchEnd` 方法中根据当前列表项的标记判断是否应该删除该项,如果是,则将其从数组中删除,并将 `swipeIndex` 属性重置为 `-1`。 ```javascript handleTouchEnd(index) { if (index !== this.swipeIndex) { return } if (this.items[index].swipe) { this.items.splice(index, 1); } else { this.items[index].left = 0; this.items[index].content = 0; } this.swipeIndex = -1; } ``` 6. 最后,在 CSS 中设置左滑区域和内容区域的样式,并在组件的 `mounted` 钩子函数中计算它们的宽度。 ```css .item { position: relative; overflow: hidden; height: 50px; background-color: #eee; } .left { position: absolute; top: 0; left: 0; width: 100px; height: 50px; background-color: #ccc; transform: translateX(-100px); transition: transform 0.3s ease-out; } .content { position: absolute; top: 0; right: 0; width: 100%; height: 50px; padding: 10px; background-color: #fff; transform: translateX(0); transition: transform 0.3s ease-out; } .item.swipe .left { transform: translateX(0); } .item.swipe .content { transform: translateX(100px); } ``` ```javascript mounted() { var items = this.$el.querySelectorAll('.item') for (var i = 0; i < items.length; i++) { this.items[i] = { left: 0, content: 0, swipe: false } } this.leftWidth = items[0].querySelector('.left').offsetWidth this.contentWidth = items[0].querySelector('.content').offsetWidth for (var i = 0; i < items.length; i++) { items[i].querySelector('.left').style.width = this.leftWidth + 'px' items[i].querySelector('.content').style.width = this.contentWidth + 'px' } } ``` 这样,就可以在 Vue H5 中实现滑动删除功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值