限时优惠活动
亲爱的苦力吧用户,我们为了回馈新老用户一直以来的鼎力支持,即日起(2025-02-06至2025-03-06)凡是购买苦力吧VIP/充值K币的新老用户,都直接可获得买一送一的优惠馈赠。例如:购买一个月的VIP会员可直接获得两个月的VIP会员;充值100K币可直接获得200K币,以此类推!有任何疑问可联系在线客服,感谢各位用户对苦力吧素材的信任与厚爱,我们将一如既往的给大家上新更多优质的素材源码,祝大家开工大吉、工作顺利、心想事成。

javascript支持自定义文件上传插件

所属分类: 网页特效-图片特效&上传    2024-01-17 11:56:32

javascript支持自定义文件上传插件 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

javascript支持自定义文件上传插件(共3个文件)

    • index.html

使用方法

class CustomUpload extends HTMLElement {
  static observedAttributes = ["imagesrc", "filename","error"];
  constructor() {
    super();
    this.input = this.querySelector("input");
    this.img = this.querySelector("img");
    this.filename = this.querySelector(".file-name");
    this.error = this.querySelector(".error");
    this.maxSize = this.getAttribute("maxsize");
    this.mappedAttributes = {
      imagesrc : {
        default: '',
        target: {
          name: this.img,
          innerHTML: false,
          attribute: 'src'
        }
      },
      filename : {
        default: '',
        target: {
          name: this.filename,
          innerHTML: true,
        }
      },
      error : {
        default: '',
        target: {
          name: this.error,
          innerHTML: true,
        }
      },
    } 
  }
  connectedCallback() {
    this.removeAttribute("nojs");
    this.input.addEventListener('change',(e)=>{
      this.setAttribute('error', '');
      const files = e.target.files
      const isValid = this.validate(files);
      if(isValid) {
        this.onUpload(files);  
      }
    })
  }
  attributeChangedCallback(name, oldValue, newValue) {
    if(this.mappedAttributes[name]) {

      const mapped = this.mappedAttributes[name];
      const target = mapped.target.name;

      mapped.target?.innerHTML ? target.innerHTML = newValue : target.setAttribute(mapped.target.attribute, newValue);
    }
  }
  onUpload(files) {
    const imageSrc = URL.createObjectURL(files[0]);
    const fileName = files[0].name;
    this.setAttribute('filename', "Uploaded file: " + fileName);
    this.setAttribute('imagesrc',imageSrc);
  }
  validate(files) {
    const file = files[0];
    if(file.size > this.maxSize) {
      this.setAttribute("error", "File's weight shouldn't exceed 1 MB")
      return false;
    }
    return true;
  }
}
customElements.define("custom-upload", CustomUpload);

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 图片特效&上传

jquery无限循环幻灯片式自动切换列表插件

一款文本文字列表自动切换特效,带渐变的淡出淡入效果,无缝转换很丝滑!
  图片特效&上传
 2378  0

jquery响应式Coverflow触摸轮换滑块插件

一款支持触屏滑动的3D幻灯片特效,提供了4种轮换模式,很不错!
  图片特效&上传
 2485  0

jquery基于impulse.slider图片3D翻转动画插件

一款简单的图片3D图片翻转插件,充分利用jQuery和CSS3变换/过渡来创建具有3d图片翻转效果的滑块,默认自动播放,可左右切换。
  图片特效&上传
 3166  0

jquery支持自定义的图片放大镜插件

一款图片细节位置放大特效,带三种特效:默认、内部放大、带矩形边框。
  图片特效&上传
 3352  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论