• vue 图片放大、缩小、旋转、滚轮操作图片放大缩小


    在这里插入图片描述

    组件

    <template>
      
      <div class="imgCont"
           ref="imgCont"
           @mousewheel.prevent="rollImg($event)"
           >
        <div class="iconBtn">
          <span class="refreshBtn">
            <i class="el-icon-zoom-out"
               @click.stop="outImg('out')">i>
            <i class="el-icon-zoom-in"
               @click.stop="outImg('in')">i>
    
            <i class="el-image-viewer__actions__divider">i>
              <i :class="mode.icon" @click="toggleMode">i>
              <i class="el-image-viewer__actions__divider">i>
            <i @click.stop="rotate('left')"
               class="el-icon-refresh-left">i>
            <i @click.stop="rotate('right')"
               class="el-icon-refresh-right right">i>
            <slot name="addIcon">slot>
          span>
        div>
        <img class="bigImage"
             ref="imgDiv"
             @mousedown.stop.prevent="moveImg($event)"
             id="img"
             :src="url" />
      div>
    template>
    
    <script>
    const Mode = {
      CONTAIN: {
        name: "contain",
        icon: "el-icon-full-screen"
      },
      ORIGINAL: {
        name: "original",
        icon: "el-icon-c-scale-to-original"
      }
    };
    export default {
      name: 'imgDeal',
      props: {
        url: {},
      },
      components: {},
      data () {
        return {
          mode: Mode.CONTAIN,
          // 图片参数
          params: {
            zoomVal: 1,
            left: 0,
            top: 0,
            currentX: 0,
            currentY: 0,
          },
          deg: 0,
        };
      },
      computed: {
    
      },
      watch: {
    
      },
      created () {
        this.restImg();
      },
      mounted () {
    
      },
      methods: {
        //1:1自适应
        toggleMode () {
          const modeNames = Object.keys(Mode);
          const modeValues = Object.values(Mode);
          const index = modeValues.indexOf(this.mode);
          const nextIndex = (index + 1) % modeNames.length;
          this.mode = Mode[modeNames[nextIndex]];
          if (this.mode.name == 'original') {
            this.originalFunc();
          } else {
            this.restImg();
          }
        },
        // mode==original 默认放大图片
        originalFunc () {
          this.params.zoomVal = 2;
          this.restFunc();
        },
        // 初始化数据,重置数据
        restImg () {
          this.params.zoomVal = 1;
          this.restFunc();
          this.mode = Mode['CONTAIN'];
        },
        restFunc () {
          this.params.left = 0;
          this.params.top = 0;
          this.params.currentX = 0;
          this.params.currentY = 0;
          this.deg = 0;
          if (this.$refs.imgDiv) {
            let img = this.$refs.imgDiv;
            img.style.transform = `translate(-50%, -50%) scale(${this.params.zoomVal}) rotate(${this.deg}deg)`;
            img.style.left = '50%';
            img.style.top = '50%';
          }
        },
        // 图片滚动放大
        rollImg (event) {
          this.params.zoomVal += event.wheelDelta / 1200;
          this.rollFunc()
        },
        outImg (flag) {
          if (flag == 'out') {
            this.params.zoomVal -= 0.2;
          } else {
            this.params.zoomVal += 0.2;
          }
          this.rollFunc()
        },
        rollFunc () {
          let e = this.$refs.imgDiv;
          if (this.params.zoomVal >= 0.2) {
            e.style.transform = `translate(-50%, -50%) scale(${this.params.zoomVal}) rotate(${this.deg}deg)`;
          } else {
            this.params.zoomVal = 0.2;
            e.style.transform = `translate(-50%, -50%) scale(${this.params.zoomVal}) rotate(${this.deg}deg)`;
            return false;
          }
        },
        
        // 图片旋转
        rotate (type) {
          let res = this.$refs.imgDiv;
          this.deg = type == 'right' ? this.deg + 90 : this.deg - 90
          res.style.transform = `translate(-50%, -50%) scale(${this.params.zoomVal}) rotate(${this.deg}deg)`
        },
        // 图片移动
        moveImg (e) {
          // 获得该时间触发的时间戳
          let mouseDate = new Date().getTime();
          this.$emit('getMouseDate',mouseDate)
          e.preventDefault()
          // 获取元素
          let imgWrap = this.$refs.imgCont
          let img = this.$refs.imgDiv
          let x = e.pageX - img.offsetLeft
          let y = e.pageY - img.offsetTop
          // 添加鼠标移动事件
          imgWrap.addEventListener('mousemove', move)
          function move (e) {
            img.style.left = e.pageX - x + 'px'
            img.style.top = e.pageY - y + 'px'
          }
          // 添加鼠标抬起事件,鼠标抬起,将事件移除
          img.addEventListener('mouseup', () => {
            imgWrap.removeEventListener('mousemove', move)
          })
          // 鼠标离开父级元素,把事件移除
          imgWrap.addEventListener('mouseout', () => {
            imgWrap.removeEventListener('mousemove', move)
          })
        },
      },
    };
    script>
    
    <style scoped lang="scss">
    .imgCont {
      text-align: center;
      vertical-align: middle;
      position: relative;
      overflow: hidden;
      width: 100%;
      height: 100%;
      .iconBtn {
        position: absolute;
        left: 0;
        bottom: 0;
        height: 35px;
        line-height: 35px;
        background-color: rgba(0, 0, 0, 0.2);
        color: #fff;
        width: 100%;
        z-index: 100;
        font-size: 20px;
        .refreshBtn {
          i {
            cursor: pointer;
            margin: 0 10px;
          }
        }
      }
      .bigImage {
        max-width: 100%;
        max-height: 100%;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        cursor: move;
        margin: 0 auto;
      }
    }
    style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211

    组件使用

    import ImgDeal from "@/components/imgDeal.vue";
    components: {
        ImgDeal 
      },
    
    • 1
    • 2
    • 3
    • 4
    <ImgDeal ref="imgDeal" :url="imgUrl">ImgDeal>
    
    • 1
  • 相关阅读:
    tcp满开始和拥塞避免
    局域网监控软件如何防止数据泄密
    《算法笔记》树与二叉树专题
    自动化安装脚本(Ansible+shell)
    Insight h2database SQL like 查询
    SQL 行列转换
    three.js顶点颜色插值THREE.VertexColors无效
    AI数据标注猿知识星球私域社区开始招募啦
    软件设计模式系列之九——桥接模式
    Leetcode 877. 石子游戏
  • 原文地址:https://blog.csdn.net/dreamy_wx/article/details/128185101