zoukankan      html  css  js  c++  java
  • vue项目中图片预览旋转功能

    最近项目中需要在图片预览时,可以旋转图片预览,在网上找了下,发现有一款功能强大的图片组件:viewerjs. git-hup: https://github.com/fengyuanchen/viewerjs
    在git上看了下,有很多功能,不过我的项目只需要做个图片旋转功能,引入这个组件感觉大材小用了,于是自己写了个简易版的,因为我们只是查看而已,没什么要求.如果你需要比较精确的图片旋转功能,可以使用这个viewerjs组件
     
    本功能的git-hup地址: https://github.com/shengbid/vue-demo  这个文件是平时练习的项目,里面还有一些我写的其他博客的源码,有需要可以下载看看

    功能描述: 一个图片预览框,三个操作按钮: 上一张,下一张,旋转; 点击上一张下一张切换图片,点击旋转顺时针旋转图片,每次旋转90°,旋转后记住当前图片的旋转角度,点击上一张下一张再次切换回来时,需要旋转到上一次旋转的角度

    代码:这是一个单独的组件,图片数据可以写在props中传值
    <template>
      <div class="rotate_contanier">
        <div class="header">
          <span @click="handleImg(1)">上一张</span>
          <span @click="handleImg(2)">下一张</span>
          <span @click="rotate">旋转</span>
        </div>
        <div class="picture_contaner">
          <div class="img_box" ref="rotate">
            <img :src="fileInfo.fileUrl" alt="">
          </div>
        </div>
      </div>
    </template>
    
    <script>
    export default {
      props: {
        
      },
      data() {
        return {
          pictureList: [
            {fileUrl: 'http://mp.ofweek.com/Upload/News/Img/member645/201711/17170046839337.jpg'},
            {fileUrl: 'http://img2.imgtn.bdimg.com/it/u=1239081181,1433383585&fm=26&gp=0.jpg'},
            {fileUrl: 'http://img1.imgtn.bdimg.com/it/u=3502008475,4132115356&fm=26&gp=0.jpg'},
            {fileUrl: 'http://img12.360buyimg.com/n5/s450x450_jfs/t9952/98/2269407420/279171/6137fe2f/59f28b2bN6959e086.jpg'},
            {fileUrl: 'http://img2.imgtn.bdimg.com/it/u=2681505513,370839281&fm=26&gp=0.jpg'},
            {fileUrl: 'http://img2.imgtn.bdimg.com/it/u=1153377230,3978893548&fm=26&gp=0.jpg'}
          ],
          fileInfo: {
            fileUrl: '',
            deg: 0
          },
          currentPage: 0
        }
      },
      created() {
        // 设置图片初始旋转角度
        this.pictureList.forEach(item => {
          item.deg = 0
        })
        this.fileInfo = this.pictureList[this.currentPage]
      },
      mounted() {
       
      },
      methods: {
        handleImg (type) {
          if (type === 1) { // 上一张
            this.currentPage++
            if (this.currentPage >= this.pictureList.length) {
              this.currentPage = 0
            }
          } else { // 下一张
            this.currentPage--
            if (this.currentPage < 0) {
              this.currentPage = this.pictureList.length - 1
            }
          }
          // 获取图片当前信息
          this.fileInfo = this.pictureList[this.currentPage]
          this.$refs.rotate.style.transform = `rotate(${this.fileInfo.deg}deg)`
        },
        // 旋转图片
        rotate () {
          this.fileInfo = this.pictureList[this.currentPage]
          this.fileInfo.deg += 90
          if (this.fileInfo.deg >= 360) {
            this.fileInfo.deg = 0
          }
          this.$refs.rotate.style.transform = `rotate(${this.fileInfo.deg}deg)`
        }
      }
    }
    </script>
    
    <style>
    .rotate_contanier {
      display: flex;
      flex-direction: column;
      padding: 20px;
      background-color: #909399;
       600px;
      height: 670px;
    }
    .header {
      height: 50px;
      margin-bottom: 10px;
      text-align: center;
      background-color: #fff;
      padding-top: 20px;
    }
    .header span {
      padding: 5px 8px;
      color: #fff;
      background-color: #409eff;
      border-radius: 4px;
      margin-right: 5px;
      cursor: pointer;
    }
    .picture_contaner {
      height: 600px;
       100%;
      background-color: #fff;
      padding: 10px;
      box-sizing: border-box;
    }
    .picture_contaner .img_box {
       100%;
      height: 100%;
      position: relative;
    }
    .picture_contaner .img_box img {
      max- 100%;
      max-height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      margin: auto;
    }
    
    </style>

    实现思路: 旋转的时候需要把容器长宽设为一致,也就是图片容器为正方形,这样旋转的时候旋转中心是对称的,不会出现图片超出容器的情况.

    把图片放到一个容器里装起来,图片的宽高不能超过容器的宽高,居中布局,旋转的时候旋转图片容器,这样图片的长宽对旋转不会有影响

    最终效果:
       
     
        
     
    图片为百度图片,如有侵权,请联系删除
  • 相关阅读:
    ORA28056:Writing audit records to Windows EventLog failed
    oracle odp.net
    C#共享内存
    JQuery EasyUI Tree和tab右键菜单实现
    Query EasyUI Tree树使用介绍
    UML类图
    Oracle 创建表空间和用户
    Oracle 11g ORA12514:TNS:监听程序当前无法识别连接描述符中请求的服务 .
    Client使用c#和odp.net连接server oracle
    WPF 4文字模糊不清晰解决方法
  • 原文地址:https://www.cnblogs.com/steamed-twisted-roll/p/11667219.html
Copyright © 2011-2022 走看看