vue+input file上传图片并以圆形展示代码分享(升级版增加遮罩)

本文分享了一个使用Vue.js实现图片上传的功能,并将上传的图片以圆形展示的代码实现,同时还增加了遮罩效果。通过html、css和javascript的结合,为用户提供了一种优雅的图片预览体验。

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

html

<div class="upload">
     <img id="img" width="100%" height="90%" :src="oneUrl" alt="头像">
     <input type="file" name="file" id="file" @change="getImgURL" title=''>
</div>

升级版html(新增遮罩)

<div class="upload">
    <img id="img" width="100%" height="90%" :src="oneUrl" alt="头像">
    <div class="mask">点击上传头像</div>
    <input type="file" name="file" id="file" @change="getImgURL" title="">
</div>

css

.upload{
      width: 80px;
      height: 80px;
      border: #fff 1px solid;
      overflow: hidden;
      border-radius: 40px;
      position: relative;
//这是为了居中
      left: calc(50% - 25px);
      #file{
        width: 100%;
        height: 100%;
//将按钮覆盖图片
        position: absolute;
        right: 0;
//透明度调为全透明
        opacity:0;
      }
    }

升级版css

.upload{
      width: 100px;
      height: 100px;
      border: #fff 1px solid;
      overflow: hidden;
      border-radius: 50px;
      position: relative;
      left: calc(50% - 50px);
      #file{
        width: 100%;
        height: 100%;
        position: absolute;
        right: 0;
        opacity:0;
      }
      .mask{
        line-height: 100px;
        text-align: center;
        color: #fff;
        position: absolute;
        bottom: 0px;
        left: 0px;
        width: 100%;
        height: 0px;
        background-color: transparent;
      }
    }
    .upload:hover .mask{
      height: 100%;
      transition: height 500ms;
      border-radius: 4px;
      background-color: rgba(0, 0, 0, 0.4);
    }

js(无变化)

getImgURL (event) {
      // 获取上传的文件对象file
      let file = event.target.files[0]
      // 获取文件的后缀名,用以判断文件类型是否为图片
      const fileType = file.name.substring(file.name.lastIndexOf('.') + 1)
      if (fileType === 'jpg' || fileType === 'png' ||fileType === 'jpeg' ||fileType === 'JPG') {
        // 判断文件大小
        const size = file.size / 1024 / 1024
        if (size > 1) {//size的单位为M
          this.$message({
            type: 'warning',
            message: '文件大小不能超过1M'
          })
          return false
        }
        // FileReader对象用于读取文件
        const r = new FileReader()
        // onload方法将会在文件读取完成后运行
        r.onload = () => {
          // 将base64编码赋值给页面img的src绑定的值
          this.oneUrl = r.result
        }
        // 读取blob或file文件对象,其中r.result是读取文件的base64编码
        r.readAsDataURL(file)
      } else {
        this.$message({
          type: 'warning',
          message: '文件格式不符,请重新选择'
        })
        return false
      }
    },

要实现在Vue中唤起照相机并添加遮罩层,你需要使用`<input type="file">`元素和CSS样式。以下是一个简单的实现: 1.Vue组件中添加一个`<input>`元素,并设置它的类型为“file”和样式为不可见: ``` <input type="file" ref="fileInput" style="display:none"> ``` 2. 添加一个按钮或其他元素,当点击时触发`<input>`元素的点击事件: ``` <button @click="openCamera">打开相机</button> ``` 3.Vue组件中定义`openCamera`方法,在该方法中触发`<input>`元素的点击事件,并通过CSS样式添加遮罩层: ``` methods: { openCamera() { // 触发文件选择器的点击事件 this.$refs.fileInput.click(); // 在页面上添加遮罩层 document.body.style.overflow = 'hidden'; document.body.style.position = 'relative'; const mask = document.createElement('div'); mask.style.position = 'absolute'; mask.style.top = 0; mask.style.left = 0; mask.style.width = '100%'; mask.style.height = '100%'; mask.style.backgroundColor = 'rgba(0,0,0,0.5)'; mask.style.zIndex = 999; document.body.appendChild(mask); } } ``` 4. 监听`<input>`元素的change事件,获取用户选择的文件并移除遮罩层: ``` mounted() { this.$refs.fileInput.addEventListener('change', this.handleFileChange); }, methods: { handleFileChange(event) { const file = event.target.files[0]; // 处理文件 // ... // 移除遮罩层 document.body.style.overflow = ''; document.body.style.position = ''; const mask = document.querySelector('.mask'); document.body.removeChild(mask); } } ``` 5. 最后,在CSS中定义遮罩层的样式: ``` .mask { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.5); z-index: 999; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值