实现放大镜功能

简单放大镜

简单放大镜

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>放大镜</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #container{
            width: 801px;
            height: 100%;
            display: flex;
            margin: 20px auto;
            border: 1px solid black;
        }
        .left{
            width: 400px;
            border-right: 1px solid black;
        }
        .top{
            height: 300px;
            position: relative;
        }
        .shadow{
            width: 200px;
            height: 200px;
            background: rgba(0, 0, 0, 0.5);
            position: absolute;
            z-index: 1;
            display: none;
        }
        .pic_img img{
            position: absolute;
            display: none;
        }
        .pic_img .show{
            display: block;
        }
        .bottom{
            display: flex;
        }
        .right{
            width: 400px;
            height: 300px;
            overflow: hidden;
            position: relative;
            display: none;
        }
        .right img{
            height: 600px;
            width: 800px;
            position: absolute;
            display: none;
        }
        .right .show{
            display: block;
        }

    </style>
</head>
<body>
    <div id="container">
        <div class="left">
            <div class="top">
                <div class="shadow"></div>
                <div class="pic_img">
                    <img class="show" src="./motoImg/1.jpg" alt="">
                </div>
            </div>
        </div>
        <div class="right">
            <img class="show" src="./motoImg/1.jpg" alt="">
        </div>
    </div>
</body>
<script>
    function $(ele){return document.querySelector(ele)};
    var pic_img = document.querySelector(".pic_img img");
    var right_img = document.querySelector(".right img");

    $(".top").onmouseover = function(e){
        $(".shadow").style.display = "block";
        $(".right").style.display = "block";
        document.onmousemove = function(ev){
            var x = ev.clientX - $("#container").offsetLeft - 100;
            var y = ev.clientY - $("#container").offsetTop - 100;
            x = x > 200 ? 200 : x;
            x = x < 0 ? 0 : x;
            y = y > 100 ? 100 : y;
            y = y < 0 ? 0 : y;

            $(".shadow").style.left = x + "px";
            $(".shadow").style.top = y + "px";
            right_img.style.left = -(x * 2) + "px";
            right_img.style.top = -(y * 2) + "px";
            
        }
    }
    $(".top").onmouseout = function(e){
        $(".shadow").style.display = "none";
        $(".right").style.display = "none";
    }
</script>
</html>
带tab选项卡的放大镜
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #container{
            width: 801px;
            margin: 20px auto;
            border: 1px solid black;
            display: flex;
        }
        .left{
            width: 400px;
            border-right: 1px solid black;
        }
        .top{
            height: 400px;
            position: relative;
        }
        .shadow{
            width: 200px;
            height: 200px;
            z-index: 1;
            display: none;
            position: absolute;
            background-color: rgba(0, 0, 0, 0.5);
        }
        .pic_img img{
            width: 400px;
            height: 400px;
            position: absolute;
            display: none;
        } 
        .pic_img .show{
            display: block;
        }
        .bottom{
            width: 400px;
            display: flex;
        }
        .bottom .ctrl{
            width: 80px;
            height: 80px;
            border: 5px solid #fff;
            background-image: url(./motoImg/1.webp);
            background-size: 100% 100%;
        }
        .bottom .ctrl:nth-of-type(2){
            background-image: url(./motoImg/2.webp);
        }
        .bottom .ctrl:nth-of-type(3){
            background-image: url(./motoImg/3.webp);
        }
        .bottom .ctrl:nth-of-type(4){
            background-image: url(./motoImg/4.webp);
        }
        .bottom .ctrl:nth-of-type(5){
            background-image: url(./motoImg/5.webp);
        }
        .bottom .active{
            border: 5px solid orange;
        }
        .right{
            width: 400px;
            height: 400px;
            position: relative;
            overflow: hidden;
            display: none;
        }
        .big_img img{
            width: 800px;
            height: 800px;
            position: absolute;
            display: none;
        }
        .big_img .show{
            display: block;
        }
    </style>
</head>
<body>
    <!-- 
        通过html+css布局,
        由四部分组成:
            1.要放大的尺寸正常的图片<div class="pic_img"></div>
            2.选项卡<div class="bottom"></div>
            3.选择放大图片位置的遮盖阴影<div class="shadow"></div>
            4.放大图片显示的区域<div class="big_img"></div>
     -->
    <div id="container">
        <!-- 左半部分 -->
        <div class="left">
            <!-- 左半部分上部,要放大的图片部分 -->
            <div class="top">
                <!-- 遮盖阴影部分 -->
                <div class="shadow"></div>
                <!-- 要放大的正常尺寸的图片 -->
                <div class="pic_img">
                    <img class="show" src="./motoImg/1.webp" alt="">
                    <img src="./motoImg/2.webp" alt="">
                    <img src="./motoImg/3.webp" alt="">
                    <img src="./motoImg/4.webp" alt="">
                    <img src="./motoImg/5.webp" alt="">
                </div>
            </div>
            <!-- 左半部分下部,选项卡,控制左上部分要显示的图片 -->
            <div class="bottom">
                <div class="ctrl active"></div>
                <div class="ctrl"></div>
                <div class="ctrl"></div>
                <div class="ctrl"></div>
                <div class="ctrl"></div>
            </div>
        </div>
        <!-- 右半部分,放大的图片显示的位置 -->
        <div class="right">
            <div class="big_img">
                <img class="show" src="./motoImg/1.webp" alt="">
                <img src="./motoImg/2.webp" alt="">
                <img src="./motoImg/3.webp" alt="">
                <img src="./motoImg/4.webp" alt="">
                <img src="./motoImg/5.webp" alt="">
            </div>
        </div>
    </div>
</body>
<script>
    // 封装获取单个DOM对象(通过传入,标签,类名,id....)函数
    function $(ele){return document.querySelector(ele);};

    // 获取选项卡所有对象集合
    var ctrls = document.querySelectorAll(".ctrl");
    // 获取所有要放大的图片的对象的集合
    var pic_imgs = document.querySelectorAll(".pic_img img");
    // 获取放大后的图片的对象的集合
    var big_imgs = document.querySelectorAll(".big_img img");

    // 定义index作为标记,主要记录当前点击的选项卡的索引是第几个图片,然后能在鼠标移入时用到,方便获取,对应放大图片的索引
    var index = 0;
    for(var i = 0; i < ctrls.length; i++){
        // 给每个选项卡对象绑定index属性,记录对应的索引
        ctrls[i].index = i;
        // 为每个选项卡div绑定单击事件
        ctrls[i].onclick = function(){
            // 清空所有图片区域的显示样式
            for(var j = 0; j < ctrls.length; j++){
                ctrls[j].className = "ctrl";
                pic_imgs[j].className = "";
                big_imgs[j].className = "";
            }
            // 为选项卡,图片添加相应触发的样式
            ctrls[this.index].className = "ctrl active";
            // 为要放大的图片绑定相应的class属性,点击选项卡后上方显示相应的要放大的图片
            pic_imgs[this.index].className = "show";
            // 为放大的图片绑定相应的class属性,点击选项卡后放大图片显示区域显示相应放大后的图片
            big_imgs[this.index].className = "show";
            index = this.index;
        }
    }
    // 给类名为top的div绑定鼠标移入事件
    $(".top").onmouseover = function(){
        // 鼠标移入后,遮盖阴影div的样式设为可见
        $(".shadow").style.display = "block";
        // 鼠标移入后,包含放大图片的div的样式设为可见
        $(".right").style.display = "block";
        document.onmousemove = function(ev){
            // 遮盖阴影相对于.top的div的left和top距离
            var x = ev.clientX - $("#container").offsetLeft - 100;
            var y = ev.clientY - $("#container").offsetTop - 100;

            // 通过三目运算控制阴影位置不超过.top的div边框
            x = x > 200 ? 200 : x;
            x = x < 0 ? 0 : x;
            y = y > 200 ? 200 : y;
            y = y < 0 ? 0 : y;

            // 为遮盖阴影块div设定left和top值(相对于.top的div)
            $(".shadow").style.left = x + "px";
            $(".shadow").style.top = y + "px";

            // 设置当前放大图片向左和向右移动的距离,阴影向右移动放大的图片向左移动,阴影模块与放大图片的移动方向相反
            // 由于本放大镜将图片放大2倍,所以放大图片在移动时候是对应阴影遮盖部分div移动的位移的两倍
            big_imgs[index].style.left = -(x * 2) + "px";
            big_imgs[index].style.top = -(y * 2) + "px";
        }
    }
    // 鼠标移出.top的div的范围时触发的事件,将遮盖阴影和右边放大图片区域隐藏
    $(".top").onmouseout = function(){
        // 鼠标移出后,遮盖阴影div的样式设为不可见
        $(".shadow").style.display = "none";
        // 鼠标移出后,包含放大图片的div的样式设为不可见
        $(".right").style.display = "none";
    }
</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值