移动端touch触摸事件(滑动效果和手势操作)

一、定义

①touch是移动端的触摸事件,而且是一组事件,主要有以下事件:

  • touchstart 事件:当手指触摸屏幕的时候触发
  • touchmove 事件:当手指在屏幕来回滑动的时候触发
  • touchend 事件:当手指离开屏幕的时候触发
  • touchcancel事件:当被终止滑动的时候触发(来电、弹消息)

②利用touch相关事件可以实现移动端常见的滑动效果和移动端常见的手势事件,比较常用的事件主要是touchstart、touchmove、touchend,并且一般是使用addEventListener绑定事件

            dom.addEventListener('touchstart',function(){

            });
            dom.addEventListener('touchmove',function(){
                
            });
            dom.addEventListener('touchend',function(){
                
            });     

二、使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>touch事件</title>
    <style>
        .body{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 200px;
            height: 200px;background: #ccc;
            float: left;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <script>
        window.onload=function(){
            var box=document.querySelector('.box');
            box.addEventListener('touchstart',function(){
                console.log('start')
            });
            box.addEventListener('touchmove',function(){
                console.log('move')
            });
            box.addEventListener('touchend',function(){
                console.log('end')
            });
        }
    </script>
</body>
</html>

三、事件对象event

四、分析移动端滑动实现的原理

①让触摸的元素随着手指的滑动做位置的改变

②位置的改变,需要当前的坐标,当前手指的坐标和移动后的坐标都可以在事件对象中拿到

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>touch事件</title>
    <style>
        .body{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 200px;
            height: 200px;background: #ccc;
            float: left;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <script>
        window.onload=function(){
            var box=document.querySelector('.box');
            box.addEventListener('touchstart',function(e){
                console.log('开始坐标('+e.touches[0].clientX+','+e.touches[0].clientY+')');
            });
            box.addEventListener('touchmove',function(e){
                console.log('移动的坐标('+e.touches[0].clientX+','+e.touches[0].clientY+')');
            });
            box.addEventListener('touchend',function(e){
                console.log('结束的坐标不会记录');
            });
        }
    </script>
</body>
</html>

五、移动端的手势事件(实现左滑手势和右滑手势的原理)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>手势事件的实现</title>
    <style>
        .body{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 200px;
            height: 200px;background: #ccc;
            float: left;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <script>
        window.onload=function(){
            // 封装手势的函数
            var bindSwipeEvent=function(dom,rightCallback,leftCallback){
                // 手势实现的条件:滑动并且滑动距离大于50px
                var isMove=false;
                var startX=0;
                var distanceX=0;
                dom.addEventListener('touchstart',function(e){
                    startX=e.touches[0].clientX;
                });
                dom.addEventListener('touchmove',function(e){
                    isMove=true;
                    var moveX=e.touches[0].clientX;
                    distanceX=moveX-startX;
                });
                dom.addEventListener('touchend',function(e){
                    // 滑动结束
                    if(isMove && Math.abs(distanceX)>50){
                        if(distanceX>0){
                            rightCallback && rightCallback.call(this,e);
                        }else{
                            leftCallback && leftCallback.call(this,e);
                        }
                    }
                    // 重置参数
                    isMove=false;
                    startX=0;
                    distanceX=0;
                });
            };
            // 调用
            bindSwipeEvent(document.querySelector('.box'),function(e){
                console.log('左滑手势');
            },function(e){
                console.log('右滑手势');
            })
        }
    </script>
</body>
</html>

 

转载于:https://www.cnblogs.com/EricZLin/p/9327005.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现移动端touch事件的横向滑动列表效果可以使用原生的JavaScript和CSS3来实现。 首先,我们需要在HTML中创建一个容器元素,用来包含列表项。容器元素需要设置overflow-x属性为scroll,使得内容超出容器范围时可以滚动。 ```html <div class="container"> <ul class="list"> <li>项1</li> <li>项2</li> <li>项3</li> <li>项4</li> <li>项5</li> </ul> </div> ``` 然后,在CSS中,我们需要设置容器元素和列表项的样式,以及使用CSS3的transition属性来实现平滑的过渡效果。 ```css .container { width: 100%; overflow-x: scroll; -webkit-overflow-scrolling: touch; /* 添加iOS滚动效果 */ } .list { display: flex; flex-wrap: nowrap; /* 设置列表项不换行 */ transition: transform 0.3s ease; /* 添加平滑的过渡效果 */ } .list li { width: 100px; height: 100px; margin-right: 10px; background-color: #ccc; } ``` 最后,在JavaScript中,我们需要监听容器元素的touchstart、touchmove和touchend事件,计算滑动距离并通过改变列表项的transform属性来实现横向滑动效果。 ```javascript const container = document.querySelector('.container'); const list = document.querySelector('.list'); let isDragging = false; let startPosition = 0; let currentTranslate = 0; let prevTranslate = 0; let animationId = 0; container.addEventListener('touchstart', touchStart); container.addEventListener('touchmove', touchMove); container.addEventListener('touchend', touchEnd); container.addEventListener('touchcancel', touchEnd); function touchStart(event) { if (event.target.classList.contains('list')) { isDragging = true; startPosition = event.touches[0].clientX; animationId = requestAnimationFrame(updateAnimation); container.classList.add('grabbing'); } } function touchMove(event) { if (isDragging) { const currentPosition = event.touches[0].clientX; currentTranslate = prevTranslate + currentPosition - startPosition; } } function touchEnd() { isDragging = false; cancelAnimationFrame(animationId); prevTranslate = currentTranslate; container.classList.remove('grabbing'); } function updateAnimation() { list.style.transform = `translateX(${currentTranslate}px)`; animationId = requestAnimationFrame(updateAnimation); } ``` 通过以上代码,我们就成功地实现了移动端touch事件的横向滑动列表效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值