css_vue 中拖拽
css_vue 中拖拽
css_vue 中拖拽
下面more是分隔符
基本常见
这个版本没有办法实现父元素和子元素一起拖动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| <template>
<div style="border: 1px solid"> <p>{{ positionX }}</p> <p>{{ positionY }}</p> <div style="width: 800px; height: 500px; margin-left: 100px; border: 2px solid; position: relative"> <div id="fei" style="border: 1px solid" @mousedown="move">
<div style="text-align: center"> <p>{{ positionX }}</p> <p>{{ positionY }}</p> </div> </div> </div> </div> </template>
<script> export default { data: function () { return { positionX: 0, positionY: 0 } }, computed: {}, methods: { move(e) { const cardWidth = 800 const cardHeigh = 500 const seflWidth = 100 const seflHeight = 100
let odiv = e.target console.log('xxxxxxxxx', e)
let disX = e.clientX - odiv.offsetLeft let disY = e.clientY - odiv.offsetTop
document.onmousemove = e => {
let left = e.clientX - disX let top = e.clientY - disY if (Number(left) <= 0) { left = 0 } else if (Number(left) >= cardWidth - seflWidth) { left = cardWidth - seflWidth } if (Number(top) <= 0) { top = 0 } else if (Number(top) >= cardHeigh - seflHeight) { top = cardHeigh - seflHeight }
this.positionX = left this.positionY = top
odiv.style.left = left + 'px' odiv.style.top = top + 'px' } document.onmouseup = e => { console.log('鼠标抬起清空') document.onmousemove = null document.onmouseup = null } } } } </script>
<style scoped> #fei { position: relative; top: 10px; left: 10px; width: 100px; height: 100px; background: #cccccc; } </style>
|
使用插件vue-drag-resize
1
| <VueDragResize :isActive="true" :w="120" :h="120" :parentW="500" :parentH="375" :parentLimitation="true">
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <template> <div id="app"> <VueDragResize :isActive="true" :w="200" :h="200">
<img src="/src/assets/tempCard/images.jpg" name="aaa" alt="" title="bbb" style="width: 50px; height: 50px" @mousedown="move" /> </VueDragResize> </div> </template>
<script> import VueDragResize from 'vue-drag-resize/src'
export default { name: 'App',
components: { VueDragResize },
data() { return { width: 0, height: 0, top: 0, left: 0 } },
methods: { resize(newRect) { this.width = newRect.width this.height = newRect.height this.top = newRect.top this.left = newRect.left } } } </script>
|