实现思路:
①鼠标按下+鼠标移动 → 拖拽
②鼠标松开 → 无拖拽
③鼠标偏移 → 拖拽距离
用JavaScript事件方法表示就是:
① onmousedown + onmousemove → startDrag()
② onmouseup → stopDrag()
drag.js代码:
var params = {left: 0,
top: 0,
currentX: 0,
currentY: 0,
flag: false
}
//获取相关CSS属性
var getCss = function(o,key){
return o.currentStyle? o.currentStyle[key] : document.defaultView.getComputedStyle(o,false)[key]
}
//拖拽的实现
var startDrag = function(bar, target, callback){
if(getCss(target, "left") !== "auto"){
params.left = getCss(target, "left")
}
if(getCss(target, "top") !== "auto"){
params.top = getCss(target, "top")
}
//o是移动对象
bar.onmousedown = function(event){
params.flag = true
if(!event){
event = window.event
//防止IE文字选中
bar.onselectstart = function(){
return false
}
}
var e = event
params.currentX = e.clientX
params.currentY = e.clientY
}
document.onmouseup = function(){
params.flag = false
if(getCss(target, "left") !== "auto"){
params.left = getCss(target, "left")
}
if(getCss(target, "top") !== "auto"){
params.top = getCss(target, "top")
}
}
document.onmousemove = function(event){
var e = event ? event: window.event
if(params.flag){
var nowX = e.clientX, nowY = e.clientY
var disX = nowX - params.currentX, disY = nowY - params.currentY
target.style.left = parseInt(params.left) + disX + "px"
target.style.top = parseInt(params.top) + disY + "px"
}
if (typeof callback == "function") {
callback(parseInt(params.left) + disX, parseInt(params.top) + disY)
}
}
}
HTML/CSS
<style type="text/css">#box{position:absolute left:100px top:100px padding:5px background:#f0f3f9 font-size:12px -moz-box-shadow:2px 2px 4px #666666 -webkit-box-shadow:2px 2px 4px #666666}
#main{border:1px solid #a0b3d6 background:white}
#bar{line-height:24px background:#beceeb border-bottom:1px solid #a0b3d6 padding-left:5px cursor:move}
#content{width:420px height:250px padding:10px 5px}
</style>
<div id="box">
<div id="main">
<div id="bar">拖拽</div>
<div id="content">
内容……
</div>
</div>
</div>
JS部分
<script src="drag.js" type="text/javascript"></script><script type="text/javascript">
var oBox = document.getElementById("box")
var oBar = document.getElementById("bar")
startDrag(oBar, oBox)
</script>
<div class="upload-img-box" ref="moveWrap"><div class="show-box" ref="rotate" @mousedown="moveImg" @mousewheel.prevent="rollImg($event,)">
<img :src="singleList.fileImgUrl" class="uploadimg" ref="img" :style="{transform:'scale('+multiples/100+') rotate('+rotate +'deg)'}"/>
</div>
<div class="img-plus" @click="toBIgChange()"><span class="tip">放大</span></div>
<div class="img-minus" @click="toSmallChange()"><span class="tip">缩小</span></div>
<div class="img-rotate" @click="toRotate()"><span class="tip">旋转</span></div>
</div>
图片上传加拖拽插件:网页链接
在vue项目入口文件index.js(main.js),或者在调用组件中引入如下代码:
import VueUploadDrag from 'vue-upload-drag'
Vue.use(VueUploadDrag)
根据你的需求在调用组件代码中增加:
<template> <maple-upload v-model="fileList" :config="config"
@onError="onError" @onSuccess="onSuccess"
/></template>
onError ({ err, file, uid }) { // 上传失败,会自动把图片移除,建议给提示
alert('图片上传失败')
console.log(err, file, uid)
},
onSuccess ({res, file, _uid , exChangeUrl, handleRemove}) { // 上传成功,请必须接受onSuccess方法
console.log(res, file, _uid , exChangeUrl, handleRemove)
if (res.code === '0') { // res为后端返回数据,举例而已
const {url} = res.obj
exChangeUrl(_uid, url)// url为后端返回的图片链接值,uid是onSuccess传入的
} else {
alert('图片上传失败')
handleRemove(_uid) // 删除图片
}
}