欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實現(xiàn)點擊圖片放大縮小及拖拽功能

 更新時間:2025年02月07日 08:34:41   作者:Z小明  
本文使用 vue創(chuàng)建一個可拖拽和縮放的圖片查看器組件,該組件不僅可以展示圖片,還支持用戶通過鼠標拖動和縮放來查看細節(jié),本文將介紹如何封裝一個簡單的圖片拖拽與縮放組件,感興趣的小伙伴跟著小編一起來看看吧

前言

本文使用 vue創(chuàng)建一個可拖拽和縮放的圖片查看器組件,該組件不僅可以展示圖片,還支持用戶通過鼠標拖動和縮放來查看細節(jié)。本文將介紹如何封裝一個簡單的圖片拖拽與縮放組件。

先看效果圖:

組件功能概述

本組件具備以下功能:

  • 顯示圖片
  • 支持鼠標拖動以調(diào)整圖片位置
  • 支持鼠標滾輪縮放圖片
  • 自動居中圖片

代碼實現(xiàn)

下面是組件的完整代碼,包括模板、js和樣式部分。

1. 模板部分

<template>
  <div style="height: 90vh;">
    <div
      class="image-container"
      @mousedown="startDrag"
      @mouseup="stopDrag"
      @mousemove="drag"
      @wheel="handleWheel"
    >
      <img
        :src="imgSrc"
        :style="{ transform: `scale(${scale})`, cursor: isDragging ? 'grabbing' : 'grab' }"
        ref="image"
      />
      <!-- @click="toggleZoom"  點擊放大-->
    </div>
  </div>
</template>

在模板中,我們定義了一個包含圖片的容器。通過綁定鼠標事件,我們可以實現(xiàn)拖拽和縮放的功能。

2. js部分

<script>
import testImg from './testImg.jpeg'; // 導入圖片
 
export default {
  name: 'ImageView',
  props: {
    // 圖片路徑
    imgSrc: {
      type: String,
      default: () => testImg, // 沒有傳值時使用默認圖片
    }
  },
  data () {
    return {
      scale: 1, // 初始縮放比例
      isDragging: false, // 是否正在拖拽
      lastMouseX: 0, // 上一次鼠標 X 坐標
      lastMouseY: 0, // 上一次鼠標 Y 坐標
      offsetX: 0, // 圖片相對容器的 X 偏移量
      offsetY: 0, // 圖片相對容器的 Y 偏移量
      imageWidth: 0, // 圖片實際寬度
      imageHeight: 0, // 圖片實際高度
    };
  },
  mounted () {
    const img = this.$refs.image; // 獲取圖片元素
    this.imageWidth = img.naturalWidth; // 獲取圖片的實際寬度
    this.imageHeight = img.naturalHeight; // 獲取圖片的實際高度
    this.centerImage(); // 初始化時居中圖片
  },
  methods: {
    // 居中圖片的方法
    centerImage () {
      const container = this.$el.querySelector('.image-container'); // 獲取容器
      const containerWidth = container.clientWidth; // 容器寬度
      const containerHeight = container.clientHeight; // 容器高度
 
      // 計算居中后的 X 和 Y 偏移量
      const centeredX = (containerWidth - this.imageWidth * this.scale) / 2;
      const centeredY = (containerHeight - this.imageHeight * this.scale) / 2;
 
      this.offsetX = centeredX; // 更新 X 偏移量
      this.offsetY = centeredY; // 更新 Y 偏移量
 
      // 設(shè)置圖片的新位置
      this.$refs.image.style.left = `${centeredX}px`;
      this.$refs.image.style.top = `${centeredY}px`;
    },
 
    // 點擊圖片時切換縮放
    toggleZoom () {
      this.scale = this.scale === 1 ? 2 : 1; // 點擊圖片時切換縮放比例
    },
 
    // 鼠標按下事件
    startDrag (event) {
      this.isDragging = true; // 標記為正在拖拽
      this.lastMouseX = event.clientX; // 記錄當前鼠標 X 坐標
      this.lastMouseY = event.clientY; // 記錄當前鼠標 Y 坐標
      this.offsetX = this.$refs.image.offsetLeft; // 記錄當前左偏移
      this.offsetY = this.$refs.image.offsetTop; // 記錄當前上偏移
    },
 
    // 鼠標抬起事件
    stopDrag () {
      this.isDragging = false; // 停止拖拽
    },
 
    // 鼠標移動事件
    drag (event) {
      if (this.isDragging) { // 如果正在拖拽
        const dx = event.clientX - this.lastMouseX; // 計算 X 軸移動距離
        const dy = event.clientY - this.lastMouseY; // 計算 Y 軸移動距離
        const newLeft = this.offsetX + dx; // 計算新的左偏移
        const newTop = this.offsetY + dy; // 計算新的上偏移
 
        // 更新圖片的位置
        this.$refs.image.style.left = `${newLeft}px`; // 設(shè)置新的左偏移
        this.$refs.image.style.top = `${newTop}px`; // 設(shè)置新的上偏移
      }
    },
 
    // 鼠標滾輪事件處理縮放
    handleWheel (event) {
      event.preventDefault(); // 阻止默認的滾動行為
      const scaleChange = event.deltaY > 0 ? 0.1 : -0.1; // 根據(jù)滾動方向調(diào)整縮放
      this.scale = Math.max(0.2, this.scale + scaleChange); // 限制最小縮放比例為 0.2
    },
  },
};
</script>

在js部分,我們使用 Vue 的數(shù)據(jù)和方法來實現(xiàn)組件的核心功能。通過事件處理函數(shù),我們管理圖片的縮放和拖拽邏輯。

3. 樣式部分

<style scoped>
.image-container {
  position: relative; /* 使容器相對定位 */
  overflow: hidden; /* 防止內(nèi)容超出邊界 */
  cursor: grab; /* 默認光標樣式 */
  width: 100%; /* 容器寬度 */
  height: 100%; /* 容器高度 */
}
 
img {
  position: absolute; /* 使得 img 可以被拖拽 */
  transition: transform 0.3s; /* 平滑的縮放效果 */
}
</style>

樣式部分確保了圖片容器的正確顯示,以及在縮放時的平滑過渡效果。 

總結(jié)

通過以上代碼,我們實現(xiàn)了一個簡單的圖片拖拽與縮放組件。用戶可以方便地查看圖片細節(jié),提高了交互體。也可以根據(jù)項目需求進一步擴展和優(yōu)化這個組件,比如增加雙擊放大功能、支持多種圖片格式等,目前這樣的已經(jīng)滿足需求,需要使用及擴展的自行優(yōu)化,記得@我去抄作業(yè)

到此這篇關(guān)于JS實現(xiàn)點擊圖片放大縮小及拖拽功能的文章就介紹到這了,更多相關(guān)JS點擊圖片放大縮小及拖拽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論