原生js滚动放大缩小图片

js 放大缩小(滚动)

<template>
  <div>
    <div style="width: 100px;height: 100px;"></div>
    <!-- ondragstart="return false" 禁止浏览器默认拖动行为 -->
    <div class="image" @mousewheel="mousewheel">
      <img
        ondragstart="return false"
        ref="imgRef"
        @mousedown="mousedown"
        @mouseup="mouseup"
        @mouseleave="mouseleave"
        @mousemove="mousemove"
        :style="{
          top: getTop,
          left: getLeft,
          width: getWidth,
          height: getHeight,
        }"
        src="@/assets/images/homePageImg/pic.png"
      />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
    //父框的大小和子框的大小尽量相同
      //图片父框坐标(x,y)
      fatherX: 500,
      fatherY: 500,
      //父框大小
      fatherWidth: 500,
      fatherHeight: 500,
      //图片相对父框位置
      top: 0,
      left: 0,
      //初始图片大小
      width: 500,
      height: 500,
      //图片宽高比
      ratio: 1,
      //放大缩小比例(即每次放大缩小的px)
      canvasBgRatio: 10,
      //图片是否可以移动
      isImgMove: false,
      //鼠标按下时坐标
      mousedownX: 0,
      mousedownY: 0,
      mousedownLeft: 0,
      mousedownTop: 0,
    }
  },
  created() {
    //图片宽高比
    this.ratio = this.width / this.height
  },
  computed: {
    getLeft: function() {
      return this.left + 'px'
    },
    getTop: function() {
      return this.top + 'px'
    },
    getWidth: function() {
      return this.width + 'px'
    },
    getHeight: function() {
      return this.height + 'px'
    },
    //图片当前坐标
    imgX: function() {
      return this.fatherX + this.left
    },
    imgY: function() {
      return this.fatherY + this.top
    },
  },
  methods: {
    //鼠标按压时间
    mousedown(param) {
      console.log('按下')
      this.mousedownX = param.clientX
      this.mousedownY = param.clientY
      this.mousedownLeft = this.left
      this.mousedownTop = this.top
      this.isImgMove = true
    },
    mouseup(param) {
      console.log('松开')
      this.isImgMove = false
    },
    mouseleave(param) {
      console.log('离开')
      this.isImgMove = false
    },
    mousemove(param) {
      if (this.isImgMove) {
        console.log('移动')
        //鼠标移动后坐标,即当前坐标
        let mouseX = param.clientX
        let mouseY = param.clientY
        //计算图片相对父框位置的变化
        let tempLeft = this.mousedownLeft - (this.mousedownX - mouseX)
        if (tempLeft < 0 && tempLeft + this.width > this.fatherWidth) {
          this.left = tempLeft
        }
        let tempTop = this.mousedownTop - (this.mousedownY - mouseY)
        if (tempTop < 0 && tempTop + this.height > this.fatherHeight) {
          this.top = tempTop
        }
      }
    },
    //鼠标滚轮事件
    mousewheel(param) {
      //如果处于移动状态则不能放大缩小
      if (this.isImgMove) {
        return false
      }
      //获取鼠标坐标
      let mouseX = param.clientX
      let mouseY = param.clientY

      // param.deltaY鼠标滚轮上滚为负数,下滚为正数,上滚放大图片,下滚缩小
      //放大
      if (param.deltaY < 0) {
        this.magnify(mouseX, mouseY)
      }
      //缩小(最小为初始值)
      if (
        param.deltaY > 0 &&
        this.width > this.fatherWidth &&
        this.height > this.fatherHeight
      ) {
        this.shrink(mouseX, mouseY)
      }
    },
    //放大
    magnify(mouseX, mouseY) {
      //计算图片的宽高
      let oldWidth = this.width
      let oldHeight = this.height
      this.height = this.height + this.canvasBgRatio
      this.width = this.height * this.ratio
      //鼠标组坐标和图片坐标距离
      let miWidth = mouseX - this.imgX
      let miHeight = mouseY - this.imgY
      //计算图片相对父框位置的变化
            this.left = this.left - ((this.width - oldWidth) / oldWidth) * miWidth
      this.top = this.top - (this.canvasBgRatio / oldHeight) * miHeight
    },
    //缩小
    shrink(mouseX, mouseY) {
      //计算图片的宽高
      let oldWidth = this.width
      let oldHeight = this.height
      this.height = this.height - this.canvasBgRatio
      this.width = this.height * this.ratio
      // 图片父框坐标和图片坐标距离
      let mfWidth = this.fatherX - this.imgX
      let mfHeight = this.fatherY - this.imgY
      //计算图片相对父框位置的变化
      this.left =
        this.left +
        ((oldWidth - this.width) / (oldWidth - this.fatherWidth)) * mfWidth
      this.top =
        this.top +
        (this.canvasBgRatio / (oldHeight - this.fatherHeight)) * mfHeight
    },
  },
}
</script>

<style scoped>
.image {
  position: absolute;
  border: 1px solid #aa0000;
  width: 500px;
  height: 500px;
  overflow: hidden;
  cursor: pointer;
}

.image img {
  position: absolute;
  width: 500px;
  height: 500px;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端开发中,可以使用原生 JavaScript 或 jQuery 来实现图片放大效果。下面分别给出两种方法: 1. 原生 JavaScript 方法: ```javascript // HTML 结构 // <img src="path_to_image.jpg" alt="Image" class="zoomable-image"> // CSS 样式 // .zoomable-image { // cursor: pointer; // } // JavaScript 代码 const images = document.querySelectorAll('.zoomable-image'); images.forEach(image => { image.addEventListener('click', function() { // 创建放大图片的容器 const zoomContainer = document.createElement('div'); zoomContainer.classList.add('zoom-container'); document.body.appendChild(zoomContainer); // 创建放大图片 const zoomedImage = document.createElement('img'); zoomedImage.src = this.src; zoomedImage.alt = this.alt; zoomContainer.appendChild(zoomedImage); // 点击放大图片容器时关闭放大效果 zoomContainer.addEventListener('click', function() { this.remove(); }); }); }); ``` 2. 使用 jQuery 方法: ```javascript // HTML 结构与 CSS 样式同上 // JavaScript 代码 $('.zoomable-image').on('click', function() { // 创建放大图片的容器 const zoomContainer = $('<div>').addClass('zoom-container'); $('body').append(zoomContainer); // 创建放大图片 const zoomedImage = $('<img>').attr('src', this.src).attr('alt', this.alt); zoomContainer.append(zoomedImage); // 点击放大图片容器时关闭放大效果 zoomContainer.on('click', function() { $(this).remove(); }); }); ``` 以上代码实现了点击图片时,在页面上创建一个放大图片,并且点击放大图片时可以关闭放大效果。你可以根据实际需求,对样式和交互进行进一步的调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值