移动端进阶之touch触摸事件,拖拽事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 150px;
            height: 150px;
            background-color: red;
            margin: 20px auto;
        }
    </style>
</head>
<body>
    <div id="box" class="box"></div>
    <script>
 
        // 注意:一下触摸事件,必须在移动端才会触发,在浏览器中调试,要写换成移动端模式
        // function load (){
            var boxEL = document.getElementById('box');
            // boxEL.ontouchstart = handleStart;
            // boxEL.ontouchmove = handleMove;
            // boxEL.ontouchend = handleEnd;
            // boxEL.oncancel = handleCancel;
            /* 以上写法比较老旧 */
            /* 支持新事件绑定语法 */
            boxEL.addEventListener('touchstart',handleStart,false); // (‘触发’,事件,是否捕获/false冒泡)
            boxEL.addEventListener('touchmove',handleMove,false);
            boxEL.addEventListener('touchend',handleEnd,false);
            function handleStart(ev) {
                // console.log('touchstart',ev);
                var touch = ev.changeTouches[0];
                console.log(touch.pageX, touch.pageY);
            }
            function handleMove(ev) {
                console.log('touchmove');
            }
            function handleEnd(ev) {
                console.log('touchend');
            }
        // }   
        // window.addEventListener('load',load, false);
    </script>
</body>
</html>

 

2.单指托拽实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>单指拖拽</title>
    <style>
        .backtop {
            position: fixed;
            right: 20px;
            bottom: 20px;
            width: 45px;
            height: 45px;
            /* 知识点
            line-height是行高,针对的对象是文字,height针对的是容器,
            也就是高度,当height和line-height值相同时会居中,
            当line-height值小于height时文字向上移动,反之向下移动。
             */
            line-height: 45px;  /* 文字垂直居中 */
            text-align: center;  /* 水平居中 */
            background-color: rgba(0, 0, 0, 0.6);
            border-radius: 50%;
            color: #fff;
            font-size: 30px;
            /* 去除标签点击事件高亮效果 */
            -webkit-tap-highlight-color: transparent;
            /* 使用transform: translate3d 处理性能高 GUP */
        }
    </style>
</head>
<body>
    <a href="#" id="backtop" class="backtop">↑</a>
    <!-- <div id="backtop" class="backtop">↑</div> -->
    <script>
        var backtop = document.getElementById('backtop');
        /* 坐标原点 */
        var curPoint = {
            x: 0,
            y: 0
        }
        backtop.addEventListener('click', function() {
            move(this, -10 + curPoint.x, -10 + curPoint.y)
            curPoint.x += -10;
            curPoint.y += -10;
        }, false)
 
        function move(el, x, y) {
            x = x || 0; // 没有传就是0
            y = y || 0;
             
            el.style.transform = 'translate3d(' + x + 'px,' + y + 'px,0)';
        }
    </script>
</body>
</html>

 

posted @   小白咚  阅读(302)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2020-02-20 Element-ui组件库Table表格导出Excel表格--存在重复数据问题-----建议前端不做导出逻辑,应该是后端做处理
2020-02-20 ###MySQL 数据库DataBase
点击右上角即可分享
微信分享提示