• 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>

  • 相关阅读:
    VUE UI组件ui-libs.vercel.app 花了太长时间进行响应怎么解决?
    佘太地纯的前世今生
    2024上海MWC 参展预告 | 未来先行,解锁数字化新纪元!
    python学习笔记12:小数类型的角度到度分秒的转换
    Bandzip下载(好用的解压缩工具)
    【Putty】win10 / win 11:SSH 远程连接工具 Putty 下载、安装
    Oracle中用户自定义函数(五)
    weblogic乱码报错解决思路
    磁盘取证
    SQL优化记录
  • 原文地址:https://blog.csdn.net/qq_41588991/article/details/134370705