• 移动化拖拽元素移动


    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>

  • 相关阅读:
    【Python】Pyinstaller打包Linux运行文件,暴露配置文件
    Node.js -- 模块化
    100天精通Python(进阶篇)——第34天:正则表达式大总结
    【51单片机】DS18B20(江科大)
    神经网络参数量和计算量,神经网络参数个数计算
    Java8实战-总结37
    数字人直播软件多少钱,数字人直播系统多少钱,真正赚钱的是?
    java-net-php-python-ssm大学生综合测评系统的设计与实现计算机毕业设计程序
    第一章 概论
    JavaScript之数组操作增删改、冒泡排序
  • 原文地址:https://blog.csdn.net/qq_43649479/article/details/126259781