zoukankan      html  css  js  c++  java
  • js放大镜效果

    在这里插入图片描述

        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>放大镜</title>
            <style>
                *{
                    padding: 0;
                    margin: 0;
                }
                #small{
                    width: 300px;
                    height: 500px;
                    position: absolute;
                    left: 100px;
                    top: 50px;
                }
                #small img{
                    width: 100%;
                    height: 100%;
                }
                #small span{
                    display: block;
                    width: 150px;
                    height: 150px;
                    background-color: black;
                    opacity: .2;
                    position: absolute;
                    top: 0;
                    left: 0;
                    display: none;
                }
                #big{
                    width:300px;
                    height: 300px;
                    display: none;
                    position: absolute;
                    left: 500px;
                    top: 50px;
                    overflow: hidden;
                }
                #big img{
                    width: 600px;
                    height: 1000px;
                    position: absolute;
    
                }
            </style>
            <script>
                window.onload = function(){
                    var oSmall = document.getElementById("small");
                    var Simg = document.querySelector("#small img");
                    var oSpan =document.querySelector("#small span");
                    var oBig = document.getElementById("big");
                    var Bimg = document.querySelector("#big img");
                    oSmall.onmouseover = function(){
                        oSpan.style.display="block";
                        oBig.style.display="block";
                    }
                    oSmall.onmouseout = function(){
                        oSpan.style.display="none";
                        oBig.style.display="none";
                    }
                    oSmall.onmousemove = function(eve){
                        var e = eve ||window.event;
                        var l = e.clientX -oSmall.offsetLeft-75; 
                        var t = e.clientY -oSmall.offsetTop -75;
                        if(l<=0){
                            l=0;
                        }
                        if(l>150){
                            l=150;
                        }
                        if(t<=0){
                            t=0;
                        }
                        if(t>350){
                            t=350;
                        }
                        oSpan.style.left = l +"px";
                        oSpan.style.top =t +"px";
                        Bimg.style.left=-2*l+"px";
                        Bimg.style.top= -2*t +"px"
                    }
                }
            </script>
        </head>
        <body>
            <div id="small">
                <img src="./timg.jpeg" alt="">
                <span></span>
            </div>
            <div id="big">
                <img src="./timg.jpeg" alt="">
            </div>
        </body>
        </html>
    
    请用今天的努力,让明天没有遗憾。
  • 相关阅读:
    03_ if 练习 _ little2big
    uva 11275 3D Triangles
    uva 12296 Pieces and Discs
    uvalive 3218 Find the Border
    uvalive 2797 Monster Trap
    uvalive 4992 Jungle Outpost
    uva 2218 Triathlon
    uvalive 3890 Most Distant Point from the Sea
    uvalive 4728 Squares
    uva 10256 The Great Divide
  • 原文地址:https://www.cnblogs.com/cupid10/p/15617745.html
Copyright © 2011-2022 走看看