近期做H5时遇到让一个按钮可任意拖拽的需求,花了半晚上搞定了,并写了个demo和web端的mousemove等方法不同,移动端是touchstart、touchmove、touchend。
注意以下几点:
1.建议头部加上<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no"> 详细移步:点击打开链接
2.要禁止浏览器默认事件,e.preventDefault(); 加在touchmove事件里。原因是:防止与click冲突,如果这个div有点击事件的话。 内部原理是移动端click事件流
touchstart - touchend - click,所以如果preventDefault写在touchstart中会吧click事件禁掉,写在touchmove中完美解决。详细移步: