js实现放大镜效果

更新日期: 2019-08-07阅读: 1.6k标签: 效果

使用电脑逛淘宝,京东等商城时,将鼠标移入图片中,图片会放大,之前一直在想这种是怎么实现的,前两天刚写出来,纯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://www.fly63.com/article/detial/5179

使用 JavaScript 实现分屏视觉效果

今天这篇文章就来讲讲使用JavaScript来实现这种分屏的视觉UI效果。现在在网站上这种分屏视觉效果应用的也非常广泛,比如 Corsair website。

CSS3的过渡效果,使用transition实现鼠标移入/移出效果

在css中使用伪类虽然实现了样式的改变,但由于没有过渡效果会显得很生硬。以前如果要实现过渡,就需要借助第三方的js框架来实现。现在只需要使用CSS3的过渡(transition)功能,就可以从一组样式平滑的切换到另一组样式。

js如何实现新手引导效果?

js最近有个小伙伴问到了怎么实现新手引导的效果,然后便去网上找了下实现方案。可以通过css的border来实现。

css3 斜切角/斜边的实现方式

设计图含有斜切角的效果时,我们一般想到的方法是切出四个角为背景,然后用border连起来,这样就能显示出该效果了,那么直接使用css呢?下面就整理css做斜边的效果。

JavaScript 实现打字机效果,跑马灯效果

这篇文章在不使用任何插件的情况,以最简洁的原生javascript来实现打字机效果和跑马灯效果。打字效果即把一段话一个字一个字的显示出来。

CSS遮罩效果(模糊效果,阴影效果,毛玻璃效果)

一般遮罩加上透明度opacity就是阴影效果。阴影效果和一般遮罩一样,唯一不同的是设置.mask遮罩的背景色用rgba()表示,当然hsla()也是可以的。模糊效果(毛玻璃效果) 通过 filter来实现

纯css实现气泡效果

主要运用的是1.border 组成的直角三角形。2,before 和 after 伪元素 。3,z-index属性;将元素的长宽设置为0,并且将border的3条边设置为透明的,就会出现border颜色的直角三角形

css文字选中效果

文字选中效果,这个可能很少有人注意过。在默认状态先一般选中的文本颜色是白字蓝底的,不过可以通过CSS进行设置。::selection定义元素上的伪选择器,以便在选定元素时设置其中文本的样式。

text-fill-color:仿苹果官网介绍效果 CSS设置文字渐变效果 文字背景图遮罩

发布iPhone XR的时候 各种心动 去官网看了一遍又一遍。闲着无聊发现 里面的介绍很用大篇幅的有背景文字来介绍。Like this:看着挺酷炫的还不错 就看了下实现方式。还挺简单的。

Vue 中多个元素、组件的过渡,及列表过渡

多元素之间如何实现过渡动画效果呢?这么写行不行呢?肯定是不行的,因为 Vue 在两个元素进行切换的时候,会尽量复用dom,就是因为这个原因,导致现在动画效果不会出现。如果不让 Vue 复用dom的话,应该怎么做呢?只需要给这两个div不同的key值就行了

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!