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;
  width: 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;
  width: 100%;
  background-color: #fff;
  padding: 10px;
  box-sizing: border-box;
}
.picture_contaner .img_box {
  width: 100%;
  height: 100%;
  position: relative;
}
.picture_contaner .img_box img {
  max-width: 100%;
  max-height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
}

</style>

 

實現思路: 旋轉的時候需要把容器長寬設為一致,也就是圖片容器為正方形,這樣旋轉的時候旋轉中心是對稱的,不會出現圖片超出容器的情況.

把圖片放到一個容器里裝起來,圖片的寬高不能超過容器的寬高,居中布局,旋轉的時候旋轉圖片容器,這樣圖片的長寬對旋轉不會有影響

最終效果:
   
 
    
 
圖片為百度圖片,如有侵權,請聯系刪除


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM