vue-quick-cropper ------------一个vue移动端截图插件

本文介绍如何在Vue项目中安装并使用vue-quick-cropper组件进行图片裁剪。通过示例代码展示了组件的基本配置及事件处理,包括选择图片、预览、裁剪和取消等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装

npm install vue-quick-cropper -S

引入

import VueQuickCropper from 'vue-quick-cropper';
Vue.use(VueQuickCropper)

代码片段

<template>
        <div id="app">

        <div v-if="!visible">
            <!-- 选择照片 -->
            <input ref="file" @change="choiceImg" type="file">
        </div>
        <!-- 照片裁剪部分 -->
        <div class="main" v-if="visible">
            <div class="header">
                <div class="button" @click="confirm">使用</div>
                <div class="button cancel" @click="cancel">取消</div>

            </div>
            <div class="content">
                <quick-cropper ref="cropper" :img-src="imgSrc" @finish="finish">
                </quick-cropper>
            </div>
        </div>
    </div>
</template>
<script>
    export default {
  el: '#app',
  data: {
    imgSrc: "",    // 图片数据
    visible:false,  // 剪切框展示
  },
  methods: {
    // 获得头像的base64和二进制
    finish(base64,data){
      console.log(base64,'图片base64')
      console.log(data,'图片二进制')
    },  
    // 确定使用
    confirm() {
      this.$nextTick(() => {
          // 确认截图
        this.$refs.cropper.confirm()
      })
    },
    // 取消
    cancel(){
      this.visible = false
    },
    // 选择img回调
    choiceImg(e) {    
      this.visible = true
      const file = e.target.files[0]
      const reader = new FileReader();
      reader.readAsDataURL(file)
      reader.onload = (e) => {
        this.imgSrc = reader.result
        // imgSrc已获取,�开始渲染图片�
        this.$refs.cropper.init()
      }
    },
  }
};
</script>
<style>
    * {
    margin: 0;
    padding: 0;
}

html,
body,
#app {
    height: 100%;
    overflow: hidden;
}

.item {
    height: 200px;
    background: lightgray;
    border: 2px solid green;
    overflow: hidden;
}

.main {
    height:100%;
    box-sizing: border-box;
    padding-top: 44px;
}

.content {
    height: 100%;
}

.header {
    position: fixed;
    top: 0;
    right: 0;
    height: 44px;
    width: 100%;
    background: #303030;
}

.button {
    float: right;
    height: 44px;
    line-height: 44px;
    font-size: 14px;
    margin-right: 15px;
    color: green;
}
.cancel{
    float:left;
    margin-left:15px;
}
</style>    

配置
这里写图片描述

兼容性不是很好…

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值