zoukankan      html  css  js  c++  java
  • 前端学习笔记day14 放大镜

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            #box {
                width: 400px;
                height: 400px;
                margin-left: 60px;
                margin-top: 100px;
                position: relative;
    
            }
            #small {
                width: 100%;
                height: 100%;
                position: relative;
            }
            #small img {
                position: absolute;
            }
            #mask {
                width: 140px;
                height: 140px;
                position: absolute;
                background: rgba(230,230,45,0.58);
                display: none;
            }
            #big {
                position: absolute;
                left: 410px;
                top: 0px;
                width: 400px;
                height: 400px;
                display: none;
                overflow: hidden;
            }
            #big img {
                width: 800px;
                height: 800px;
                display: block;
                position: absolute;
            }
            }
        </style>
    </head>
    <body>
        <div id='box'>
            <div id='small'>
                <img src="images/small.webp">
                <div id='mask'></div>
            </div>
            <div id='big'>
                <img src="images/big.jpg" id='pic'>
            </div>
        </div>
    
        <script>
            var box = document.getElementById('box');
            var mask = document.getElementById('mask');
            var big = document.getElementById('big');
            var pic = document.getElementById('pic');
            box.onmouseover = function() {
                mask.style.display = 'block';
                big.style.display = 'block'
            }
            box.onmousemove = function(e) {
                var maskX = e.pageX - box.offsetLeft -70;
                var maskY = e.pageY - box.offsetTop -70;
                maskX = (maskX < 0) ? 0 : maskX;
                maskX = (maskX > box.offsetWidth - mask.offsetWidth) ? (box.offsetWidth - mask.offsetWidth) : maskX;
                maskY = (maskY < 0) ? 0 : maskY;
                maskY = (maskY > box.offsetWidth - mask.offsetWidth) ? (box.offsetWidth - mask.offsetWidth) : maskY;
                mask.style.left = maskX + 'px';
                mask.style.top = maskY + 'px';
    
                var maxMask = box.offsetWidth - mask.offsetWidth;
                var maxBig =  pic.offsetWidth - box.offsetWidth;
                var x = maskX / maxMask * maxBig;
                var y = maskY / maxMask * maxBig;
                pic.style.left = -x + 'px';
                pic.style.top = -y + 'px';
    
    
            }
            box.onmouseout = function() {
                mask.style.display = 'none';
                big.style.display = 'none'
            }
        </script>
    </body>
    </html>

    运行结果:

  • 相关阅读:
    grunt 使用比较
    一些技术要点
    git 使用笔记
    oo的一些概念
    借用构造函数继承非原型
    bower解决js的依赖管理
    需要了解的一些东西
    一些常用的代码
    js模式(一):单例模式
    写给自己的计划
  • 原文地址:https://www.cnblogs.com/xuanxuanlove/p/10208609.html
Copyright © 2011-2022 走看看