美文网首页
js 放大镜功能实现

js 放大镜功能实现

作者: 琳媚儿 | 来源:发表于2020-01-31 11:55 被阅读0次

css

<style>
        #boxs {
            width: 200px;
            height: 200px;
            position: relative;
        }

        #boxs img {
            position: relative;
            width: 200px;
            height: 200px;
            border: 1px solid #cccccc;
        }

        #box {
            position: absolute;
            display: none;
            top: 10px;
            width: 150px;
            height: 150px;
            background-color: #fede4f;
            opacity: .5;
            cursor: move;
        }

        #big {
            position: absolute;
            display: none;
            left: 400px;
            top: 10px;
            width: 400px;
            height: 400px;
            background-color: thistle;
            overflow: hidden;
            z-index: 999;
            border: 1px solid #cccccc;
        }



        #big img {
            width: 600px;
            height: 600px;
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>

index.html

<body>
    <div id="boxs">
        <img src="http://a2.att.hudong.com/36/48/19300001357258133412489354717.jpg" alt="">
        <div id="box"></div>
        <div id="big">
            <img id="bigimg" src="http://a2.att.hudong.com/36/48/19300001357258133412489354717.jpg" alt="">
        </div>
    </div>
</body>

main.js

window.addEventListener('load', function () {
    var boxs = document.querySelector('#boxs');
    var box = document.querySelector('#box');
    var big = document.querySelector('#big');
    boxs.addEventListener('mouseover', function () {
        box.style.display = 'block';
        big.style.display = 'block';
    })
    boxs.addEventListener('mouseout', function () {
        box.style.display = 'none';
        big.style.display = 'none';
    })
    boxs.addEventListener('mousemove', function (e) {
        var x = e.pageX - this.offsetLeft;
        var y = e.pageY - this.offsetTop;
        var boxX = x - box.offsetWidth / 2;
        var boxY = y - box.offsetHeight / 2;
        // 遮挡层最大移动距离
        var boxMax = boxs.offsetWidth - box.offsetWidth;
        if (boxX <= 0) {
            boxX = 0;
        } else if (boxX >= boxMax) {
            boxX = boxMax;

        }
        if (boxY <= 0) {
            boxY = 0;
        } else if (boxY >= boxMax) {
            boxY = boxMax;
        }
        box.style.left = boxX + 'px';
        box.style.top = boxY + 'px';
        // 大图片的移动距离=遮挡层移动距离*大图片最大移动距离/遮挡层的最大移动距离
        var bigImg = document.querySelector('#bigimg');
        var bigMax = bigImg.offsetWidth - big.offsetWidth;
        var bigX = boxX * bigMax / boxMax;
        var bigY = boxY * bigMax / boxMax;
        bigImg.style.left = -bigX + 'px';
        bigImg.style.top = -bigY + 'px';
    })
})
e8b5985c-ab04-4635-a9dc-d4762bee35c0.jpg

相关文章

  • 2018-12-01

    JS实现放大镜功能 网店中的商品都能放大查看细节,这里是放大功能的简单实现。。。。 这个放大镜具有选择放大倍数的功...

  • js 放大镜功能实现

    css index.html main.js

  • js实现放大镜

    效果图 实现原理 借助宽高等比例放大的两张图片,结合js中鼠标偏移量、元素偏移量、元素自身宽高等属性完成;左侧遮罩...

  • JS实现@功能

    最近公司的PC端即时通讯工具需要添加@功能,整体软件采用的是Electron+Node.js来编写的,其实功能并不...

  • js 实现语音播报

    参考 js实现语音播报功能

  • js实现放大镜详解

    解决问题流程 原理

  • js实现放大镜效果

    前言:在拿到一个需求之前,我们要先进行分析,再设计,最后实现代码的编写以及调试。 放大镜的原理分析 鼠标在小图片上...

  • JS实现放大镜效果

    HTML部分: CSS部分: JS部分:

  • 文件模块

    创建 Node.js 命令行项目 安装commander 实现创建功能 index.js db.js 完成所有功能...

  • 【React】Get Started

    1. React之初衷 首先用原生JS实现一个简单的功能: 使用React实现同样的功能: 2. 原生js VS ...

网友评论

      本文标题:js 放大镜功能实现

      本文链接:https://www.haomeiwen.com/subject/smlgthtx.html