放大镜效果的实现js

<div id="box1">
    <img src="../day17/img/WaterF6.jpg" alt="">
    <span></span>
</div>
<div id ="box2">
    <img src="../day17/img/WaterF6.jpg" alt=""></div>
<ul>
    <li><img src="../day17/img/WaterF6.jpg" alt=""></li>
    <li><img src="../day17/img/WaterF5.jpg" alt=""></li>
</ul>


class big{
// 选择元素
constructor(){
    this.Box1 = document.getElementById("box1");
    this.img1 = document.querySelector("#box1 img");
    this.span1 = document.querySelector("#box1 span");
    this.Box2 = document.getElementById("box2");
    this.img2 = document.querySelector("#box2 img");
    this.li = document.querySelectorAll("ul li");
}
// 绑定事件
shijian(){
    var that =this;
    // 鼠标进入  显示元素
    this.Box1.onmouseover =function(){
        that.over();
    }
    // 鼠标移动
    this.Box1.onmousemove =function(eve){
        var e = eve || window.event;
        that.move(e);
    }
    // 鼠标离开 隐藏元素
    this.Box1.onmouseout =function(){
        that.out();
    }
    // 切换图片的点击事件
    for (let i = 0; i < this.li.length; i++) {
        this.li[i].onclick =function(){
            that.img1.src = this.children[0].src;
            that.img2.src = this.children[0].src;
        }
        
    }
}
// 事件方法
// 鼠标进入方法
over(){
  this.span1.style.display = "block";
  this.Box2.style.display = "block";
}
// 鼠标移动方法
move(e){
    var l = e.pageX - this.Box1.offsetLeft-this.span1.offsetWidth/2;
    var t = e.pageY - this.Box1.offsetTop-this.span1.offsetHeight/2;
    if (l<0) l = 0;
    if(t<0)  t = 0;
    if(l>this.Box1.offsetWidth -this.span1.offsetWidth){
        l = this.Box1.offsetWidth -this.span1.offsetWidth;
    }
    if(t>this.Box1.offsetHeight-this.span1.offsetHeight){
        t = this.Box1.offsetHeight-this.span1.offsetHeight;
    }
    // 遮罩层移动
    this.span1.style.left = l + "px";
    this.span1.style.top = t + "px";
    // 计算比例
    var x= l/(this.Box1.offsetWidth -this.span1.offsetWidth);
    var y= t/(this.Box1.offsetHeight-this.span1.offsetHeight);
    // 右侧大图等比例移动
    this.img2.style.left = (this.Box2.offsetWidth - this.img2.offsetWidth) * x + "px";
    this.img2.style.top = (this.Box2.offsetHeight - this.img2.offsetHeight) * y + "px";

}
// 鼠标离开方法
out(){
    this.span1.style.display = "none";
  this.Box2.style.display = "none";
}

}
// 运行
var b =new big();
b.shijian();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值