这两天一直在写上传头像那一部分,需要用到cropper裁剪,算起来这是我第二次用了,本来以为会比较顺手,结果一直报这个错。
本来以为是jq和cropper的引入顺序问题,结果调好了还在报这个错误,然后又考虑到会不会是jq和cropper的版本不对应,又知道cropper和cropper.js不一样,甚至还有一个jquery-cropper,去cropper.js官网看也不懂,看人家做出来的效果又觉得难,最后还是认真看了看别人总结翻译官网文档的博客,才知道本地引的和用cdn链接的js,对应的cropper写法完全不一样。按照别人翻译出来的效果就是很简单的样子,没有弹窗预览效果和翻转图片什么的,又觉得自己写不出来,就还是找别人写好的样式,结果又开始报上面的错,最后迫不得已,才决定自己按照别人的样式写一下,写完才觉得其实很简单,一整个麻住了,感觉自己浪费太多时间去cv别人的去去反复解决那一个报错,还是把这个经过和代码写下来提醒提醒自己,不能还没有开始做就觉得自己不能做。
<div class="changeHeadBtn">
<p class="upBtnFont"><i class="iconfont icon-banbenshengji"></i>更换头像</p>
</div>
<div class="box">
<div class="cropperContent">
<div class="cropperTop">
<div class="seleceImgBtn">
<p class="upInputFont">选择图片</p>
<input type="file" class="cropperInput"/>
</div>
<p class="scloseCropper">x</p>
</div>
<div class="cropperCenter">
<div class="centerLeft">
</div>
<div class="centerRight">
<p>图片预览:</p>
<div class="small"></div>
<div class="small" style="border-radius: 100%;"></div>
</div>
</div>
<div class="cropperBottom">
<div class="manBtn">
<div class="resetBtn bottomBtn">复位</div>
<div class="rotateBtn bottomBtn">旋转</div>
<div class="huanXiangBtn bottomBtn">换向</div>
</div>
<div class="crop bottomBtn">确定</div>
</div>
</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.12/cropper.min.js"></script>
css
.changeHeadBtn {
width: 170px;
height: 45px;
background-color: #ff7e88;
color: white;
position: relative;
margin: 0 auto;
letter-spacing: 1px;
line-height: 45px;
margin-top: 20px;
}
.changeHeadBtn > p{
display: inline-block;
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
margin: 0px;
text-align: center;
}
.box {
position: fixed;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
background-color: rgba(0, 0, 0, 0.5);
display: none;
}
.box .cropperContent {
display: flex;
flex-direction: column;
width: 700px;
height: 600px;
background-color: white;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-radius: 20px;
padding: 20px;
}
.box .cropperContent .cropperTop {
display: flex;
justify-content: space-between;
}
.box .cropperContent .cropperTop .seleceImgBtn {
width: 110px;
height: 40px;
background-color: #ff7782;
outline-color: white;
position: relative;
text-align: center;
}
.box .cropperContent .cropperTop .seleceImgBtn .upInputFont {
text-decoration: none;
margin: 0;
padding: 0;
box-sizing: border-box;
color: white;
text-align: center;
position: relative;
top: 10px;
}
.box .cropperContent .cropperTop .seleceImgBtn .cropperInput {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
cursor: pointer;
display: inline-block;
z-index: 99;
opacity: 0;
}
.box .cropperContent .cropperTop .scloseCropper {
font-size: 20px;
margin-right: 10px;
margin-bottom: 5px;
color: #777777;
cursor: pointer;
}
.box .cropperContent .cropperCenter {
flex: 1;
display: flex;
margin: 20px 0px;
}
.box .cropperContent .cropperCenter .centerLeft {
flex: 1;
border: solid 1px #8a8a8a;
margin-right: 20px;
width: 440px;
height: 430px;
}
.box .cropperContent .cropperCenter .centerLeft > img {
}
.box .cropperContent .cropperCenter .centerRight {
color: #777777;
}
.box .cropperContent .cropperCenter .centerRight p {
display: inline-block;
}
.box .cropperContent .cropperCenter .centerRight .small {
width: 200px;
height: 200px;
overflow: hidden;
}
.box .cropperContent .cropperCenter .centerRight .smallRound {
width: 200px;
height: 200px;
overflow: hidden;
border-radius: 100%;
}
.box .cropperContent .cropperBottom {
display: flex;
justify-content: space-between;
padding: 10px 0px;
}
.bottomBtn {
display: inline-block;
width: 60px;
height: 30px;
line-height: 30px;
margin-right: 10px;
text-align: center;
background-color: #ff7782;
cursor: pointer;
color: white;
}
js
$('.upBtnFont')[0].onclick = function(){
$('.box')[0].style.display = 'block'
}
var cropper = null
$('.cropperInput')[0].onchange = function(){
fileToBase64(this)
cropper = new Cropper(image, {
aspectRatio: 1 / 1,
viewMode:1,
preview:'.small',
guides:false,
crop: function (e) {
}
});
}
function fileToBase64(a){
var txt=a.files[0];
formData = new FormData()
formData.append('img[]',txt);
var src=window.URL.createObjectURL(txt);
$('.centerLeft')[0].innerHTML = `
<img src='${src}' id='image'/>
`
let headImg = $('.centerLeft')[0].children[0]
headImg.onload = function(){
let imgWidth = headImg.offsetWidth
let imgHeight = headImg.offsetHeight
if(imgWidth > imgHeight){
headImg.style.width = '100%'
headImg.style.height = 'auto'
}else{
headImg.style.width = 'auto'
headImg.style.height = '100%'
}
}
}
$('.resetBtn')[0].onclick = function(){
if(ifInput()){
cropper.reset()
}
}
$('.rotateBtn')[0].onclick = function(){
if(ifInput()){
cropper.rotate(90)
}
}
var scale = true
$('.huanXiangBtn')[0].onclick = function(){
if(ifInput()){
if(scale){
cropper.scale(-1,1)
scale = false
}else{
cropper.scale(1,1)
scale = true
}
}
}
function ifInput(){
if($('.cropperInput')[0].files[0]) return true
else return false
}
$('.crop')[0].onclick = function(){
if(ifInput()){
cropper.getCroppedCanvas().toBlob(b => {
console.log(b)
var formData = new FormData()
formData.append('multipartFile',b,'temp.jpg');
})
}
}
实例化Cropper接收的参数有很多,基本上是对画布的处理,这篇博客写得很详细:https://blog.csdn.net/weixin_38023551/article/details/78792400
cropper的cdn链接:https://cdnjs.com/libraries/cropperjs
下载cropper的地址:https://github.com/fengyuanchen/cropperjs/tree/main/docs
结束结束。
两天啥也没干,净解决这个cropper is not a function。嗐!!!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)