• 移动化拖拽元素移动


    1. html>
    2. <html lang="en">
    3. <head>
    4.     <meta charset="UTF-8">
    5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7.     <title>Documenttitle>
    8.     <style>
    9.         body{
    10.             margin: 0;
    11.         }
    12.         #touchMove {
    13.             position: relative;
    14.             width: 100px;
    15.             border: 1px solid red;
    16.         }
    17.     style>
    18. head>
    19. <body>
    20.     <div id="touchMove" class="">开始体验div>
    21.     <script>
    22.         window.onload = function () {
    23.             var oDiv = document.getElementById('touchMove');
    24.             var disX, moveX, L, T, starX, starY, starXEnd, starYEnd;
    25.             oDiv.addEventListener('touchstart', function (e) {
    26.                 e.preventDefault();//阻止触摸时页面的滚动,缩放
    27.                 oDiv.style.transition = 'none'
    28.                 disX = e.touches[0].clientX - this.offsetLeft;
    29.                 disY = e.touches[0].clientY - this.offsetTop;
    30.                 //手指按下时的坐标
    31.                 starX = e.touches[0].clientX;
    32.                 starY = e.touches[0].clientY;
    33.                 //console.log(disX);
    34.             });
    35.             oDiv.addEventListener('touchmove', function (e) {
    36.                 L = e.touches[0].clientX - disX;
    37.                 T = e.touches[0].clientY - disY;
    38.                 //移动时 当前位置与起始位置之间的差值
    39.                 starXEnd = e.touches[0].clientX - starX;
    40.                 starYEnd = e.touches[0].clientY - starY;
    41.                 console.log( e.touches[0].clientX , disX);
    42.                 if (L < 0) {//限制拖拽的X范围,不能拖出屏幕
    43.                     L = 0;
    44.                 }else if (L > document.documentElement.clientWidth - this.offsetWidth) {
    45.                     L = document.documentElement.clientWidth - this.offsetWidth;
    46.                 }
    47.                 if (T < 0) {//限制拖拽的Y范围,不能拖出屏幕
    48.                     T = 0;
    49.                 }else if (T > document.documentElement.clientHeight - this.offsetHeight) {
    50.                     T = document.documentElement.clientHeight - this.offsetHeight;
    51.                 }
    52.                 moveX = L + 'px';
    53.                 moveY = T + 'px';
    54.                 this.style.left = moveX;
    55.                 this.style.top = moveY;
    56.                 console.log(moveX);
    57.             });
    58.             window.addEventListener('touchend', function (e) {
    59.                 //alert(parseInt(moveX))
    60.                 //判断滑动方向
    61.                 //判断滑动方向
    62.                 oDiv.style.transition = 'all 0.3s'
    63.                 if (L > document.documentElement.clientWidth / 2) {
    64.                     oDiv.style.left = document.documentElement.clientWidth - oDiv.offsetWidth  + 'px'
    65.                 } else {
    66.                     oDiv.style.left = 0
    67.                 }
    68.             });
    69.         }
    70.     script>
    71. body>
    72. html>

  • 相关阅读:
    单片机STM32有什么推荐的裸机编程架构
    常见的 NoSQL 数据库有哪些?
    世界环境日 | 周大福用心服务推动减碳环保
    数据化运营05 关注用户留存就够了吗?值得你关注的另外 3 种留存
    冰蝎的原理与安装使用
    《STL源码剖析》笔记——allocator
    MySQL——连接查询与子查询
    C++面试连环问-STL
    Linux gcc 预处理,编译,汇编,链接的命令打字练习
    Go/Golang语言学习实践[回顾]教程13--详解Go语言的词法解析单元
  • 原文地址:https://blog.csdn.net/qq_43649479/article/details/126259781