限时优惠活动
亲爱的苦力吧用户,我们为了回馈新老用户一直以来的鼎力支持,即日起(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. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 图片特效&上传

html5原生js图片集旋转动画展示特效

跟随鼠标轨迹摆动的图片动画特效,比较常见的用在个人网站相册的效果。
  图片特效&上传
 5432  0

jquery支持拖拽上传验证的AJAX文件上传插件

simpleupload是一个轻量级、支持AJAX的文件上传插件,支持拖放、文件验证、上传进度条、AJAX表单提交等。
  图片特效&上传
 8324  0

jquery轻量级旋转木马环绕循环图片旋转动画特效

Acarousel.js是一个极小的图片旋转切换插件,多张图片组成环绕的大转盘,可点击图片进行轮转,还可以点击左右箭头进行切换。
  图片特效&上传
 4335  0

纯js+css仿网易图片轮播特效代码

很实用的幻灯片特效,左右带切换箭头,底部还有可点击切换的方格。
  图片特效&上传
 6476  0

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

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