其他分享
首页 > 其他分享> > JS练习_鼠标移入显示大图

JS练习_鼠标移入显示大图

作者:互联网

演示:

源码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>随鼠标显示大图</title>
    <style>
        .picture-list img{
            width: 320px;
            height: 160px;
        }
        .big-picture img{
            width: 640px;
            height: 320px;
        }
        .big-picture{
            position: absolute;// 绝对定位
        }


    </style>
</head>
<body>
<div class="picture-list">
    <img src="images/桌面壁纸1.jpg" alt="">
    <img src="images/桌面壁纸2.jpg" alt="">
    <img src="images/桌面壁纸3.jpg" alt="">
</div>
<div class="big-picture">

</div>
<script>
    let imgList = document.querySelectorAll('.picture-list img');
    let bigPicture = document.querySelector('.big-picture');
    let picList = document.querySelector('.picture-list')
    for (let i in imgList){
        imgList[i].onmouseenter = function (){
            console.log(this.src);
            bigPicture.innerHTML = `<img src='${this.src}'>`;
        }
        imgList[i].onmouseleave = function (){
            bigPicture.innerHTML = '';
        }
    }
    picList.onmousemove = function (e){
        let x = e.clientX;
        let y = e.clientY;
        // 加上移动像素使得图片与鼠标不会相互覆盖
        bigPicture.style.top = y+5+"px";
        bigPicture.style.left =x+5+"px";
    }
</script>
</body>
</html>

标签:picture,鼠标,img,大图,imgList,JS,let,bigPicture,big
来源: https://www.cnblogs.com/zhuhukang/p/15316333.html