H5上传图片

关于手机端上传图片找了许久没有心仪的插件决定自已写一个简单的上传图片插件
页面描述:
1.上传图片到后台
2.图片、大小、文件数、格式判断
3.上传后显示图片,预览图片,移除图片

用到的JS是 Jquery,mui.js,mui.css,zoom.js,preimg.js 如果百度找不到js的,可以回复我,当然还有阿里图标,阿里图标自行解决。

  <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="./node_modules/mui/css/mui.min.css">
  <link rel="stylesheet" href="./node_modules/fonts/iconfont.css">
</head>
<style>
  .box {
      /* 有间隙的问题 */
      font-size: 0px; 
      margin-top: 30px;
    }

    .input {
      opacity: 0;
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
    }
    
    .i {
      position: relative;
      width: 120px;
      height: 120px;
      border: 1px solid #cccccc;
      font-size: 100px;
      text-align: center;
      line-height: 100px;
      display: inline-block;
      vertical-align:bottom;
      margin-right:5px;
      margin-bottom:5px;
    }

    #img {
      display: inline-block;
      width: 120px;
      height: 120px;
      border: 1px solid sandybrown;
      margin: 10px;
    }

    .placeholder {
      width: 120px;
      height: 120px;
      border: 1px solid #cccccc;
      display: inline-block;
      text-align: center;
      vertical-align: middle;
      margin-right:15px;
      margin-bottom:5px;
      position: relative;
    }
    
    .placeholder > i {
      position: absolute;
      left: 50%;
      top: 30%;
      margin-left: -12px;
    }

     .placeholder > p {
      position: absolute;
      left: 50%;
      top: 65%;
      transform: translate(-50%, -50%);
      color: #000;
     }
 
    .uploadImg {
      width: 100%;
      height: 100%;
    }

    .closeImg {
      width: 20px;
      height: 20px;
      background-color: red;
      text-align: center;
      line-height: 30px;
      color: #fff;
      font-weight: 500;
      border-radius: 50%;
      position: absolute;
      top: -10px;
      right: -10px;
    }

    .closeImg i {
      width: 100%;
      height: 100%;
      font-weight: 700;
      text-align: center;
      font-size: 20px;
    }

    .errorImg {
      width: 100%;
      height: 20px;
      color: #fff;
      background-color: #ff3c3c;
      font-size: 12px;
      line-height: 20px;
      position: absolute;
      top: 0px;
      left: 0px;
    }

    .mui-preview-image.mui-fullscreen {
    position: fixed;
    z-index: 20;
    background-color: #000;
}
.mui-preview-header,
.mui-preview-footer {
    position: absolute;
    width: 100%;
    left: 0;
    z-index: 10;
}
.mui-preview-header {
    height: 44px;
    top: 0;
}
.mui-preview-footer {
    height: 50px;
    bottom: 0px;
}
.mui-preview-header .mui-preview-indicator {
    margin: 0 auto;
    display: block;
    line-height: 25px;
    color: #fff;
    text-align: center;
    margin: 15px auto 4;
    width: 70px;
    background-color: rgba(0, 0, 0, 0.4);
    border-radius: 12px;
    font-size: 16px;
}
.mui-preview-image {
    display: none;
    -webkit-animation-duration: 0.5s;
    animation-duration: 0.5s;
    -webkit-animation-fill-mode: both;
    animation-fill-mode: both;
}
.mui-preview-image.mui-preview-in {
    -webkit-animation-name: fadeIn;
    animation-name: fadeIn;
}
.mui-preview-image.mui-preview-out {
    background: none;
    -webkit-animation-name: fadeOut;
    animation-name: fadeOut;
}
.mui-preview-image.mui-preview-out .mui-preview-header,
.mui-preview-image.mui-preview-out .mui-preview-footer {
    display: none;
}
.mui-zoom-scroller {
    position: absolute;
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    align-items: center;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    justify-content: center;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    width: 100%;
    height: 100%;
    margin: 0;
}
.mui-zoom {
    -webkit-transform-style: preserve-3d;
    transform-style: preserve-3d;
}
.mui-slider .mui-slider-group .mui-slider-item img {
    width: auto;
    height: auto;
    width: 100%;
    height: 100%;
}
.mui-android-4-1 .mui-slider .mui-slider-group .mui-slider-item img {
    width: 100%;
}
.mui-android-4-1 .mui-slider.mui-preview-image .mui-slider-group .mui-slider-item {
    display: inline-table;
}
.mui-android-4-1 .mui-slider.mui-preview-image .mui-zoom-scroller img {
    display: table-cell;
    vertical-align: middle;
}
.mui-preview-loading {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    display: none;
}
.mui-preview-loading.mui-active {
    display: block;
}
.mui-preview-loading .mui-spinner-white {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -25px;
    margin-top: -25px;
    height: 50px;
    width: 50px;
}
.mui-preview-image img.mui-transitioning {
    -webkit-transition: -webkit-transform 0.5s ease, opacity 0.5s ease;
    transition: transform 0.5s ease, opacity 0.5s ease;
}
@-webkit-keyframes fadeIn {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}
@keyframes fadeIn {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}
@-webkit-keyframes fadeOut {
    0% {
        opacity: 1;
    }
    100% {
        opacity: 0;
    }
}
@keyframes fadeOut {
    0% {
        opacity: 1;
    }
    100% {
        opacity: 0;
    }
}

.placeholder .successImgIcon {
  position:absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 40px;
  color: #85CE61;
  margin: 0px;
}

.placeholder .errorImgIcon {
  position:absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 40px;
  color: #000;
  font-weight: 700;
  margin: 0px;
}

.mui-toast-message {
  font-size: 13px;
}
  </style>

<body>

  <div class="box">
    <i class="mui-icon mui-icon-plusempty i"><input class="input" type="file" name="myfile" id="fileId"></i>
  </div>

  <script src="./node_modules/jquery/dist/jquery.min.js"></script>
  <script src="./node_modules/mui/js/mui.js"></script>
  <script src="./node_modules/mui/js/zoom.js"></script>
  <script src="./node_modules/mui/js/preImg.js"></script>
  <script>
    $(function () {
      mui.previewImage();
      var disUploadNum = 1
      $('.input').change(function (e) {
        // 限制用户前一张图片还没有上传完,又要上传第二张图片的问题
        if( disUploadNum === 0 ) {
          mui.toast('亲,请等待图片上传结果', {
            duration: 'long',
            type: 'div'
          })
          return false
        }
        disUploadNum = 0;
        var fileIn = e.target.files[0];
        var file = $('#fileId').val();
        var _input = $('#fileId');
        var fileNameArr = $('#fileId').val().split('\\')
        var fileSize = document.getElementById('fileId').files[0].size
        var fileName = fileNameArr[fileNameArr.length - 1]
        var formatStr = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length)
        if (formatStr !== 'png' && formatStr !== 'gif' && formatStr !== 'jpeg' && formatStr !== 'jpg') {
          mui.toast('亲,上传文件的格式不正确', {
            duration: 'long',
            type: 'div'
          })
          disUploadNum = 1;
          return false
        }
        if (fileSize > 2000000) {
          mui.toast('亲,文件太大啦,请上传小于2M的图片', {
            duration: 'long',
            type: 'div'
          })
          disUploadNum = 1;
          return false
        }
        console.log($('.placeholder').length);
        if ($('.placeholder').length >= 3) {
          mui.toast('亲,最多上传3张文件', {
            duration: 'long',
            type: 'div'
          })
          disUploadNum = 1;
          return false
        }
        $('<div class="placeholder"><i class="mui-spinner mui-spinner"></i><p>上传中..</p></div>').prependTo($(
          ".box"))
        setTimeout(() => {
          var reader = new FileReader();
          reader.readAsDataURL(fileIn);
          reader.onload = function (e) {
            $('<img class="uploadImg"  src=' + e.target.result +
              '>').prependTo($('.placeholder')[
              0])
            $('.placeholder').eq(0).find('.uploadImg').css({
              'opacity': '0.2'
            });
            setTimeout(() => {
              request()
            }, 500);
          }
          var formData = new FormData();
          formData.append("file", this.files[0]);

          function request() {
            $.ajax({
              url: '地址',
              type: 'post',
              data: formData,
              datatype: 'JSON',
              processData: false,
              headers: {
               token: 123456
             }
              contentType: false,
              async: true,
              cache: false,
              success: function (data) {
                if (data.code === 200) {
                  _input.val('');
                  $('.placeholder').eq(0).find('i').removeClass('mui-spinner')
                  $('.placeholder').eq(0).find('i').addClass('iconfont icon-chenggong successImgIcon')
                  $('.placeholder').eq(0).find('p').remove()
                  setTimeout(() => {
                    $('.placeholder').eq(0).find('.successImgIcon').remove()
                    $('<div class="closeImg"><i class="mui-icon mui-icon-closeempty"></i></div>')
                      .prependTo(
                        $('.placeholder')[0])
                    $('.placeholder').eq(0).find('.uploadImg').attr({
                      'src': data.url,
                      'data-preview-src': '',
                      'data-preview-group': '2'
                    }).css({
                      'opacity': '1'
                    })
                    disUploadNum = 1
                  }, 500);
                  if ($('.placeholder').length >= 3) {
                    $('.i').fadeOut()
                  }
                }
                if (data.code === 500) {
                  _input.val('');
                  $('.placeholder').eq(0).find('i').removeClass('mui-spinner')
                  $('.placeholder').eq(0).find('i').addClass(
                    'iconfont icon-shangchuanshibai errorImgIcon')
                  $('.placeholder').eq(0).find('p').remove()
                  $('<div class="closeImg"><i class="mui-icon mui-icon-closeempty"></i></div>').prependTo(
                    $('.placeholder')[0])
                  $('<div class="errorImg">上传失败</div>').prependTo($('.placeholder')[0])
                  disUploadNum = 1
                  mui.toast('亲,上传错误,请重新上传', {
                    duration: 'long',
                    type: 'div'
                  })
                  return false
                }
              },
              error: function () {
                _input.val('');
                $('.placeholder').eq(0).find('i').removeClass('mui-spinner')
                $('.placeholder').eq(0).find('i').addClass(
                  'iconfont icon-shangchuanshibai errorImgIcon')
                $('.placeholder').eq(0).find('p').remove()
                $('<div class="closeImg"><i class="mui-icon mui-icon-closeempty"></i></div>').prependTo(
                  $('.placeholder')[0])
                $('<div class="errorImg">上传失败</div>').prependTo($('.placeholder')[0])
                disUploadNum = 1
                mui.toast('亲,上传错误,请检查您的网络是否连接', {
                  duration: 'long',
                  type: 'div'
                })
                return false
              }
            });
          }
        }, 0.00001);
      })

      $('.box').on('click', '.closeImg', function () {
        $(this).parent().remove()
        $('.i').fadeIn()
      })
    })
  </script>
</body>

</html>

最近写的插件可能有些功能没有考虑到的及时评论我,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值