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>
posted @ 2021-09-21 12:11  博客zhu虎康  阅读(336)  评论(0编辑  收藏  举报