zoukankan      html  css  js  c++  java
  • 前端放大镜特效

    pc端购物网站的商品有些具有放大镜特效,自己简单实现了一下

    html部分

    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="index.css">
    </head>

    <body>
        <div id="wrap">
            <div class="small_box">
                <img src="images/c2.jpg" alt="">
                <span id="mask"></span>
            </div>
            <div class="big_box">
                <img id="bigImg" src="images/c1.jpg" alt="">
            </div>
        </div>
        <div class="list">
            <img src="images/c2.jpg" alt="">
            <img src="images/d2.jpg" alt="">
            <img src="images/g2.jpg" alt="">
        </div>
        <script src="index.js"></script>
    </body>

    </html>
     
     
    css部分·
     
    * {
        padding0;
        margin0;
        bordernone;
    }

    img {
        vertical-aligntop;
    }

    #wrap {
        width350px;
        height350px;
        background#e71208;
        margin50px 0 0 50px;
        positionrelative;
    }

    .small_box {
        width100%;
        height100%;
        border1px solid #ccc;
        positionrelative;
    }

    #mask {
        width100px;
        height100px;
        backgroundrgba(2552550.4);
        positionabsolute;
        left0;
        top0;
        cursormove;
        displaynone;
    }

    .big_box {
        width500px;
        height500px;
        border1px solid #ccc;
        positionabsolute;
        top0;
        left360px;
        overflowhidden;
        displaynone;
    }

    .big_box img {
        width100%;
        height100%;
        positionabsolute;
        left0;
        top0;
    }

    .list {
        width450px;
        margin-top40px;
        text-aligncenter;
    }

    .list img {
        width100px;
        height100px;
        border1px solid rebeccapurple;
        cursorpointer;
        margin5px;
    }
    js部分
     
    window.onload = function() {
        let wrap = document.getElementById("wrap")
        let bigBox = document.getElementsByClassName("big_box")[0]
        let smallBox = document.getElementsByClassName("small_box")[0]
        let mask = document.getElementById("mask")
        let bigImg = document.getElementById("bigImg")
        let list = document.getElementsByClassName("list").children

        //监听鼠标进入小盒子
        smallBox.onmouseover = function() {
            mask.style.display = "block"
            bigBox.style.display = "block"
        }

        //监听鼠标移动
        smallBox.onmousemove = function() {
            let e = window.event || arguments[0]
                //求出鼠标的坐标
            let pointX = e.clientX - smallBox.offsetParent.offsetLeft
            let pointY = e.clientY - smallBox.offsetParent.offsetTop
                //让放大镜移动
                //让鼠标在mask中心
            pointX = pointX - mask.offsetWidth * 0.5
            pointY = pointY - mask.offsetHeight * 0.5
                //边界检测
            if (pointX < 0) {
                pointX = 0
            } else if (pointX >= smallBox.offsetWidth - mask.offsetWidth) {
                pointX = smallBox.offsetWidth - mask.offsetWidth
            }
            if (pointY < 0) {
                pointY = 0
            } else if (pointY >= smallBox.offsetHeight - mask.offsetHeight) {
                pointY = smallBox.offsetHeight - mask.offsetHeight
            }

            mask.style.left = pointX + "px"
            mask.style.top = pointY + "px"

            //大图移动
            bigImg.style.left = -pointX / (smallBox.offsetWidth / bigBox.offsetWidth) + "px"
            bigImg.style.top = -pointY / (smallBox.offsetHeight / bigBox.offsetHeight) + "px"
        }

        //监听鼠标离开小盒子
        smallBox.onmouseout = function() {
            mask.style.display = "none"
            bigBox.style.display = "none"
        }

    }
     放大镜大图移动公式
    smallX/bigX(坐标)=smallbox.width/大图宽度
    bigX=smallX/(smallbox.width/大图宽度)

  • 相关阅读:
    JAVA中添加jar包
    shell 脚本读取数据库示例
    Div 布局之 DIV垂直居中显示
    awk 学习笔记
    提示ufmyhr日志已满,无法继续操作软件,如何解决
    12种貌似卫生的不卫生习惯
    远程通客户端反复提示要下载客户端软件
    固定资产反启用后再启用报00:00:00错误
    2008年5月14日
    睡前六个必要动作,一觉睡到大天亮
  • 原文地址:https://www.cnblogs.com/wywd/p/13149596.html
Copyright © 2011-2022 走看看