vue鼠标经过图片图片放大

![在这里插入图片描述](https://img-blog.csdnimg.cn/5925e8464b0d4b858cad77c75f068ef8.png#pic_center)

![在这里插入图片描述](https://blog.csdn.net/yehaocheng520/article/details/119003274#pic_center)



<div class="big-img-box" ref="big-img-box" @mouseleave="moveend">
  <div class="imgWrap" @mouseenter="movestart" @mousemove.stop>
    <img :src="HOST + commonUploadFileUrl + bigImgsrc" alt="" />
  </div>
  <div
    v-if="bigImgUrl"
    @mousemove.stop
    @mousemove="move"
    class="move"
    ref="move"
    :style="{ left: -left / 3 + 'px ', top: -top / 3 + 'px' }"
  ></div>
  <div
    class="bigPic"
    ref="bigPic"
    v-if="bigImgUrl"
    :style="{ backgroundPosition: left + 'px ' + top + 'px' }"
  ></div>
</div>

<div class="small-img-box">
  <div class="img-content-box">
    <ul>
      <li
        v-for="(item, index) in detailInfo.images"
        :key="index"
        @mouseover="handleBigImg(item, index)"
        :class="{ smallImgBorder: currentSmallImgIndex == index }"
      >
        <img :src="HOST + commonUploadFileUrl + item.src" alt="" />
      </li>
    </ul>
  </div>
</div>


css
.big-img-box {
  width: 480px;
  height: 360px;
  margin: 0 0 20px;
  border: 1px solid #eee;
  position: relative;
}
 .imgWrap {
    width: 100%;
    height: 100%;
    overflow: hidden;
  }
  .move {
    position: absolute;
    left: 0;
    top: 0;
    width: 200px;
    height: 200px;
    background: rgba(0, 0, 255, 0.15);
    cursor: move;
  }
  .bigPic {
    width: 480px;
    height: 480px;
    position: absolute;
    left: 480px;
    top: 0;
    border: 2px solid #f90;
    background-position: 0 0;
    background-size: 300% 300%;
    background-repeat: no-repeat;
    z-index: 2;
  }


// 循环图片样式
.small-img-box {
  width: 480px;
  height: 70px;
  position: relative;
  .img-content-box {
    width: 100%;
    margin: 0 auto;
    height: 100%;
    display: block;
    ul {
      width: 105%;
      height: 100%;
      li {
        display: inline-block;
        width: 88px;
        height: 100%;
        margin-right: 10px;
        overflow: hidden;
        img {
          display: block;
          width: 100%;
        }
      }
    }
  }
}


// 方法
 //鼠标移入后,就要展示选择框,因此鼠标移入的位置跟选择框的位置有关
movestart(e) {
  this.move(e);
  this.bigImgUrl = this.bigImgsrc;
  var imgUrl = this.HOST + this.commonUploadFileUrl + this.bigImgUrl;//这个就是当前图片的路径,不管多管
  this.$nextTick(() => {//页面加载完成后,更改大图的`backgroundImage`,这个也可以直接在html中动态绑定style,那样会简单很多
    this.$refs.bigPic.style.backgroundImage = `url(${imgUrl})`;
  });
},


//鼠标移动时,选择框的位置也会发现改变
move(e) {
  var left = e.pageX - this.oAppLeft - 100;
  var top = e.pageY - this.oAppTop - 100;
  if (left < 0) {
    left = 0;
  } else if (left > 280) {
    left = 280;
  }
  if (top < 0) {
    top = 0;
  } else if (top > 160) {
    top = 160;
  }
  this.left = -left * 3;
  this.top = -top * 3;
},

 moveend() {
      this.bigImgUrl = "";
      this.left = 0;
      this.top = 0;
    },
您好!对于您的问题,您可以使用 Vue 的事件绑定和动态绑定 class 的方式来实现。 在模板中,您可以通过 `@mouseenter` 和 `@mouseleave` 监听鼠标移入和移出事件,同时使用 `:class` 动态绑定 class,根据当前状态切换不同的图片。 假设您有两张图片,分别为 `image1.png` 和 `image2.png`,您可以这样写: ```html <template> <div class="image" @mouseenter="isHover = true" @mouseleave="isHover = false" :class="{ 'image-hover': isHover }" > </div> </template> <style> .image { background-image: url('image1.png'); background-size: cover; width: 200px; height: 200px; } .image-hover { background-image: url('image2.png'); } </style> <script> export default { data() { return { isHover: false } } } </script> ``` 在模板中,我们定义了一个 `div`,并设置了默认的背景图片为 `image1.png`。同时,我们监听了 `@mouseenter` 和 `@mouseleave` 事件,并使用 `isHover` 变量来保存当前鼠标是否移入。在 `:class` 中,我们使用了对象语法,将 `image-hover` class 绑定到 `isHover` 变量的值上。在样式中,我们设置了 `image-hover` class 的背景图片为 `image2.png`。 这样,当鼠标移入时,`isHover` 变量的值为 `true`,`div` 的 class 将会变成 `image image-hover`,从而切换为 `image2.png`;当鼠标移出时,`isHover` 变量的值为 `false`,`div` 的 class 将恢复为 `image`,从而切换回 `image1.png`。 希望这可以帮助到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值