JS实现图片放大镜效果

实现效果如下图:

 

//HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css.css">
    <script src="js.js"></script>
</head>
<body>
    <div class="demo">
        <img src="image/image.jpg" alt="" class="demo_img">
        <div class="demo_mouse"></div>
        <div class="demo_enlarge">
            <img src="image/image.jpg" alt="" class="demo_enlarge_img">
        </div>
    </div>
</body>
</html>
//CSS代码
.demo{
    position: relative;
    width: 300px;
    height: 300px;
    background-color: #eee;
    cursor: pointer;
}
.demo_img{
    width: 100%;
    height: 100%;
    /* object-fit: contain; */

}
.demo>.demo_mouse{
    display: none;
    width: 100px;
    height: 100px;
    left: 0;
    top: 0;
    position: absolute;
    background-color: #ff8f00;
    opacity: 0.3;
}
.demo>.demo_enlarge{
    display: none;
    width: 500px;
    height: 500px;
    left: 320px;
    top: 0;
    /* background-color: #ff8f00; */
    position: absolute;
    overflow: hidden;
    /* background-color: #ff8f00; */
    /* opacity: 0.5; */
}
.demo_enlarge_img{
    position: absolute;
    top: 0;
    left: 0;
    width: 1500px;
    height: 1500px;
}
//JS代码
window.addEventListener('load', function () {
    let odemo = document.querySelector(".demo")
    let odemo_mouse = document.querySelector(".demo_mouse")
    let odemo_enlarge = document.querySelector(".demo_enlarge")
    let odemo_enlarge_img =document.querySelector(".demo_enlarge_img")
    odemo.addEventListener("mouseover", function () { enterShow("show") })
    odemo.addEventListener("mouseout", function () { enterShow("close") })
    odemo.addEventListener("mousemove", function (e) { let that = this; getLocation(that, e) })
    function enterShow(add) {
        if (add == "show") {
            odemo_mouse.style.display = "block";
            odemo_enlarge.style.display = "block";
        } else {
            odemo_mouse.style.display = "none";
            odemo_enlarge.style.display = "none";
        }

    }
    function getLocation(that, e) {//鼠标只要移动就触发此方法
        let X = e.pageX - that.offsetLeft  //拿到鼠标在.demo中的位置
        let Y = e.pageY - that.offsetTop    //拿到鼠标在.demo中的位置
        let maskX = X - odemo_mouse.offsetWidth / 2   // /2是为了控制鼠标在黄色阴影中心位置
        let maskY = Y - odemo_mouse.offsetHeight / 2   // /2是为了控制鼠标在黄色阴影中心位置
        if (maskX < 0) { //防止黄色阴影跑出.demo外(设置黄色阴影的移动区间)
            maskX = 0
        } else if (maskX > odemo.offsetWidth - odemo_mouse.offsetWidth) {
            maskX = odemo.offsetWidth - odemo_mouse.offsetWidth
        }
        if (maskY < 0) {
            maskY = 0
        } else if (maskY > odemo.offsetHeight- odemo_mouse.offsetHeight) {
            maskY = odemo.offsetHeight - odemo_mouse.offsetHeight
        }
        odemo_mouse.style.top = maskY + "px" //将黄色阴影的Top移动到鼠标位置
        odemo_mouse.style.left = maskX + "px" //将黄色阴影的Top移动到鼠标位置
        let bigMax =odemo_enlarge_img.offsetWidth-odemo_enlarge.offsetWidth
        let bigX =maskX*bigMax/(odemo.offsetWidth - odemo_mouse.offsetWidth)
        let bigY =maskY*bigMax/(odemo.offsetHeight - odemo_mouse.offsetHeight)
        odemo_enlarge_img.style.left=-bigX+"px"
        odemo_enlarge_img.style.top=-bigY+"px"
    }
})

HTML与CSS代码没有需要注意的地方,关键在于JS代码的书写

首先利用JS获取到关键的DOM节点

此功能分为3点:

        1.鼠标移入显示黄色阴影部分,鼠标移出黄色阴影部分消失.

            

               这里值得注意的是mouseover 与mouseout两个方法的使用

 

        2.鼠标移动,黄色阴影部分跟随移动

               

                这里值得注意的是pageX(获取鼠标的X坐标)offsetLeft(获取近的带有定位父级的左边的距离 )offsetWidth(元素的宽度width+左右padding+左右边框)

 

        3.右边显示出放大部分(只显示放大阴影部分才对)

        

         放大的核心是放大视口固定不变(即.demo_enlarge固定不变,图片移动)

        

 是这样一种逻辑思路,当黄色阴影移动,大图片会反方向移动相同比例

求大图片移动距离可利用:

 

书写可能不详细,如又疑问可留言,知无不答!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值