使用原生dom及js实现类似vben-admin图片裁剪效果

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
body>div{
float: left;
}
#box{
width: 330px;
height: 240px;
overflow: hidden;
position: relative;
}
#box::after{
content: '';
position: absolute;
top: 0;
left: 0;
display: inline-block;
width: 100%;
height: 100%;
background-color: #444444;
opacity: 0.6;
z-index: 2;
}
.absolut-div{
position: absolute;
left: 0;
top: 0;
width: 100px;
height: 100px;
border: 1px solid teal;
box-shadow: 0 2px 2px teal;
transform: translate3d(20px,20px,0);
cursor: move;
z-index: 3;
}
#box img{
display: block;
width: 100%;
height: 100%;
}
.option-btn{
display: flex;
margin-top: 0.625rem;
cursor: pointer;
flex-wrap: wrap;
}
#cutimg{
width: 200px;
height: 200px;
border-radius: 50%;
border: 1px solid #778899;
}
</style>
</head>
<body>
<div style="width: 50%;" class="left-div">
<div id="box">
<img src="" alt="图片裁剪">
<div class="absolut-div" onm ousedown="mousedown(event)"></div>
</div>
<input type="file" files="fileList" name="" id="file1" value="" onchange="fn1(event)" />
<div class="option-btn">
<button type="button" onclick="leftrotate()">左旋转</button>
<button type="button" onclick="rightrotate()">右旋转</button>
<button type="button" onclick="horflip()">水平翻转</button>
<button type="button" onclick="verflip()">垂直翻转</button>
<button type="button" onclick="boostit()">放大</button>
<button type="button" onclick="shrinkit()">缩小</button>
<button type="button" onclick="showorigin()">还原</button>
</div>
</div>
<div style="width: 50%;" class="right-div">
<div id="cutimg"></div>
</div>
<script>
let img1 = document.getElementById("box").children[0]
let dragdom = document.getElementById("box")
let targetdom = document.getElementsByClassName("absolut-div")[0]
let largeImg = document.getElementById("cutimg")
let transformcss = ''
let num = 0
let rox = 0
let roy = 0
let mul = 1
let imgUrl = ''
function fn1(event){
targetdom.style.transform = 'translate3d(20px,20px,0)'
let file = event.target.files[0]
if(file.type.indexOf("image") == 0) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e) {
console.log(this)
// 图片base64化
imgUrl = this.result;
img1.setAttribute("src",imgUrl)
targetdom.style.backgroundImage = `url(${imgUrl})`
targetdom.style.backgroundSize = '330px 240px'
targetdom.style.backgroundPosition = '-20px -20px'
targetdom.style.backgroundRepeat = 'no-repeat'
largeImg.style.backgroundImage = `url(${imgUrl})`
largeImg.style.backgroundSize = '660px 480px'
largeImg.style.backgroundPosition = '-40px -40px'
largeImg.style.backgroundRepeat = 'no-repeat'
};
}
}
function mousedown(event){
let offsetx = event.offsetX
let offsety = event.offsetY
dragdom.onmousemove = function(event){
let clientx = event.clientX
let clienty = event.clientY
let left = clientx-offsetx
let top = clienty-offsety
if(clientx-offsetx<0){
left = 0
}else if(clientx-offsetx>dragdom.offsetWidth-targetdom.offsetWidth){
left = dragdom.offsetWidth-targetdom.offsetWidth
}
if(clienty-offsety<0){
top = 0
}else if(clienty-offsety>dragdom.offsetHeight-targetdom.offsetHeight){
top = dragdom.offsetHeight-targetdom.offsetHeight
}
targetdom.style.transform = `translate3d(${left}px,${top}px,0)`
targetdom.style.backgroundPosition = `-${left}px -${top}px`
largeImg.style.backgroundPosition = `-${left*2}px -${top*2}px`
}
document.onmouseup = function(){
dragdom.onmousemove =false
}
}
function leftrotate(){
num -= 45
img1.style.transform = `rotate(${num}deg)`
}
function rightrotate(){
num += 45
img1.style.transform = `rotate(${num}deg)`
}
function horflip(){
roy += 180
img1.style.transform = `rotateY(${roy}deg)`
}
function verflip(){
rox += 180
img1.style.transform = `rotateX(${rox}deg)`
}
function boostit(){
mul += 0.1
img1.style.transform = `scale(${mul})`
}
function shrinkit(){
mul -= 0.1
img1.style.transform = `scale(${mul})`
}
function showorigin(){
num = 0
rox = 0
roy = 0
mul = 1
img1.style.transform = 'none'
}
</script>
</body>
</html>

上一篇:web移动端总结


下一篇:HTML、CSS卡片悬停交互效果实现