console
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>css3平移, 缩放demo</title>
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport">
<meta name="renderer" content="webkit">
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
body {
font-size: 14px;
font-family: sans-serif,Microsoft YaHei,Helvetica Neue,-apple-system,PingFang SC,Hiragino Sans GB,Arial;
color: #27282b;
background: #fff;
}
a {
text-decoration: none;
color: #27282b;
}
*,
*::before,
*::after {
outline: none;
box-sizing: border-box;
}
html,
body {
height: 100%;
}
.d1 {
width: 50%;
height: 100px;
background: #eee;
user-select: none;
position: relative;
margin: 0 auto 50px auto;
}
.container {
width: 100vw;
height: 100vh;
overflow: hidden;
position: relative;
}
.panel-w {
user-select: none;
position: absolute;
top: 10px;
left: 10px;
z-index: 2;
}
.cont-z {
width: 50vw;
height: 50vh;
background: linear-gradient(90deg, #c7c7e7 50%, transparent 50%),
linear-gradient(#c7c7e7 50%, transparent 50%), #dee1ee;
background-size: 50px 50px;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
z-index: 1;
}
</style>
</head>
<body>
<div class="wrapper" id="app">
<div class="container">
<div class="panel-w">缩放:{{scale}} x:{{trfX}} y:{{trfY}}</div>
<div class="cont-z" :style="mainCss"></div>
</div>
</div>
<script src="https://lib.baomitu.com/vue/2.6.10/vue.min.js"></script>
<script>
const vm = new Vue({
el: '#app',
data() {
return {
scale: 1,
trfX: 0,
trfY: 0,
trfcacheX: 0,
trfcacheY: 0,
isAltKey: false,
isMouseDown: false,
initClientX: 0,
initClientY: 0,
}
},
computed: {
mainCss() {
return `
transform: translate3d(${this.trfX}px, ${this.trfY}px, 0) scale(${this.scale});
`
}
},
methods: {
func1() {
},
winEvent() {
window.addEventListener('wheel', (e) => {
let shrink = (this.scale - (this.isAltKey ? 0.5 : 0.1)).toFixed(2)
let ampl = (this.scale + (this.isAltKey ? 0.5 : 0.1)).toFixed(2)
if (e.deltaY > 0) {
this.scale = Math.max(0.05, shrink)
} else {
this.scale = Math.max(0, ampl)
}
})
window.addEventListener('keydown', (e) => {
if (e.ctrlKey) {
return false
}
if (e.altKey) {
this.isAltKey = true
}
})
window.addEventListener('keyup', (e) => {
this.isAltKey = false
})
document.querySelector('.cont-z').onmousedown = (e) => {
this.isMouseDown = true
this.initClientX = e.clientX
this.initClientY = e.clientY
this.trfX = this.trfcacheX
this.trfY = this.trfcacheY
}
window.addEventListener('mousemove', (e) => {
let x = e.clientX
let y = e.clientY
if (this.isMouseDown) {
this.trfX = this.trfcacheX + (x - this.initClientX)
this.trfY = this.trfcacheY + (y - this.initClientY)
}
})
window.addEventListener('mouseup', (e) => {
this.isMouseDown = false
this.trfcacheX = this.trfX
this.trfcacheY = this.trfY
})
window.addEventListener('contextmenu', (e) => {
event.preventDefault()
this.scale = 1
})
},
},
mounted() {
this.winEvent()
},
})
</script>
</body>
</html>