图片放大镜

图片放大镜

此案例代码用到的技术栈有vue2、jquery等
下载安装jQuery的命令如下

npm install jquery

代码如下

<template>
  <div>
    <!-- <h3>放大镜案例</h3> -->

    <!-- 放大镜 -->
    <div class="loupe-box">
      <div class="img-box">
        <img :src="imgUrlValue" alt="图片" />
        <div class="slide"></div>
        <div class="mark"></div>
        <div class="view"></div>
      </div>
      <!-- 图片盒子 -->
      <div class="seamless-warp3">
        <span class="left-arrow" @click="clickLeftArrow"></span>
        <span class="right-arrow" @click="clickRightArrow"></span>

        <div class="slideshow-outer-box">
          <div class="slideshow-box">
            <div
              v-for="(item, index) in slideList"
              class="img-item-box"
              :key="item"
              @mouseover="
                (event, imgUrl) => mouseoverSlideHandler(event, item, index)
              "
            >
              <img style="width: 100%; height: 100%" :src="item" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $ from 'jquery'
export default {
  data() {
    return {
      slideList: [
        "https://img2.baidu.com/it/u=3147571584,2473606295&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
        "https://img13.360buyimg.com/n1/s150x150_jfs/t1/146619/4/38072/146082/64996004F27ab3fcf/438de053a6d14f47.jpg",
        "https://img14.360buyimg.com/n1/s150x150_jfs/t1/45273/11/23686/153723/649960c8Fd950f334/0f2a9a08729c92ea.jpg",
        "https://img13.360buyimg.com/n1/s150x150_jfs/t1/54800/18/24153/237713/64acc183F668e310c/c8e702715b166e70.jpg",
        "https://img13.360buyimg.com/n1/s150x150_jfs/t1/147852/2/34285/219955/648184f7Fee9bd8c3/958a54714beffa0e.jpg",
        "https://img12.360buyimg.com/n1/s150x150_jfs/t1/218622/3/35340/219866/64ccad2bFa48c85d8/e3d667e5c5e84100.jpg",
        "https://img13.360buyimg.com/n1/jfs/t1/107637/3/37127/306072/6419a5b0F8b2c5ca2/89b71bb9fdf05cd1.jpg",
      ],
      // 需要放大的图
      imgUrlValue: "",
      clickLeftNum: 1,
      clickRightNum: 1,
    };
  },
  mounted() {
    this.imgUrlValue = this.slideList[0];
    // 初始化图片容器长度
    $(".slideshow-box").css("width", this.slideList.length * 62 + "px");
    this.loupeInit();
  },
  methods: {
    // 左边箭头
    clickLeftArrow() {
      // 图片视口和图片容器左边到浏览器距离小于等于则禁止点击
      // 进行点击
      if (
        $(".slideshow-outer-box").offset().left <=
        $(".slideshow-box").offset().left
      ) {
        // $('.slideshow-box').css("transform",`translate(${this.clickLeftNum * 62}px, 0px)`)
        // $('.slideshow-box').css("transition",`all 400ms linear 0s`)
        // 进行点击右边箭头样式
        $(".left-arrow").hover(
          function () {
            $(this).css("background-color", "#b9b9b9");
          },
          function () {
            $(this).css("background-color", "#00a0e9");
          }
        );

        return;
      }
      // 进行点击
      else {
        $(".slideshow-box").css(
          "transform",
          `translate(${this.clickLeftNum * 62}px, 0px)`
        );
        $(".slideshow-box").css("transition", `all 400ms linear 0s`);
        $(".right-arrow:hover").css("background-color", "#0F39FA");
        // 禁止点击右边箭头样式  鼠标移入  鼠标移出
        $(".right-arrow").hover(
          function () {
            $(this).css("background-color", "#0f39fa");
          },
          function () {
            $(this).css("background-color", "#00a0e9");
          }
        );
      }

      this.clickLeftNum += 1;
      this.clickRightNum -= 1;
    },
    // 右边箭头
    clickRightArrow() {
      // 总移动的距离小于可视区盒子则可进行点击否则禁止点击
      // if( ($('.slideshow-box').get(0).offsetWidth - this.clickRightNum * 62) >= $('.slideshow-outer-box').get(0).offsetWidth ) {
      // 图片视口和图片容器右边到浏览器距离小于则进行点击
      if (
        $(".slideshow-outer-box").offset().left +
          $(".slideshow-outer-box").outerWidth() <
        $(".slideshow-box").offset().left + $(".slideshow-box").outerWidth()
      ) {
        $(".slideshow-box").css(
          "transform",
          `translate(${this.clickRightNum * -62}px, 0px)`
        );
        $(".slideshow-box").css("transition", `all 400ms linear 0s`);

        // 可进行点击左边样式
        $(".left-arrow").hover(
          function () {
            $(this).css("background-color", "#0f39fa");
          },
          function () {
            $(this).css("background-color", "#00a0e9");
          }
        );
      }
      // 禁止点击
      else {
        // 禁止点击箭头样式  鼠标移入  鼠标移出
        $(".right-arrow").hover(
          function () {
            $(this).css("background-color", "#b9b9b9");
          },
          function () {
            $(this).css("background-color", "#00a0e9");
          }
        );
        return;
      }
      this.clickRightNum += 1;
      this.clickLeftNum -= 1;
    },
    // 鼠标移上图片
    mouseoverSlideHandler(event, imgUrl, index) {
      this.imgUrlValue = imgUrl;
      // 循环排他
      $(".img-item-box").each(function (index1, element) {
        $(element).css("border", "none");
        if (index == index1) {
          $(element).css("border", "1px solid red");
        }
      });
      // 鼠标移上去切换放大镜图片
      $(".big-img").get(
        0
      ).style.background = `url(${this.imgUrlValue}) no-repeat 0 0/cover`;
    },
    // 放大镜操作
    loupeInit() {
      // 容器和容器的宽高
      var box = $(".img-box").get(0);
      var boxW = box.offsetWidth;
      var boxH = box.offsetHeight;

      // 滑块和滑块的宽高
      var slide = $(".slide").get(0);
      var slideW = slide.offsetWidth;
      var slideH = slide.offsetHeight;

      // 初始隐藏滑块和放大镜
      $(".slide").css("display", "none");

      // 设置鼠标移上去显示滑块 移除隐藏滑块
      $(".img-box").hover(
        function () {
          $(".slide").css("display", "block");
          $(".view").css("display", "block");
        },
        function () {
          $(".slide").css("display", "none");
          $(".view").css("display", "none");
        }
      );

      // 获取大的滑块
      var view = $(".view").get(0);
      var viewW = view.offsetWidth; //500
      var viewH = view.offsetHeight; //500

      // 计算比例:  大滑块/小滑块 = 大图片/小图片
      var calc = viewW / slideW;
      // console.log(calc);

      // 根据比例创建大图片
      var bigImg = $("<div>").get(0);
      bigImg.style.width = boxW * calc + "px";
      bigImg.style.height = boxH * calc + "px";
      bigImg.style.background = `url(${this.imgUrlValue}) no-repeat 0 0/cover`;
      bigImg.style.position = "absolute";
      bigImg.className = "big-img";
      view.appendChild(bigImg);
      // 隐藏放大镜
      $(".view").css("display", "none");

      box.onmousemove = function (e) {
        // 鼠标的坐标
        // console.log(e.target, "=>", e.offsetX, e.offsetY);  // 鼠标在大盒子, 鼠标在小盒子

        // 把滑块的中心, 挪动到鼠标的坐标
        var left = e.offsetX - slideW / 2;
        var top = e.offsetY - slideH / 2;

        // 边界控制 left和top能到达的最大值
        var MAX_LEFT = boxW - slideW;
        var MAX_TOP = boxH - slideH;

        // 边界控制
        left = left < 0 ? 0 : left > MAX_LEFT ? MAX_LEFT : left;
        top = top < 0 ? 0 : top > MAX_TOP ? MAX_TOP : top;

        // 黄色滑块移动
        slide.style.left = left + "px";
        slide.style.top = top + "px";

        // view显示的大图片的移动
        bigImg.style.left = -left * calc + "px";
        bigImg.style.top = -top * calc + "px";
      };
    },
  },
};
</script>

<style lang="less" scoped>
.loupe-box {
    // margin: 0 auto;
    margin-left: 100px;
  margin-right: 50px;
  width: 390px;

  .img-box {
    // width: 330px;
    // margin-right: 20px;
    width: 390px;
    height: 390px;
    position: relative;

    img {
      width: 100%;
      height: 100%;
      // z-index: 100;
    }

    // 滑块
    .slide {
      width: 237px;
      height: 237px;
      background-color: #f5cd23;
      opacity: 0.4;

      position: absolute;
      left: 0;
      top: 0;
    }

    // 遮罩层
    .mark {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      background: transparent;
    }

    // 放大视口
    .view {
      z-index: 100;
      width: 500px;
      height: 500px;
      // float: left;
      position: absolute;
      left: 249px;
      top: 0px;
      outline: 1px solid #ccc;
      margin-left: 150px;
      // position: relative;
      overflow: hidden;
    }
  }

  .seamless-warp3 {
    // overflow: hidden;
    height: 54px;
    width: 100%;
    // width: 62px * 5;
    margin: 0 auto;
    margin-top: 24px;
    position: relative;

    .slideshow-outer-box {
      overflow: hidden;
      height: 54px;
      width: 62px * 5;
      margin: 0 auto;
    }

    .left-arrow,
    .right-arrow {
      // position: relative;
      position: absolute;
      z-index: 1000;
      display: inline-block;
      // width: 40px;
      // height: 40px;
      width: 30px;
      height: 30px;
      border-radius: 50%;
      background-color: #00a0e9;
      cursor: pointer;

      &:hover {
        background-color: #0f39fa;
      }

      &::before {
        position: absolute;
        content: "";
        /* width: 16px;
                        height: 16px;
                        top: 12px;
                        left: 15px; */

        width: 10px;
        height: 10px;
        top: 10px;
        left: 12px;
        border: 2px solid #fff;
      }
    }

    .left-arrow {
      top: calc(50% - 15px);
      left: -4px;
    }

    .right-arrow {
      top: calc(50% - 15px);
      right: 0px;
    }

    .left-arrow::before {
      border-right: 0;
      border-bottom: 0;
      transform: rotate(-45deg);
    }

    .right-arrow::before {
      position: absolute;
      border-left: 0;
      border-top: 0;
      left: 9px;
      transform: rotate(-45deg);
    }

    .slideshow-box {
      width: 62px * 10;
      height: 54px;
      .img-item-box {
        float: left;
        width: 54px;
        height: 54px;
        line-height: 54px;
        background-color: #999;
        color: #fff;
        text-align: center;
        font-size: 14px;
        margin-right: 8px;
      }
      .img-item-box:hover {
        border: 1px solid red;
      }
    }
  }
}
</style>

案例效果如图下

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值