• js实现拖拽移动


    1. <script>
    2. export default {
    3. data () {
    4. return {}
    5. },
    6. mounted () {
    7. this.btn()
    8. },
    9. methods: {
    10. btn () {
    11. const imgs = document.querySelectorAll('.img')
    12. const main = document.querySelector('#main')
    13. const zw = document.querySelector('.zw')
    14. const isMobile = navigator.userAgent.match(/Mobile/)
    15. let isDrag = false
    16. let index
    17. let py = {
    18. left: 0,
    19. top: 0
    20. }
    21. const move = (el, x, y) => {
    22. el.setAttribute('style', `pointer-events:none;position:absolute;left:${x}px;top:${y}px`)
    23. }
    24. document.addEventListener(isMobile ? 'touchstart' : 'mousedown', e => {
    25. isMobile && (e = e.touches[0])
    26. index = e.target.dataset.index
    27. if (index && !isDrag) {
    28. py.left = e.pageX - imgs[index].offsetLeft
    29. py.top = e.pageY - imgs[index].offsetTop
    30. zw.style.display = 'block'
    31. main.insertBefore(zw, imgs[index])
    32. move(imgs[index], e.pageX - py.left, e.pageY - py.top)
    33. }
    34. isDrag = true
    35. })
    36. document.addEventListener(isMobile ? 'touchmove' : 'mousemove', e => {
    37. isMobile && (e = e.touches[0])
    38. if (isDrag && index) {
    39. move(imgs[index], e.pageX - py.left, e.pageY - py.top)
    40. }
    41. })
    42. document.addEventListener(isMobile ? 'touchend' : 'mouseup', e => {
    43. isDrag = false
    44. zw.style.display = ''
    45. if (imgs[index]) {
    46. imgs[index].setAttribute('style', '')
    47. main.insertBefore(imgs[index], zw)
    48. }
    49. })
    50. imgs.forEach(v => {
    51. v.addEventListener(isMobile ? 'touchmove' : 'mouseenter', e => {
    52. isMobile && (e = e.touches[0])
    53. if (isDrag) {
    54. const list = [...main.children]
    55. const imgIndex = list.findIndex(el => v == el)
    56. const zwIndex = list.findIndex(el => zw == el)
    57. if (zwIndex < imgIndex) {
    58. main.insertBefore(v, zw)
    59. } else {
    60. main.insertBefore(zw, v)
    61. }
    62. }
    63. })
    64. })
    65. }
    66. }
    67. }
    68. script>
    69. <style>
    70. #main {
    71. display: flex;
    72. justify-content: space-evenly;
    73. width: 1020px;
    74. margin: 40px auto 0;
    75. }
    76. .img {
    77. width: 100px;
    78. user-select: none;
    79. height: 100px;
    80. background: no-repeat center center;
    81. background-size: cover;
    82. border: 1px solid black;
    83. }
    84. .bg1 {
    85. background-image: url('https://cdn.pixabay.com/photo/2020/02/05/22/01/bush-4822500__480.jpg');
    86. }
    87. .bg2 {
    88. background-image: url('https://cdn.pixabay.com/photo/2022/01/24/13/51/temple-6963458__480.jpg');
    89. }
    90. .bg3 {
    91. background-image: url('https://cdn.pixabay.com/photo/2020/12/02/01/06/chipmunk-5795916__480.jpg');
    92. }
    93. .bg4 {
    94. background-image: url('https://img0.baidu.com/it/u=3488728512,3798628168&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500');
    95. }
    96. .bg5 {
    97. background-image: url('https://img1.baidu.com/it/u=2837573517,2587904370&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750');
    98. }
    99. .bg6 {
    100. background-image: url('https://img0.baidu.com/it/u=3647457944,660791411&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750');
    101. }
    102. .zw {
    103. width: 100px;
    104. height: 100px;
    105. display: none;
    106. }
    107. style>

  • 相关阅读:
    Tornado服务实现文件下载功能
    Asterisk Ubuntu 安装
    如何用Java编写代码来等待一个线程join()??
    Spark--经典SQL50题
    Ubuntu安装截图工具Shutter及使用
    我们如何在工作与生活中找到平衡点?
    【华为OD机试真题 python】最长连续子序列 【2022 Q4 | 100分】
    3.6让MBR使用磁盘
    java 企业工程管理系统软件源码 自主研发 工程行业适用
    k8s.8-使用sealos部署kubernetes集群并实现集群管理
  • 原文地址:https://blog.csdn.net/qq_41588991/article/details/134370705