input的type为file类型时,按钮样式的处理

当input的type为file类型时,css是无法对input的文字及相关样式进行处理的,常见有两种方式来间接设置上传按钮的样式。

方法一、

1、增加用于设置按钮样式的元素

2、对input设置透明度为透明,并设为绝对定位且宽高等于新增按钮元素的宽高,可使点击区域覆盖按钮的区域,也使其浮于新增按钮元素的上方,在视觉上等同于点击我们设置的按钮。

该方法的优点是:简洁高效,无需新增JS代码来间接触发input

实例:

<li>
   <input 
        type="file" 
        class="upload" 
        @change="addImg" 
        ref="inputer" 
        multiple 
        accept="image/png,image/jpeg,image/gif,image/jpg"
   />
   <div class="add">
      <i class="iconfont icon-plus">+</i>
      <p>点击上传</p>
   </div>
</li>


 li{
   position: relative;
   width: 118px;
   height: 118px;
   font-size: 14px;
   display: inline-block;
   padding: 10px;
   margin-right: 25px;
   border: 1px dashed #aaa;
   text-align: center;
   vertical-align: middle;
   &:hover{
    cursor: pointer;
  }
 }
 li .upload{
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  width: 118px;
  height: 118px;
  opacity: 0;
  &:hover{
    cursor: pointer;
  }
}
 li:hover{
   border-color:#3594F4;
 }
 .add{
   display: block;
   background-color: #F8F8F8;
   color: #666;
   height: 94px;
   padding: 8px 0;
 }
 li:hover .add{
  background-color: #3594F4;
  color: #ffffff;
}
.add .iconfont{
  padding: 10px 0;
  font-size: 40px;
}

 

 

 

方法二、

通过点击按钮元素给input发送事件

使用的是dispatchEvent()方式

该方法的缺点是新增了本可以减少的JS代码,且兼容性可能存在问题

直接看实例:

<div class="container">
    <div class="add-img" @click="addImg">
        <i class="icon">+</i>
        <p class="add-tip">{{label}}</p>
    </div>
    <input type="file" class="inputer" ref="inputer">
</div>


methods:{
      addImg(){
        var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false; 
        if(ie){
          this.$ref.click()
        }else{
          var e = document.createEvent("MouseEvents");
          e.initEvent("click", true, true);  
          this.$refs.inputer.dispatchEvent(e);
        }
    }
}


.container{
  position: relative;
  width:118px;
  height:118px;
  margin:20px auto;
  overflow: hidden;
  .add-img{
    width:118px;
    height:118px;
    border: 1px dashed #999;
    border-radius: 16px;
    background-color:#F8F8F8;
    &:hover{
      cursor: pointer;
    }
    .icon{
      font-size: 60px;
      line-height: 1.3
    }
  }
  .inputer{
    position: absolute;
    top:0;
    left:0;
    opacity: 0
  }
}

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要修改input type=file按钮样式,可以使用CSS来实现。但是由于input type=file的设计,只有很少的CSS属性可以应用于它。所以,我们需要使用一些技巧才能改变它的外观。以下是一些实现方法: 方法一:使用label元素代替input元素,并使用CSS样式来美化label元素。 HTML代码: ``` <label for="file-upload" class="custom-file-upload"> <i class="fa fa-cloud-upload"></i>选择文件 </label> <input id="file-upload" type="file"/> ``` CSS代码: ``` .custom-file-upload { display: inline-block; padding: 6px 12px; cursor: pointer; border: 1px solid #ccc; border-radius: 4px; } .custom-file-upload:hover { background-color: #f5f5f5; } .custom-file-upload i { margin-right: 5px; } ``` 方法二:使用伪元素和背景图片来美化input元素。 HTML代码: ``` <input type="file" class="custom-file-input"/> <label class="custom-file-label" for="custom-file-input">选择文件</label> ``` CSS代码: ``` .custom-file-input { position: absolute; left: -9999px; } .custom-file-label { display: inline-block; padding: 6px 12px; cursor: pointer; border: 1px solid #ccc; border-radius: 4px; font-size: 14px; font-weight: normal; background-image: url('file-upload.png'); background-repeat: no-repeat; background-position: center; background-size: 16px; padding-left: 24px; } .custom-file-label:hover { background-color: #f5f5f5; } ``` 其中,file-upload.png是一个上传图标的图片。 以上两种方法都可以实现美化input type=file按钮的效果。您可以根据自己的需求选择其中一种方法进行使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值