最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue實(shí)現(xiàn)圖片預(yù)覽放大以及縮小問(wèn)題

 更新時(shí)間:2023年01月14日 14:30:27   作者:前端張小小  
這篇文章主要介紹了vue實(shí)現(xiàn)圖片預(yù)覽放大以及縮小問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vue圖片預(yù)覽放大以及縮小

1.在vue的環(huán)境下實(shí)現(xiàn)圖片放大縮小,可以使用viewerjs

效果圖:

關(guān)于 viewerjs的使用

1)首先安裝依賴(lài)

npm i v-viewer --save

2)全局注冊(cè)(在main.js)以及引用css

//導(dǎo)入圖片預(yù)覽viewer組件以及css
import Viewer from 'v-viewer'
import 'viewerjs/dist/viewer.css'
Vue.use(Viewer);
Viewer.setDefaults({
  Options: { "inline": true, "button": true, "navbar": true, "title": true, "toolbar": true, "tooltip": true, "movable": true, "zoomable": true, "rotatable": true, "scalable": true, "transition": true, "fullscreen": true, "keyboard": true, "url": "data-source" }
});

關(guān)于viewerjs中setDeafaults的配置下面有一張表,大家可以參考一下

這些工作都做完以后,然后可以在components(公共組件)文件下新建一個(gè)文件夾,命名看個(gè)人習(xí)慣,再新建一個(gè)以xxx.vue文件(同上,命名不做具體要求看個(gè)人習(xí)慣)。

在xxx.vue中寫(xiě)入

<template>
  <div class="content">
    <!-- <h1>Viewer圖片預(yù)覽插件</h1> -->
    <viewer :images="viewerList">
      <img
        v-for="src in viewerList"
        :src="src.icon"
        :key="src.id"
        style="width: 50px; height: 50px; margin-right: 5px"
      />
    </viewer>
  </div>
</template>
 
<script>
export default {
  props: {
    viewerList: {
      type: Array,
      default: [],
    },
  },
  mounted() {},
  data() {
    return {};
  },
};
</script>
 
<style>
</style>

因?yàn)橐呀?jīng)全局注冊(cè)過(guò)了,可以直接使用<viewer>標(biāo)簽 然后這個(gè)作為子組件。viewer綁定的一定要為數(shù)組,不然是會(huì)報(bào)錯(cuò)的(避坑)。

當(dāng)這些都準(zhǔn)備好的就可以在“父頁(yè)面”使用了。

在父頁(yè)面不要忘了引用以及注冊(cè)

import viewer from "../../../components/viewer/viewer-preview.vue";
export default {
  name: "business",
  components: { viewer },
}

僅展示了關(guān)鍵代碼,其他的data(){ return{   }   }等等按需寫(xiě)入。

注冊(cè)完以后在需要用到的地方直接寫(xiě)入,數(shù)據(jù)的都是通過(guò)父?jìng)髯觩rops的方式

  <Table :columns="figurenHeader" :data="figurenData">
          <template slot-scope="{ row }" slot="img">
            <div style="margin-top: 10px">
              <viewer :viewerList="row.iconUrlList"></viewer>
            </div>
          </template>
        </Table>

因?yàn)槭褂玫氖莟able里面的插槽,所以傳入的數(shù)據(jù)為row.iconUrlList.  這樣整個(gè)工作就算是結(jié)束了。

vue實(shí)現(xiàn)圖片預(yù)覽(放大縮小拖拽)純手寫(xiě)

這張圖是顯示的圖片放大的一個(gè)預(yù)覽情況,這里是參考預(yù)覽操作實(shí)現(xiàn)的一個(gè)背景為黑色的部分,上層的圖片可實(shí)現(xiàn)滾輪放大或者點(diǎn)擊上部的放大鏡圖標(biāo)進(jìn)行放大,代碼是基于Ant Design Vue框架的基礎(chǔ)上

這里先分解部分,后面有全部代碼

1.需要有黑色背景用于預(yù)覽背景:

這里的背景要占滿(mǎn)整個(gè)屏幕(這里的一般是參考其他插件預(yù)覽的樣式進(jìn)行模擬設(shè)計(jì)的),樣式在后方代碼內(nèi)

2.展示圖片并且把圖片展示到背景板最中間。

3.最重要的下方的兩部分:

滾輪放大縮小圖片

bbimg() {
      let e = e || window.event
      this.params.zoomVal += e.wheelDelta / 1200
      if (this.params.zoomVal >= 0.2) {
        this.test = `transform:scale(${this.params.zoomVal});`
      } else {
        this.params.zoomVal = 0.2
        this.test = `transform:scale(${this.params.zoomVal});`
        return false
      }
    },

圖片拖拽

imgMove(e) {
      console.log('e', e)
      let oImg = e.target
      let disX = e.clientX - oImg.offsetLeft
      let disY = e.clientY - oImg.offsetTop
      console.log('disX', disX)
      document.onmousemove = (e) => {
        e.preventDefault()
        let left = e.clientX - disX
        let top = e.clientY - disY
        this.test = this.test + `left: ${left}px;top: ${top}px;`
      }
      document.onmouseup = (e) => {
        document.onmousemove = null
        document.onmouseup = null
      }
    },

這里的test和classStyle是作為圖片的動(dòng)態(tài)樣式,雖然名字起得著急,但是不影響使用

整體實(shí)現(xiàn)的功能

  • 點(diǎn)擊圖片,可以進(jìn)行滾輪放大及縮小,
  • 點(diǎn)擊后按壓左鍵可進(jìn)行拖拽查看圖片
  • 點(diǎn)擊上方的放大及縮小圖標(biāo)也可以進(jìn)行放大等操作,
  • 點(diǎn)擊 x 可關(guān)于預(yù)覽
  • 點(diǎn)擊關(guān)閉后,恢復(fù)大小,避免點(diǎn)擊其他照片影響大小

下面是全部實(shí)現(xiàn)代碼:

<template>
  <a-card style="width: 100%">
    <div>
      <img
        :src="file"
        alt=""
        @click="handlePhotoShow(file)"
        />
      <!-- preview="0"
        preview-text="圖片" -->
    </div>
    <div class="showImg" v-if="pictShow" @mousewheel="bbimg(this)">
      <div class="setting_box">
        <a-icon
          class="setting_zoom"
          v-if="zoomInShow == false"
          type="zoom-in"
          @click="handleZoomIn"
        />
        <a-icon
          color="#fff"
          class="setting_zoom"
          v-if="zoomInShow == true"
          type="zoom-out"
          @click="handleZoomOut"
        />
        <a-icon color="#fff" class="setting_close" type="close" @click="handleClose" />
      </div>
      <img :src="file" alt="" :class="classStyle" :style="test" @mousedown="imgMove" />
    </div>
  </a-card>
</template>
 
<script>
export default {
  data() {
    return {
      test: '',
      pictShow: false,
      zoomInShow: false,
      params: {
        zoomVal: 1,
        left: 0,
        top: 0,
        currentX: 0,
        currentY: 0,
        flag: false,
      },
      file: '',
    }
  },
  computed: {
    classStyle() {
      return this.zoomInShow ? 'a1' : 'a2'
    },
  },
  methods: {
    // 實(shí)現(xiàn)圖片放大縮小 
    bbimg() {
      let e = e || window.event
      this.params.zoomVal += e.wheelDelta / 1200
      if (this.params.zoomVal >= 0.2) {
        this.test = `transform:scale(${this.params.zoomVal});`
      } else {
        this.params.zoomVal = 0.2
        this.test = `transform:scale(${this.params.zoomVal});`
        return false
      }
    },
    // 實(shí)現(xiàn)圖片拖拽
    imgMove(e) {
      console.log('e', e)
      let oImg = e.target
      let disX = e.clientX - oImg.offsetLeft
      let disY = e.clientY - oImg.offsetTop
      console.log('disX', disX)
      document.onmousemove = (e) => {
        e.preventDefault()
        let left = e.clientX - disX
        let top = e.clientY - disY
        this.test = this.test + `left: ${left}px;top: ${top}px;`
      }
      document.onmouseup = (e) => {
        document.onmousemove = null
        document.onmouseup = null
      }
    },
    handleZoomIn() {
      this.zoomInShow = true
    },
    handleZoomOut() {
      this.zoomInShow = false
    },
    handlePhotoShow(file) {
      console.log('file', file)
      this.file = file
      this.pictShow = true
    },
    handleClose() {
      this.pictShow = false
      this.test = `transform:scale(1)`
    },
  },
}
</script>
<style scoped lang="less">
.showImg {
  width: 100%;
  height: 100vh;
  background-color: rgba(0, 0, 0, 1);
  position: fixed;
  *position: absolute;
  z-index: 20;
  margin: 0 auto;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  .setting_box {
    width: 100%;
    height: 50px;
    line-height: 50px;
    font-size: 20px;
    background-color: rgba(0, 0, 0, 0.3);
    position: absolute;
    top: 0;
    z-index: 999;
    .setting_zoom,
    .setting_close {
      position: absolute;
      z-index: 1000;
      top: 20px;
      color: #fff;
      opacity: 1;
    }
    .setting_zoom {
      right: 50px;
    }
    .setting_close {
      right: 10px;
    }
  }
}
.a1 {
  max-width: 200vw;
  max-height: 180vh;
  position: absolute;
  z-index: 22;
  margin-top: 40px;
  cursor: move;
}
.a2 {
  max-width: 95vw;
  max-height: 90vh;
  position: absolute;
  z-index: 22;
  margin-top: 40px;
  cursor: move;
}
.zoom-box {
  cursor: zoom-in;
}
.photo_box {
  margin: 0 5px 5px 0;
}
</style>

因?yàn)榫唧w也是查看了很多博客等資源最后完成的。

其實(shí)在代碼內(nèi)有一部分代碼:

<img
	:src="file"
	preview="0"
	preview-text="圖片"
	alt=""
	@click="handlePhotoShow(file)"
/>

其實(shí)有  preview="0" preview-text="圖片" 這兩行實(shí)現(xiàn)圖片的預(yù)覽,但是找了資料沒(méi)找到具體實(shí)現(xiàn)的部分,但是這個(gè)屬性確實(shí)實(shí)現(xiàn)了

這里手寫(xiě)預(yù)覽的原因是這個(gè)插件在數(shù)量大的情況下是沒(méi)有反應(yīng)的。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 利用Vue實(shí)現(xiàn)一個(gè)markdown編輯器實(shí)例代碼

    利用Vue實(shí)現(xiàn)一個(gè)markdown編輯器實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于如何利用Vue實(shí)現(xiàn)一個(gè)markdown編輯器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • vue中項(xiàng)目頁(yè)面空白但不報(bào)錯(cuò)產(chǎn)生的原因及分析

    vue中項(xiàng)目頁(yè)面空白但不報(bào)錯(cuò)產(chǎn)生的原因及分析

    這篇文章主要介紹了vue中項(xiàng)目頁(yè)面空白但不報(bào)錯(cuò)產(chǎn)生的原因及分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Vue使用mockjs問(wèn)題(返回?cái)?shù)據(jù)、get、post 請(qǐng)求)

    Vue使用mockjs問(wèn)題(返回?cái)?shù)據(jù)、get、post 請(qǐng)求)

    這篇文章主要介紹了Vue使用mockjs問(wèn)題(返回?cái)?shù)據(jù)、get、post 請(qǐng)求),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2023-05-05
  • vue中使用h5 video標(biāo)簽實(shí)現(xiàn)彈窗播放本地視頻的方法

    vue中使用h5 video標(biāo)簽實(shí)現(xiàn)彈窗播放本地視頻的方法

    本文主要介紹了vue中使用h5 video標(biāo)簽實(shí)現(xiàn)彈窗播放本地視頻的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • element的表單元素使用總結(jié)

    element的表單元素使用總結(jié)

    表單元素挺多的,本文主要介紹了element的表單元素使用總結(jié),主要包括文本框類(lèi)、選擇類(lèi)、其他類(lèi),有一定的參考價(jià)值,感興趣的可以了解一下
    2021-06-06
  • vue3+vite+SQL.js如何讀取db3文件數(shù)據(jù)

    vue3+vite+SQL.js如何讀取db3文件數(shù)據(jù)

    這篇文章主要介紹了vue3+vite+SQL.js如何讀取db3文件數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Vue?table新增、編輯解讀

    Vue?table新增、編輯解讀

    這篇文章主要介紹了Vue?table新增、編輯,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Vue3多組件的N種編寫(xiě)方式

    Vue3多組件的N種編寫(xiě)方式

    Vue 本身以及周邊生態(tài)在設(shè)計(jì)語(yǔ)法糖上幾乎沒(méi)讓我失望過(guò),包括本次亮相的 Vue Vine,它的出現(xiàn)引起了我對(duì) Vue3 組件編寫(xiě)方式的好奇,以及哪一種方式更接近「最佳實(shí)踐」?下面讓我來(lái)為大家一一道來(lái)
    2024-07-07
  • Vue3中的createGlobalState用法及示例詳解

    Vue3中的createGlobalState用法及示例詳解

    createGlobalState 是 Vue 3 中一種管理全局狀態(tài)的簡(jiǎn)便方式,通常用于管理多個(gè)組件間共享的狀態(tài),由 @vueuse/core 提供的,允許創(chuàng)建一個(gè)響應(yīng)式的全局狀態(tài),本文給大家介紹了Vue3中的createGlobalState用法及示例,需要的朋友可以參考下
    2024-10-10
  • vue調(diào)試工具vue-devtools安裝及使用方法

    vue調(diào)試工具vue-devtools安裝及使用方法

    本文主要介紹 vue的調(diào)試工具 vue-devtools 的安裝和使用,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-11-11

最新評(píng)論

黄浦区| 清苑县| 米林县| 北海市| 牡丹江市| 江门市| 天台县| 柘荣县| 赣州市| 辽阳县| 阳谷县| 通江县| 惠州市| 炉霍县| 太仓市| 金川县| 旺苍县| 平顶山市| 偃师市| 鄂托克前旗| 拜泉县| 修武县| 交城县| 广德县| 莆田市| 上思县| 马鞍山市| 五大连池市| 勐海县| 盐城市| 万源市| 永兴县| 诸暨市| 宁都县| 亚东县| 商河县| 宁陕县| 新化县| 商洛市| 久治县| 庆阳市|