面向对象编程-图片选择添加预览-基于jQuery

我们平时做上传图片的时候 都会用到一个功能 就是选择本地图片+预览

今天我用面向对象编程的思路弄了一个例子

首先 先画好css样式 我弄了一个简单款

给外面的盒子 设置flex布局 然后picture元素不设置宽度设置flex:1

然后添加按钮宽度固定,将input隐藏

这是没有图片点击的时候的效果 下面是基本的html代码

    <div class="box">
      <div class="picturebox" id="picturebox">
        <div class="picture">
          <input type="file" accept="image/*" id="tp" />
          <ul></ul>
        </div>
        <a href="JavaScript:;" class="add">+</a>
      </div>
    </div>

我们给加号按钮添加点击事件,当我们点击的时候 出发input的点击事件

      constructor() {
        that = this;
        $("#picturebox .add").on("click", this.add);
        $("#picturebox ul").on("click", "a", this.deletes);
        $("#picturebox #tp").on("change", this.input_change);
      }
      add() {
        //触发input点击事件
        $("#picturebox #tp").val("");
        $("#picturebox #tp").click();
      }

当input内容被改变的时候 将文件的base64内容赋值给li里面的img 从而实现预览

      input_change() {
        var file = this.files[0];
        console.log("图片选择成功", file);
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function (e) {
          var li =
            '<li><img src="' +
            this.result +
            '" /><a href="JavaScript:;">×</a></li>';
          $("#picturebox ul").append(li);
          that.size_change();
        };
      }

然后我们做个判断 当当前盒子容不下那么多图片的时候 会把加号隐藏,当盒子容的下的时候 加号显示

      size_change() {
        // 48*n 39
        // 距离不够了 就将添加按钮隐藏
        var disparity =
          $("#picturebox").width() - $("#picturebox .add").width();
        var ul_length = $("#picturebox li").length * 48;
        console.log(disparity);
        console.log("ul_length", ul_length);
        if (disparity < ul_length) {
          $(".add").css("display", "none");
        } else {
          $(".add").css("display", "");
        }
      }

这是最后的效果的gif图

附上代码(记得引入jquery.js哦!!)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>图片添加功能</title>
    <script src="./jquery-min.js"></script>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        list-style-type: none;
        text-decoration: none;
      }
      .box {
        width: 205px;
        /* width: 400px; */
      }
      .picturebox {
        padding: 5px;
        border: 1px solid black;
        border-radius: 5px;
        display: flex;
      }
      .picturebox .add {
        display: flex;
        justify-content: center;
        align-items: center;
        font-weight: 900;
        font-size: 25px;
        width: 48px;
        height: 48px;
        border-radius: 10px;
        background-color: black;
        color: aliceblue;
      }
      .picturebox .picture {
        position: relative;
        overflow: hidden;
      }
      .picturebox .picture input[type="file"] {
        position: absolute;
        top: 0;
        padding: 0;
        visibility: hidden;
      }
      .picturebox ul {
        flex: 1;
        height: 48px;
        /* background-color: pink; */
        overflow: hidden;
      }
      .picturebox ul li {
        position: relative;
        float: left;
        width: 48px;
        height: 48px;
        border-radius: 10px;
        /* background-color: blue; */
      }
      .picturebox ul li img {
        width: 100%;
        height: 100%;
        border-radius: 10px;
        /* border: 1px solid black; */
      }
      .picturebox ul li a {
        position: absolute;
        top: 0;
        right: 0;
        width: 20px;
        height: 20px;
        border-radius: 10px;
        background-color: black;
        text-align: center;
        font-size: 16px;
        line-height: 20px;
        font-weight: 900;
        color: aliceblue;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="picturebox" id="picturebox">
        <div class="picture">
          <input type="file" accept="image/*" id="tp" />
          <ul></ul>
        </div>
        <a href="JavaScript:;" class="add">+</a>
      </div>
    </div>
  </body>
  <script>
    var that = null;
    class picture {
      constructor() {
        that = this;
        $("#picturebox .add").on("click", this.add);
        $("#picturebox ul").on("click", "a", this.deletes);
        $("#picturebox #tp").on("change", this.input_change);
      }
      add() {
        //触发input点击事件
        $("#picturebox #tp").val("");
        $("#picturebox #tp").click();
      }
      deletes() {
        //删除被点击的图片
        $(this).parent().remove();
        that.size_change();
        return false; //阻止继续冒泡了
      }
      //input的内容发生改变时候触发的事件
      input_change() {
        var file = this.files[0];
        console.log("图片选择成功", file);
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function (e) {
          var li =
            '<li><img src="' +
            this.result +
            '" /><a href="JavaScript:;">×</a></li>';
          $("#picturebox ul").append(li);
          that.size_change();
        };
      }
      size_change() {
        // 48*n 39
        // 距离不够了 就将添加按钮隐藏
        var disparity =
          $("#picturebox").width() - $("#picturebox .add").width();
        var ul_length = $("#picturebox li").length * 48;
        console.log(disparity);
        console.log("ul_length", ul_length);
        if (disparity < ul_length) {
          $(".add").css("display", "none");
        } else {
          $(".add").css("display", "");
        }
      }
    }
    var a = new picture();
  </script>
</html>

 【博学谷学习记录】超强总结,用心分享

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值