JS实现放大镜(面向对象方式)

代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            margin: 0px;
            padding: 0px;
        }
        #head1 {
            width: 2000px;
            height:700px;
        }
        #small{
            margin-top: 150px;
            margin-left: 350px;
            width: 300px;
            height: 200px;
            border:1px solid #eee;
            border-radius: 4px;
            position: relative;
        }

        #small img{
            width: 100%;
            height: 100%;
        }

        div    {
            float: left;
            /* margin-right: 10px; */
        }

        #big{
            margin-top: 150px;
            margin-left: 20px;
            width: 300px;
            height: 200px;
            overflow: hidden;
            position: relative;
            border:1px solid #eee;
            border-radius: 4px;
            display: none;
        }

        #look{
            width: 300px;
            height: 200px;
            position: absolute;
            left: 0px;
            top:0px;
        }

        #move{
            width: 100px;
            height: 100px;
            background:#000;
            opacity: .5;
            position: absolute;
            display: none;
            left: 0px;
            top: 0px;
        }
    </style>
</head>
<body>
        <ul id="head1"></ul>
        <!-- <div id="small">
            <img src="../新建文件夹/3.jpg">
            <p id="move"></p>
        </div>
        <div id="big">
            <img src="../新建文件夹/3.jpg" id="look">
        </div> -->
        <script>
            //左边小框
            (function(){
               function Small(width,height,url){
                  this.width = width;
                  this.height = height;
                  this.url = url;
               }
               Small.prototype.init = function(){
                   this.createElement();
               }
               Small.prototype.createElement = function(){
                   this.smallBox = document.createElement('div');
                   document.getElementById('head1').appendChild(this.smallBox);
                   this.smallBox.style.width = this.width;
                   this.smallBox.style.height = this.height;
                   this.smallBox.id = 'small';
                   this.imageBox = document.createElement('img');
                   this.imageBox.style.width = this.width;
                   this.imageBox.style.height = this.height;
                   this.imageBox.src = this.url;
                   this.smallBox.appendChild(this.imageBox);
                   this.moveBox = document.createElement('p');
                   this.moveBox.id = 'move';
                   this.smallBox.appendChild(this.moveBox);

               }
               window.Small = Small;
            })();

            //右边放大框
            (function(){
                function Big(width,height,bigWidth,bigHeight,url){
                   this.width = width;
                   this.height = height;
                   this.bigWidth = bigWidth;
                   this.bigHeight = bigHeight;
                   this.url = url;
                }
                Big.prototype.init = function(){
                   this.createElement();
               }
                Big.prototype.createElement = function(){
                   this.bigBox = document.createElement('div');
                   document.getElementById('head1').appendChild(this.bigBox);
                   this.bigBox.style.width = this.width;
                   this.bigBox.style.height = this.height;
                   this.bigBox.id = 'big';
                   this.imageBox = document.createElement('img');
                   this.imageBox.style.width = this.bigWidth;
                   this.imageBox.style.height = this.bigHeight;
                   this.imageBox.id = 'look';
                   this.imageBox.src = this.url;
                   this.bigBox.appendChild(this.imageBox);
               }
               window.Big = Big;
            })();


            (function(){
                function Large(option){
                    this.width = option.width;
                    this.height = option.height;
                    this.bigWidth = option.bigWidth;
                    this.bigHeight = option.bigHeight;
                    this.url = option.url;
                }
                Large.prototype.init = function(){
                    this.createElement();
                    this.mouseMove();
                }
                Large.prototype.createElement = function(){
                    this.smaller = new Small(this.width,this.height,this.url);
                    console.log(this.smaller)
                    this.smaller.init();
                    this.big = new Big(this.width,this.height,this.bigWidth,this.bigHeight,this.url);
                    this.big.init();
                }
                Large.prototype.mouseMove = function(){
                    this.small = document.getElementById('small');
                    this.bigger = document.getElementById('big');
                    this.move = document.getElementById('move');
                    console.log(move)
                    this.look = document.getElementById('look');
                    this.small.onmousemove = function(event){
                        event = event || window.event;
                        this.small.style.cursor = 'move';
                        var move_left = event.clientX - this.small.offsetLeft - this.move.offsetWidth/2;
                        // 计算move移动块的top值
                        console.log(move_left)
                        var move_top = event.clientY - this.small.offsetTop - this.move.offsetHeight/2;
                        // 超出左边界赋值为0
                        move_left = move_left < 0 ? 0 : move_left;
                        // 超出右边界赋值为盒子宽度-移动块的宽度
                        move_left = move_left > this.small.offsetWidth - this.move.offsetWidth ? this.small.offsetWidth - this.move.offsetWidth : move_left;
                        // 超出上边界赋值为0
                        move_top = move_top < 0 ? 0 : move_top;
            
                        move_top = move_top > this.small.offsetHeight - this.move.offsetHeight ? this.small.offsetHeight - this.move.offsetHeight : move_top;
                        this.move.style.left = move_left + 'px';
                        // console.log(this.move.style.left)
                        this.move.style.top = move_top + 'px';
                    

                        var big_x = move_left/(this.small.offsetWidth-this.move.offsetWidth) * (this.look.offsetWidth-this.bigger.offsetWidth);
                        var big_y = move_top/(this.small.offsetHeight-this.move.offsetHeight) * (this.look.offsetHeight-this.bigger.offsetHeight);
                        // 修改图片定位
                        this.look.style.left = -big_x + 'px';
                        this.look.style.top = -big_y + 'px';
                    }.bind(this);
           
                    this.small.onmouseover = function(){
                        // var move = document.getElementById('move');
                        
                        this.move.style.display = 'block';
                        this.bigger.style.display = 'block';
                        // console.log(this.bigger)
                    }.bind(this);
                
                    this.small.onmouseout = function(){
                        this.move.style.display = 'none';
                        this.bigger.style.display = 'none';
                    }.bind(this);
                }
                window.Large = Large;
            })();
            var lar = new Large(
                {width:'300px',
                height:'200px',
                bigWidth:'600px',
                bigHeight:'400px',
                url:'../新建文件夹/3.jpg'});
            lar.init();
        </script>
</body>
</html>

演示

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值