zoukankan      html  css  js  c++  java
  • 元素可拖拽(移动端与pc端)

      项目中经常遇到这种需求,特此记录下:

      css部分:

    <style>
        #target {
             200px;
    	height: 200px;
    	border-radius: 6px;
    	background: #eee;
    	position: absolute;
        }
    </style>    
    

      

      html部分:

    <div id="target"></div>
    

      

      js部分(这里分了两种情况,因为pc端与移动端的处理情况有点不太一样):

      (1)、pc端

    const winW = window.innerWidth
    const winH = window.innerHeight 
    const target = document.getElementById('target')
    const targetW = target.offsetWidth
    const targetH = target.offsetHeight
    const maxL = winW - targetW           // 距离左侧的最大距离
    const maxT = winH - targetH           // 距离顶部的最大距离
    let posArr = []
            
    target.addEventListener('mousedown', function (e) {
        posArr = [e.clientX, e.clientY]   
    })
    
    document.addEventListener('mousemove', function (e) {
        let [x, y] = [e.clientX, e.clientY]  
        const deltaX = x - posArr[0]        // 移动的水平距离
        const deltaY = y - posArr[1]        // 移动的垂直距离
        const left = parseInt(target.style.left || 0)   // 如果值不存在时,赋默认值很关键,不然值为NaN
        const top = parseInt(target.style.top || 0)
        let [moveX, moveY] = ['', '']
        if (left + deltaX > maxL) {
        moveX = maxL
        } else if (left + deltaX >= 0 && left + deltaX <= maxL) {
        moveX = left + deltaX
        } else {
        moveX = 0
        }
    
        if (top + deltaY > maxT) {
        moveY = maxT
        } else if (top + deltaY >= 0 && top + deltaY <= maxT) {
        moveY = top + deltaY
        } else {
        moveY = 0
        }
        target.style.left = moveX + 'px'
        target.style.top = moveY + 'px'
        posArr = [x, y]
    })

      (2)、移动端,情况稍微有点不一样,监听的事件为touch事件,其次获取元素当前位置也有点不一样,具体代码:

    const winW = window.innerWidth
    const winH = window.innerHeight 
    const target = document.getElementById('target')
    const targetW = target.offsetWidth
    const targetH = target.offsetHeight
    const maxL = winW - targetW           // 距离左侧的最大距离
    const maxT = winH - targetH           // 距离顶部的最大距离
    let posArr = []
            
    target.addEventListener('touchstart', function (e) {
        posArr = [e.changedTouches[0].clientX, e.changedTouches[0].clientY]
    })
    
    document.addEventListener('touchmove', function (e) {
        let [x, y] = [e.changedTouches[0].clientX, e.changedTouches[0].clientY]
        const deltaX = x - posArr[0]        // 移动的水平距离
        const deltaY = y - posArr[1]        // 移动的垂直距离
        const left = parseInt(target.style.left || 0)   // 如果值不存在时,赋默认值很关键,不然值为NaN
        const top = parseInt(target.style.top || 0)
        let [moveX, moveY] = ['', '']
        if (left + deltaX > maxL) {
        moveX = maxL
        } else if (left + deltaX >= 0 && left + deltaX <= maxL) {
        moveX = left + deltaX
        } else {
        moveX = 0
        }
        if (top + deltaY > maxT) {
        moveY = maxT
        } else if (top + deltaY >= 0 && top + deltaY <= maxT) {
        moveY = top + deltaY
        } else {
        moveY = 0
        }
        target.style.left = moveX + 'px'
        target.style.top = moveY + 'px'
        posArr = [x, y]
    })
  • 相关阅读:
    微软企业库4.1学习笔记(二十六)Unity依赖注入模块3
    微软企业库4.1学习笔记(三十七)日志模块 在应用中使用日志模块
    微软企业库5.0学习笔记(三十五)数据访问模块 DataSet以及数据库事务
    微软企业库4.1学习笔记(四十一)依赖注入模块Unity 简介
    项目统一开发管理解决方案思路[项目组成员同时做很多项目的解决思路探讨]
    在moss2007WEB应用服务器上发布独立web程序时遇到的问题的解决思路
    工作流表单自定义的误区
    文档库创建的子文件夹的URL显示为 http://[机器名]/.... 导致无法正常访问的问题解决办法
    申请加入 .NET企业应用开发 博客团队请回复
    儿子照片
  • 原文地址:https://www.cnblogs.com/jf-67/p/13405063.html
Copyright © 2011-2022 走看看