H5移动端常用事件,此中大有文章

简介: 笔记

1. 移动端事件


  • 首先,移动端不建议使用 click 事件
  • 因为它有300毫秒的延迟,对用户体验不太友好

常用事件:touchstart(),touchend(),touchmove()

HTML文件

<!DOCTYPE html>
<html lang="ZH-cn">
    <head>
        <meta charset="utf-8">
        <title>标题</title>
        <meta name="viewport" content = "width=device-width, initial-scale=1.0, user-scalable=no"/>
    </head>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html,body{
            height: 100%;
        }
        .con{
            height: 300px;
            width: 200px;
            color: white;
            background: green;
        }
    </style>
    <body>
        <h1>移动端事件</h1>
        <button 
            class="touchstart"
            id="touchstart"
        >
                点击我
        </button>
        <div class="con" id="con">
        </div>
    </body>
</html>

JS部分

function $my(id){
    return document.getElementById(id);
}
let btn = $my("touchstart");
let con = $my("con");
// 一样的会触发,但不建议使用
// btn.addEventListener("click", function(){
//     console.log("click");
// })
// 手指触摸事件
btn.addEventListener("touchstart", function(){
    console.log("touchstart");
})
// 手指松开事件
btn.addEventListener("touchend", function(){
    console.log("touchend");
})
// 手指移动事件 + 事件节流
let time = null;
con.addEventListener("touchmove", function(){
    if(!time){
        time = setTimeout(()=>{
            console.log("快乐的飞起");
            time = null;
        },1000)
    }
})

大家可以先试一下效果,复制代码即可大家可以先试一下效果,复制代码即可1.png


2. 封装移动端自定义事件

  • 众所周知:低版本安卓手机上是没有touch类事件的
  • 所以我们只能自己封装

2.1 封装移动端单击与长按事件

(function(window){
    // 对外开放的接口
    function myMobile(selector){
        return myMobile.prototype._init(selector);
    }
    myMobile.prototype = {
        _init: function(selector){
            if(typeof selector === "string"){
                this.ele = window.document.querySelector(selector);
                // this:原型对象
                return this;
            }
        },
        // 单击事件
        tap: function(hanler){
            this.ele.addEventListener("touchstart", tochFn);
            this.ele.addEventListener("touchend", tochFn);
            let startTime,
                endTime;
            function tochFn(e){
                // e.preventDefault();
                switch (e.type){
                    case "touchstart":
                        startTime = new Date().getTime();
                        break;
                    case "touchend":
                        endTime =  new Date().getTime();
                        // 200是一个阙值
                        if( endTime - startTime < 200){
                            hanler.call(this, e);
                        }
                    break;
                }
            }
        },
        // 长按事件
        longtap: function(hanler){
            this.ele.addEventListener("touchstart", tochFn);
            this.ele.addEventListener("touchmove", tochFn);
            this.ele.addEventListener("touchend", tochFn);
            let timerId;
            function tochFn(e){
                // e.preventDefault();
                switch (e.type){
                    case "touchstart":
                        timerId = setTimeout(function(){
                            hanler.call(this, e);
                        },2000)
                        break;
                    case "touchmove":
                        clearTimeout(timerId);
                        break;
                    case "touchend":
                       clearTimeout(timerId)
                       break;
                }
            }
        },
        // 左侧滑动
    }
    window.$ = window.myMobile = myMobile;
})(window);

2.2 封装左侧滑动与右侧滑动

   slideLeft: function (hanler){
            this.ele.addEventListener("touchstart", touchFn);
            this.ele.addEventListener("touchend", touchFn);
            var startX, startY, endX, endY;
            function touchFn(e){
                e.preventDefault();
                // console.log(e.changedTouches);
                var firstTouch = e.changedTouches[0];
                switch (e.type){
                    case "touchstart":
                        startX = firstTouch.pageX;
                        startY = firstTouch.pageY;
                        break;
                    case "touchend":
                        endX = firstTouch.pageX;
                        endY = firstTouch.pageY;
                        // x方向移动的距离大于Y(定义为左滑操作)并且移动距离超过了25px
                        if ( Math.abs(endX - startX) >= Math.abs(endY -startY) && startX - endX >= 25){
                            hanler.call(this, e)
                        }
                        break;
                }
            }
        }

3. FAQ


  • 浏览器切换为手机模式下运行
  • 或者真机调试都可以
目录
相关文章
|
5月前
|
小程序 JavaScript
微信小程序默认获取聚焦事件
微信小程序默认获取聚焦事件
94 0
|
7月前
|
JavaScript 前端开发
原生JS实现移动端短信验证码功能
原生JS实现移动端短信验证码功能
149 0
原生JS实现移动端短信验证码功能
|
1月前
|
小程序
【微信小程序】-- 页面事件 - 上拉触底 - 案例(二十七)
【微信小程序】-- 页面事件 - 上拉触底 - 案例(二十七)
|
4月前
|
JavaScript 定位技术
部分移动端独有的JS事件
部分移动端独有的JS事件
37 0
|
4月前
|
小程序
微信小程序onReachBottom事件使用
微信小程序onReachBottom事件使用
151 0
|
8月前
|
小程序
微信小程序系列——你知道什么是事件传播以及怎么防止吗?
微信小程序系列——你知道什么是事件传播以及怎么防止吗?
|
8月前
|
前端开发 JavaScript 定位技术
移动端手势事件和触摸交互
移动端手势事件和触摸交互
|
缓存 小程序 JavaScript
【微信小程序】收藏功能的实现(条件渲染、交互反馈)
wxml页面部分比较简单,添加一个view容器,命名为tool。然后添加三个小view,分别表示点赞、评论、收藏。每个小view中包括图片和数量,即image和text标签。每个功能按钮都绑定了对应的点击事件,即catchtap属性。除此之外,每个功能按钮绑定当前文章的id号。
366 0
|
算法
高级UI(二): 触摸反馈
触摸反馈应该是自定义view最简单的部分了,不过内部的原理是比较复杂的,去了解里面的核心机制,需要自己去阅读源码,才能更好理解整个触摸机制,当然,知其所以然也是远远不够的,下面我就带大家了解自定义view触摸机制的难点重点
96 0
高级UI(二): 触摸反馈
|
小程序 API
微信小程序:EventChannel实现页面间事件通信通道
微信小程序:EventChannel实现页面间事件通信通道
549 0

热门文章

最新文章