美文网首页js
js上传图片

js上传图片

作者: world_7735 | 来源:发表于2019-03-07 10:57 被阅读8次
    <!DOCTYPE html>
    <html>
    
    <head>
      <meta charset="utf-8">
      <title>layui</title>
      <meta name="renderer" content="webkit">
      <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
      <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
      <link rel="stylesheet" href="./css/reset.css" media="all">
      <!-- <link rel="stylesheet" href="./css/index.css" media="all"> -->
      <script src="./js/jquery-1.9.1.min.js" charset="utf-8"></script>
      <!-- <script src="./js/lib/mavatar.js" charset="utf-8"></script> -->
      <style>
        .home_tabs_cupNoraml {
        width: 200px;
        height: 200px;
        margin: 10px auto 0px auto;
        border: 1px solid black;
      text-align: center;
      position: relative;
    }
    .prompt3_pic {
        width: 100%;
        height: 100%;
        text-align: center;
        position: relative;
    }
    .imgSpan_pic {
      background: url("https://static.98ep.com/Img/printChina/zhan_t.png")  center center no-repeat;
      background-size:100%; 
      display: block;
      width: 100%;
        height: 100%;
    }
    .filepath_pic {
        width: 100%;
        height: 100%;
      opacity: 0;
      z-index: 1;
      position: absolute;
      top:0;
      left: 0;
    
    }
    .img3_pic {
        height: 100%;
        width: 100%;
        display: none;
    }
    .btn_pic{
      position: absolute;
        left: 0px;
        top: 50%;
        border: 1px solid #bbb;
        color: #fff;
        background-color: #2db7f5;
        line-height: 20px;
        border-color: #2db7f5;
        padding: 3px 9px;
    }
      </style>
    </head>
    
    <body>
    
      <div class="home_tabs_cupNoraml">
        <div class="prompt3_pic">
          <span class="imgSpan_pic">
          </span>
          <input type="file" class="filepic filepath_pic" onchange="changepic(this,0)" accept="image/jpg,image/jpeg,image/png,image/PNG">
          <!--当vaule值改变时执行changepic函数,规定上传的文件只能是图片-->
        </div>
        <img src="#" class="img3_pic" />
        <button class="btn_pic">重置</button>
    
      </div>
      <div class="home_tabs_cupNoraml">
        <div class="prompt3_pic">
          <span class="imgSpan_pic">
          </span>
          <input type="file" class="filepic filepath_pic" onchange="changepic(this,1)" accept="image/jpg,image/jpeg,image/png,image/PNG">
          <!--当vaule值改变时执行changepic函数,规定上传的文件只能是图片-->
        </div>
        <img src="#" class="img3_pic" />
        <button class="btn_pic" >重置</button>
      </div>
    </body>
    
    </html>
    <script>
      function changepic(self, len) {
        for (var i = 0; i < $(".home_tabs_cupNoraml").length; i++) {
          if (i == len) {
            $(".prompt3_pic").eq(len).css("display", "none");
            var reads = new FileReader();
            f = document.querySelectorAll('.filepic')[len].files[0];
            reads.readAsDataURL(f);
            reads.onload = function (e) {
              document.querySelectorAll('.img3_pic')[len].src = this.result;
              $(".img3_pic").eq(len).css("display", "block");
            };
          } else if (i == len) {
            $(".prompt3_pic").eq(len).css("display", "none");
            var reads = new FileReader();
            f = document.querySelectorAll('.filepic')[len].files[0];
            reads.readAsDataURL(f);
            reads.onload = function (e) {
              document.querySelectorAll('.img3_pic')[len].src = this.result;
              $(".img3_pic").eq(len).css("display", "block");
            };
          }
        }
      }
      $(".btn_pic").click(function () {
        var len = $(this).parents(".home_tabs_cupNoraml").index();
        console.log(len);
        for (var j = 0; j < $(".home_tabs_cupNoraml").length; j++) {
          $(".prompt3_pic").eq(len).css("display", "block");
          $(".img3_pic").eq(len).css("display", "none");
        }
      });
    </script>
    

    相关文章

      网友评论

        本文标题:js上传图片

        本文链接:https://www.haomeiwen.com/subject/itaqpqtx.html