vue仿淘宝放大镜功能

vue仿淘宝放大镜功能

html部分

// An highlighted block
 <div class="appleft">
      <div
        class="left"
        style="display: none"
        :style="{ display: 'block' }"
        @mouseover="overShow()"
        @mouseleave="outHide()"
      >
        <img class="leftImg" :src="pageimg" alt="" />
        <!-- 鼠标层罩 -->
        <div v-show="topShow" class="top" :style="topStyle"></div>
        <!-- 最顶层覆盖了整个原图空间的透明层罩 -->
        <div
          class="maskTop"
          @mouseenter="enterHandler"
          @mousemove="moveHandler"
          @mouseout="outHandler"
        ></div>
      </div>
      <div v-show="rShow" class="right">
        <img :style="r_img" class="rightImg" :src="pageimg" alt="" />
      </div>
    </div>

js部分

// An highlighted block
export default {
  data() {
    return {
      topStyle: { transform: "" },
      r_img: {},
      topShow: false,
      rShow: false,
      pageimg:
        "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1933440720,2469399825&fm=26&gp=0.jpg",
        }},
         methods: {
    // 鼠标进入原图空间函数
    enterHandler() {
      // 层罩及放大空间的显示
      this.topShow = true;
      this.rShow = true;
    },
    // 鼠标移动函数
    moveHandler(event) {
      // 鼠标的坐标位置
      let x = event.offsetX;
      let y = event.offsetY;
      // 层罩的左上角坐标位置,并对其进行限制:无法超出原图区域左上角
      let topX = x - 100 < 0 ? 0 : x - 100;
      let topY = y - 100 < 0 ? 0 : y - 100;
      // 对层罩位置再一次限制,保证层罩只能在原图区域空间内
      if (topX > 200) {
        topX = 200;
      }
      if (topY > 200) {
        topY = 200;
      }
      // 通过 transform 进行移动控制
      this.topStyle.transform = `translate(${topX}px,${topY}px)`;
      this.r_img.transform = `translate(-${2 * topX}px,-${2 * topY}px)`;
    },
    // 鼠标移出函数
    outHandler() {
      // 控制层罩与放大空间的隐藏
      this.topShow = false;
      this.rShow = false;
    },
    overShow() {
      document.getElementById("commodityright").style.display = "none";
    },
    outHide() {
      document.getElementById("commodityright").style.display = "inline-block";
    },
  },
};

css部分

// An highlighted block
.app {
  width: 80%;
  height: 100%;
  margin-top: 5vh;
  margin-left: 30vh;
  padding: 50px;
  border: 1px solid #ccc;
  border-bottom: none;
  border-top: none;
  flex-direction: row;
  justify-content: space-around;
}
/* 放大镜 */
.appleft {
  display: inline-block;
  position: relative;
  height: 100%;
}
/* 文字等等 */
.appright {
  width: 20vw;
  height: 110%;
  float: right;
  margin-right: 5vw;
}
/* 放大的图片,通过定位将左上角定位到(0,0) */
.rightImg {
  display: inline-block;
  width: 800px;
  height: 800px;
  position: absolute;
  top: 0;
  left: 0px;
}
/* 右边的区域图片放大空间 */
.right {
  margin-left: 412px;
  width: 400px;
  height: 400px;
  border: 1px solid red;
  position: relative;
  overflow: hidden;
}
/* 一个最高层层罩 */
.maskTop {
  width: 400px;
  height: 400px;
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
}
/* 层罩,通过定位将左上角定位到(0,0) */
.top {
  width: 200px;
  height: 200px;
  background-color: lightcoral;
  opacity: 0.4;
  position: absolute;
  top: 0;
  left: 0;
}
/* 原图的显示 */
.leftImg {
  width: 38.5vh;
  height: 38.5vh;
  display: inline-block;
}
/* 原图的容器 */
.left {
  width: 38.5vh;
  height: 38.5vh;
  border: 1px solid teal;
  float: left;
  position: relative;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值