js实现放大镜效果

摘要:使用电脑逛淘宝,京东等商城时,将鼠标移入图片中,图片会放大,之前一直在想这种是怎么实现的,前两天刚写出来,纯js实现的,无任何工具库。下面先来看下思路吧!刚学js的时候可能对于布局不是很重要

使用电脑逛淘宝,京东等商城时,将鼠标移入图片中,图片会放大,之前一直在想这种是怎么实现的,前两天刚写出来,纯js实现的,无任何工具库。下面先来看下思路吧!刚学js的时候可能对于布局不是很重要,但学到面向对象编程后,布局就变得很重要了,有时候布局会影响到整体效果;先来看下布局吧!

<div class="photo">
        <div class="sbox">
            <img src="http://img0.imgtn.bdimg.com/it/u=4175582523,2707192513&fm=15&gp=0.jpg">
            <span></span>
        </div>
        <div class="bbox">
            <img src="http://img0.imgtn.bdimg.com/it/u=4175582523,2707192513&fm=15&gp=0.jpg">
        </div>
        <div class="nav">
            <span> <</span>
            <img src="http://img0.imgtn.bdimg.com/it/u=4175582523,2707192513&fm=15&gp=0.jpg" class="selected">
            <img src="http://img4.imgtn.bdimg.com/it/u=181188734,374783636&fm=15&gp=0.jpg">
            <img src="http://img2.imgtn.bdimg.com/it/u=2136674516,3472494802&fm=15&gp=0.jpg">
            <img src="http://img0.imgtn.bdimg.com/it/u=3344949169,188332301&fm=15&gp=0.jpg">
            <span>></span>
        </div>
    </div>


    <style>
        .photo {
            width: 98%;
            height: 500px;
            margin: 20px auto;
            border: 1px solid black;
            position: relative;
        }

        .sbox {
            width: 600px;
            height: 300px;
            position: absolute;
            top: 20px;
            left: 20px;
        }

        .sbox img {
            width: 600px;
            height: 300px;
        }

        .sbox span {
            position: absolute;
            background-color: rgba(199, 199, 199, .5);
            top: 0;
            display: none;
            left: 0;
        }

        .bbox {
            width: 600px;
            height: 300px;
            overflow: hidden;
            position: absolute;
            top: 20px;
            left: 630px;
            display: none;
        }

        .bbox img {
            width: 1200px;
            height: 600px;
            position: absolute;
            top: 0;
            left: 0;
        }

        .nav {
            width: 300px;
            height: 32px;
            position: absolute;
            overflow: hidden;
            bottom: 20px;
            left: 20px;

        }

        .nav img {
            width: 60px;
            height: 30px;
            border: 1px solid #dddddd;
        }

        .nav img:first-of-type {
            margin-left: 20px;
        }

        .nav span {
            position: absolute;
            width: 16px;
            height: 32px;
            text-align: center;
            line-height: 30px;
            background-color: #cccccc;
            top: 0;
            cursor: pointer;
        }

        .nav span:last-child {
            right: 0;
        }

        .nav .selected {
            border: 1px solid #e48c63;
        }
    </style>


布局就搞定了,比较简单的哈。下面就是一个思路吧

  1. 选择元素
  2. 鼠标进入
    1. 显示span和大图
    2. 计算span的宽高
  3. 鼠标移出
    1. 隐藏span和大图
  4. 鼠标移动
    1. span跟随鼠标移动
    2. span的边界限定
    3. 计算比例
    4. 大图跟随小图移动

大体思路就是这样的,个人建议是根据思路直接自己写,实在不会可以参照我的代码哈!因为自己写出来的和看了别人代码再写出来的感觉不太一样的。好啦,下面就是js部分的代码!

<script>
    // 创建函数
    function Big() {
        // 获取元素
        this.span = document.querySelector(".sbox span");
        this.sbox = document.querySelector(".sbox");
        this.bbox = document.querySelector(".bbox");
        this.simg = document.querySelector(".sbox img");
        this.bimg = document.querySelector(".bbox img");
        this.img = document.querySelectorAll(".nav img");
        this.init();
    }
    Big.prototype.init = function () {
        // this的指向问题
        var that = this;
        for (var i = 0; i < this.img.length; i++) {
            this.img[i].index = i;
        // 点击导航图
            this.img[i].onclick = function () {
                for (var j = 0; j < that.img.length; j++) {
                    // 清除默认样式
                    that.img[j].className = "";
                }
                // 给当前的图片加样式
                that.img[this.index].className = "selected";
                that.index = this.index;
                // 将当前图放入主体框中
                that.changeImg();
            }
        }
        // 鼠标移入事件
        this.sbox.onmouseover = function () {
            // 显示
            that.span.style.display = "block";
            that.bbox.style.display = "block";
            // 计算span宽高
            that.span.style.width = that.bbox.offsetWidth / that.bimg.offsetWidth * that.sbox.offsetWidth + "px";
            that.span.style.height = that.bbox.offsetHeight / that.bimg.offsetHeight * that.sbox.offsetHeight + "px";
        }
        // 鼠标移动
        this.sbox.onmousemove = function () {
            that.move();
        }
        // 鼠标移出
        this.sbox.onmouseout = function () {
            // 隐藏
            that.span.style.display = "none";
            that.bbox.style.display = "none";
        }
    }
    // 改变图片
    Big.prototype.changeImg = function () {
        this.simg.src = this.img[this.index].src;
        this.bimg.src = this.img[this.index].src;
    }
    // 移动
    Big.prototype.move = function () {
        this.span.style.left = event.clientX - this.sbox.offsetLeft - this.span.offsetWidth / 2 + "px";
        this.span.style.top = event.clientY - this.sbox.offsetTop - this.span.offsetHeight / 2 + "px";
        // 边界限定
        if (this.span.offsetLeft < 0) {
            this.span.style.left = 0;
        }
        if (this.span.offsetTop < 0) {
            this.span.style.top = 0;
        }
        if (this.span.offsetLeft > this.sbox.offsetWidth - this.span.offsetWidth) {
            this.span.style.left = this.sbox.offsetWidth - this.span.offsetWidth + "px";
        }
        if (this.span.offsetTop > this.sbox.offsetHeight - this.span.offsetHeight) {
            this.span.style.top = this.sbox.offsetHeight - this.span.offsetHeight + "px";
        }
        // 计算比例
        var num=this.bbox.offsetWidth/this.bimg.offsetWidth;
        console.log(num);
        // 大图移动
        this.bimg.style.left=-(this.span.offsetLeft/num)+"px";
        this.bimg.style.top=-(this.span.offsetTop/num)+"px";
    }
    new Big();
</script>




本文内容仅供个人学习、研究或参考使用,不构成任何形式的决策建议、专业指导或法律依据。未经授权,禁止任何单位或个人以商业售卖、虚假宣传、侵权传播等非学习研究目的使用本文内容。如需分享或转载,请保留原文来源信息,不得篡改、删减内容或侵犯相关权益。感谢您的理解与支持!

链接: https://shenqiku.cn/article/FLY_5179