元素鼠标缩放自定义指令
import Vue from 'vue'
let containerScale = {
init: 1,
min: 0.2,
max: 3,
onceNarrow: 0.05,
onceEnlarge: 0.05
}
let container = {
scale: 1,
scaleOrigin: {
x: 0,
y: 0
},
scaleShow: '100%'
}
Vue.directive('scale', {
bind (el) {
const scaleDom = el
const scaleFn = ev => {
if (ev.deltaY < 0) {
let newScale = handleAdd(container.scale, containerScale.onceEnlarge)
if (newScale <= containerScale.max) {
container.scale = newScale
container.scaleShow = handleMul(container.scale, 100)
scaleDom.style.transform = `scale(${newScale})`
}
} else if (container.scale) {
let newScale = handleSub(container.scale, containerScale.onceNarrow)
if (newScale >= containerScale.min) {
container.scale = newScale
container.scaleShow = handleMul(container.scale, 100)
scaleDom.style.transform = `scale(${newScale})`
}
}
}
scaleDom.onmousewheel = ev => {
ev.preventDefault()
scaleFn(ev)
}
}
})
function handleMul (a, b) {
let c = 0
let d = a.toString()
let e = b.toString()
try {
c += d.split('.')[1].length
} catch (f) { }
try {
c += e.split('.')[1].length
} catch (f) { }
return (Number(d.replace('.', '')) * Number(e.replace('.', ''))) / Math.pow(10, c)
}
function handleAdd (a, b) {
let c, d, e
try {
c = a.toString().split('.')[1].length
} catch (f) {
c = 0
}
try {
d = b.toString().split('.')[1].length
} catch (f) {
d = 0
}
return (e = Math.pow(10, Math.max(c, d))), (handleMul(a, e) + handleMul(b, e)) / e
}
function handleSub (a, b) {
let c, d, e
try {
c = a.toString().split('.')[1].length
} catch (f) {
c = 0
}
try {
d = b.toString().split('.')[1].length
} catch (f) {
d = 0
}
return (e = Math.pow(10, Math.max(c, d))), (handleMul(a, e) - handleMul(b, e)) / e
}