移动端左划右划事件触发简单的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0">
    <title>Title</title>
    <style>
        *{margin:0;padding:0;}
        ul li {height: 50px; box-sizing: border-box; list-style-type: none; border:1px solid #ccc;}
    </style>
</head>
<body>
<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
</ul>
</body>
<script>
    window.onload = function () {
        var touch = new Touch(document.getElementsByTagName("li"),80).init();
        
        //向左滑动触发事件
        touch.swipeLeft = function (dom) {
            alert(dom.innerText);
        };

        //向右滑动事件
        touch.swipeRight = function (dom) {
            alert(dom.innerText);
        }
    };

    function Touch(dom,range) {
        this.init = function () {
            var that = this;
            for(var i = 0; i<dom.length; i++){
                (function (dom) {
                    function touchstart(event) {
                        var e = event || window.event;
                        if(e.targetTouches.length === 1){
                            var startX = e.targetTouches[0].clientX,
                                startY = e.targetTouches[0].clientY;
                            function touchmove(e) {
                                var moveEndX = e.targetTouches[0].clientX,
                                    moveEndY = e.targetTouches[0].clientY;
                                if((that.getAngle(startX,startY,moveEndX,moveEndY) >= 135 || that.getAngle(startX,startY,moveEndX,moveEndY) <= -135) && that.getRange(startX,startY,moveEndX,moveEndY) >= range){
                                    that.swipeLeft(dom);
                                    dom.removeEventListener("touchmove",touchmove);
                                }else if((that.getAngle(startX,startY,moveEndX,moveEndY) >= -45 && that.getAngle(startX,startY,moveEndX,moveEndY) <= 45)&& that.getRange(startX,startY,moveEndX,moveEndY) >= range){
                                    that.swipeRight(dom);
                                    dom.removeEventListener("touchmove",touchmove);
                                }
                            }

                            function touchend() {
                                dom.removeEventListener("touchend",touchend);
                                dom.removeEventListener("touchmove",touchmove);
                            }

                            dom.addEventListener("touchmove",touchmove);
                            dom.addEventListener("touchend",touchend);
                        }
                    }

                    dom.addEventListener("touchstart",touchstart);
                })(dom[i]);
            }

            return this;
        };

        //计算滑动的角度
        this.getAngle = function (px1, py1, px2, py2) {
            //两点的x、y值
            x = px2-px1;
            y = py2-py1;
            hypotenuse = Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2));
            //斜边长度
            cos = x/hypotenuse;
            radian = Math.acos(cos);
            //求出弧度
            angle = 180/(Math.PI/radian);
            //用弧度算出角度
            if (y<0) {
                angle = -angle;
            } else if ((y == 0) && (x<0)) {
                angle = 180;
            }
            return angle;
        };

        //计算两点之间的距离
        this.getRange = function (px1,py1,px2,py2) {
            return Math.sqrt(Math.pow(Math.abs(px1 - px2), 2) + Math.pow(Math.abs(py1 - py2), 2));
        };

        this.swipeLeft = function (dom) {};

        this.swipeRight = function (dom) {}
    }

</script>
</html>


需要修改的都在window.onload里面

使用:

首先实例化对象,将需要触发dom数组传入,第二个值是手指头滑动多长的距离触发事件

var touch = new Touch(dom,80).init();

然后给touch赋值左划和右划事件

        //向左滑动触发事件
        touch.swipeLeft = function (dom) {
            alert(dom.innerText);
        };

        //向右滑动事件
        touch.swipeRight = function (dom) {
            alert(dom.innerText);
        }
dom就是触发事件的单个的dom对象,这里直接弹出了一个dom的内容,具体还是测试吧


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
setOnDragListener 通常用于监听 View 的拖动事件,而属于手势事件,可以通过 GestureDetector 来监听。下面是一个示例代码: ```java public class MyView extends View { private GestureDetector mGestureDetector; public MyView(Context context, AttributeSet attrs) { super(context, attrs); mGestureDetector = new GestureDetector(context, new MyGestureListener()); } @Override public boolean onTouchEvent(MotionEvent event) { return mGestureDetector.onTouchEvent(event); } private class MyGestureListener extends GestureDetector.SimpleOnGestureListener { private static final int SWIPE_THRESHOLD = 100; private static final int SWIPE_VELOCITY_THRESHOLD = 100; @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { boolean result = false; try { float diffY = e2.getY() - e1.getY(); float diffX = e2.getX() - e1.getX(); if (Math.abs(diffX) > Math.abs(diffY)) { if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { if (diffX > 0) { onSwipeRight(); } else { onSwipeLeft(); } result = true; } } } catch (Exception exception) { exception.printStackTrace(); } return result; } } public void onSwipeRight() { // 处理事件 } public void onSwipeLeft() { // 处理事件 } } ``` 在这个示例代码中,MyView 继承自 View,重写 onTouchEvent 方法,在该方法中将触摸事件交给 GestureDetector 处理。在 GestureDetector 的回调函数中,判断手势是否为滑动,如果是,则调用 MyView 中的 onSwipeLeft 或 onSwipeRight 方法进行处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值