zoukankan      html  css  js  c++  java
  • js图片放大镜

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <style>
    #small {
         300px;
        height:300px;
        border: #000 1px solid;
        float:left;
        position: relative;
    }
    #small img {
         300px;
        height: 300px;
    }

    #small span {
        display: block;
         120px;
        height: 120px;
        background: red;
        opacity: .5;
        border: #333 1px solid;
        position: absolute;
        left: 0;
        top: 0;
        display: none;
    }

    #big {
         300px;
        height: 300px;
        border: #000 1px solid;
        float: left;
        margin-left: 20px;
        overflow: hidden;
        position: relative;
        display: none;
    }

    #big img {
        position: absolute;
    }
    </style>
    <script>
    window.onload = function(){
        var oSmall  = document.getElementById('small');
        var oMask = document.getElementById('mask');
        var oBig = document.getElementById('big');
        var oImg = document.getElementById('bigimg');
        
        oSmall.onmouseover = function(){
            oMask.style.display = 'block';
            oBig.style.display ='block';    
        }
        
        oSmall.onmouseout = function(){
            oMask.style.display = 'none';
            oBig.style.display = 'none';    
        }
        
        oSmall.onmousemove = function(ev){
            var oEvent = ev || event;
            var l = oEvent.clientX - oMask.offsetWidth/2;
            var t = oEvent.clientY - oMask.offsetHeight/2;
            
            if(l < 0){
                l = 0;    
            }else if( l > oSmall.offsetWidth - oMask.offsetWidth){
                l = oSmall.offsetWidth - oMask.offsetWidth;    
            }
            
            if(t < 0){
                t = 0;    
            }else if(t > oSmall.offsetHeight - oMask.offsetHeight){
                t = oSmall.offsetHeight - oMask.offsetHeight;    
            }
            oMask.style.left = l + 'px';
            oMask.style.top = t + 'px';
            
            oImg.style.left = l * (oBig.offsetWidth - oImg.offsetWidth) / (oSmall.offsetWidth - oMask.offsetWidth) + 'px';
            
            oImg.style.top = t * (oBig.offsetHeight - oImg.offsetHeight) / (oSmall.offsetHeight - oMask.offsetHeight) + 'px';
        }
    }
    </script>
    </head>

    <body>
    <div id="small">
        <img src="s.jpg" alt=""/>
        <span id="mask"></span>
    </div>
    <div id="big">
        <img src="b.jpg" alt="" id="bigimg"/>
    </div>
    </body>
    </html>

  • 相关阅读:
    Oracle 安装报错 [INS-06101] IP address of localhost could not be determined 解决方法输入日志标题
    Linux下安装oracle数据库提示DISPLAY not set. Please set the DISPLAY and try again。
    redhat 关机注销命令详解
    VirtualBox的四种网络连接方式
    修改RedHat的系统显示时间
    insufficient memory to configure kdump(没有足够的内存)解决方法(待验证、待解决)
    xen坑随笔 heartbeat dpkg垃圾数据库清除
    tomcat 监控脚本
    负载均衡随笔
    GIT命令介绍
  • 原文地址:https://www.cnblogs.com/guolimin/p/6099458.html
Copyright © 2011-2022 走看看