html静态网页:放大镜

简介: html静态网页:放大镜

功能分三个模块:

1-鼠标跟随

2-处理越界

3-方大

效果:

1.png文件架构:

1.png

学习交流群:970353786

第一部分代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
        <style type="text/css">
            #box{
               width: 400px;
               height: 400px;
               border: 1px solid black;
               margin: 100px auto;
               position: relative;
            }
            #glass{
                width: 50px;
                height: 50px;
                background-color: green;
                left: 100px;
                top: 50px;
                position: absolute;
            }
        </style>
  </head>
  <body>
        <div id="box">
            <div id="glass">
            </div>
        </div>
  </body>
    <script type="text/javascript">
        var box = document.querySelector('#box');
        var glass = document.querySelector('#glass');
        // box.onclick = function(e){
        box.onmousemove = function(e){
            var x = e.pageX - box.offsetLeft;
            var y = e.pageY - box.offsetTop;
            x -= parseInt(getComputedStyle(glass).width)/2
            y -= parseInt(getComputedStyle(glass).height)/2
            glass.style.left = x + "px";
            glass.style.top = y + "px";
        }
    </script>
</html>

第二部分代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
        <style type="text/css">
            #box{
               width: 400px;
               height: 400px;
               border: 1px solid black;
               margin: 100px auto;
               position: relative;
            }
            #glass{
                width: 50px;
                height: 50px;
                background-color: green;
                left: 100px;
                top: 50px;
                position: absolute;
                display: none;
            }
        </style>
  </head>
  <body>
        <div id="box">
            <div id="glass">
            </div>
        </div>
  </body>
    <script type="text/javascript">
        var box = document.querySelector('#box');
        var glass = document.querySelector('#glass');
        // box.onclick = function(e){
        box.onmousemove = function(e){
            var x = e.pageX - box.offsetLeft;
            var y = e.pageY - box.offsetTop;
            x -= parseInt(getComputedStyle(glass).width)/2
            y -= parseInt(getComputedStyle(glass).height)/2
            //越界处理
            //左越界处理
            if (x < 0) {
                x = 0;
            }
            //上越界处理
            if (y < 0) {
                y = 0;
            }
            //右越界处理
            var maxLeft = parseInt(getComputedStyle(this).width) - parseInt(getComputedStyle(glass).width)
            if (x > maxLeft ) {
                x = maxLeft;
            }
            //下越界处理
            var maxHeight = parseInt(getComputedStyle(this).height) - parseInt(getComputedStyle(glass).height)
            if (y > maxHeight ) {
                y = maxHeight;
            }
            console.log(x,y);
            glass.style.left = x + "px";
            glass.style.top = y + "px"; 
        }
        box.onmouseover = function(){
            glass.style.display = "block"
        }
        box.onmouseout = function(){
            glass.style.display = "none"
        }
    </script>
</html>

第三部分代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
        <style type="text/css">
            *{
                padding: 0;
                margin: 0;
            }
            .box{
                width: 800px;
                margin: 50px auto;
            }
            .left{
                width: 250px;
                height: 250px;
                border: 1px solid black;
                float: left;
                margin-right:30px ;
                position: relative;
            }
            .left img{
                width: 100%;
            }
            .right{
                width: 500px;
                height: 500px;
                border: 1px solid black;
                overflow: hidden;
                float: left;
                display: none;
                position: relative;
            }
            .right img{
                position: absolute;
            }
            .glass{
                /* 如何计算放大镜宽高?? */
                /* 
                 已知:小div宽高 = 小图宽高 = 250*250
                      大div宽高 = 500*500
                      大图宽高 = 800*800
                 计算公式:
                    放大镜宽 / 小div宽 = 大div宽 / 大图宽
                    ==>放大镜宽 = 大div宽 / 大图宽 * 小div宽
                                = 500/800*250
                                =1250/8 = 156.125
                 */
                width:157px;
                height:157px;
                background-color: blue;
                position: absolute;
                left: 0;
                top: 0;
                opacity: 0.3;
                display: none;
            }
        </style>
  </head>
  <body>
        <div class="box">
            <div class="left">
                <img src="1.jpeg" >
                <div class="glass"></div>
            </div>
            <div class="right">
                <img src="2.jpeg" >
            </div>
        </div>
  </body>
    <script type="text/javascript">
        var leftDiv = document.querySelector('.left');
        var glass = document.querySelector('.glass');
        var rightDiv = document.querySelector('.right');
        var bigImg = document.querySelector('.right img');
        leftDiv.onmousemove = function(e){
            var x = e.pageX - this.offsetLeft;
            var y = e.pageY - this.offsetTop;
            x -= parseInt(getComputedStyle(glass).width)/2
            y -= parseInt(getComputedStyle(glass).height)/2
            //越界处理
            //左越界处理
            if (x < 0) {
                x = 0;
            }
            //上越界处理
            if (y < 0) {
                y = 0;
            }
            //右越界处理
            var maxLeft = parseInt(getComputedStyle(this).width) - parseInt(getComputedStyle(glass).width)
            if (x > maxLeft ) {
                x = maxLeft;
            }
            //下越界处理
            var maxHeight = parseInt(getComputedStyle(this).height) - parseInt(getComputedStyle(glass).height)
            if (y > maxHeight ) {
                y = maxHeight;
            }
            console.log(x,y);
            glass.style.left = x + "px";
            glass.style.top = y + "px"; 
            //大图移动,需要计算移动的距离
            /* 如何计算大图移动的距离?? */
            /* 
             已知:小div宽高 = 小图宽高 = 250*250
                  大div宽高 = 500*500
                  大图宽高 = 800*800
                  放大镜宽高= 157*157
                  放大镜移动的x和y
             计算大图移动的距离left和top????
                计算公式:
                放大镜移动x / 大图移动的x = 小图宽 / 大图宽
                ===>大图移动的x = 大图宽 / 小图宽 * 放大镜移动x
              */
            var left = -x * 800/250;
            var top =  -y * 800/250; 
            bigImg.style.left = left + "px";
            bigImg.style.top = top + "px";
        }
        leftDiv.onmouseover = function(){
            glass.style.display = "block";
            rightDiv.style.display = "block";
        }
        leftDiv.onmouseout = function(){
            glass.style.display = "none";
            rightDiv.style.display = "none";
        }
    </script>
</html>
相关文章
|
1天前
|
前端开发
仿新浪sina轻个人微博html静态网页模板
一款最新的仿新浪sina个人微博html静态网页模板(轻博客/轻微博/贴吧主页、qq社交空间主题),模板清新简洁、新颖,包含关注、粉丝、人气、个人资料、文章、视频等。
31 0
静态网页html+css的真ikun网站
静态网页html+css的真ikun网站
|
8月前
|
前端开发 编译器 Linux
JavaWeb第二章:HTML和CSS的知识制作静态网页
JavaWeb第二章:HTML和CSS的知识制作静态网页
68 0
html静态网页实例一(附完整代码)
html静态网页实例一(附完整代码)
2482 1
html静态网页实例一(附完整代码)
html静态网页:自动出题评分系统
html静态网页:自动出题评分系统
240 0
html静态网页:自动出题评分系统
|
前端开发 内存技术
html:修改版火影忍者静态网页设计,实例三(附带完整源码)
html:修改版火影忍者静态网页设计,实例三(附带完整源码)
838 0
html:修改版火影忍者静态网页设计,实例三(附带完整源码)
html静态网页实例二(附完整代码)
html静态网页实例二(附完整代码)
431 0
html静态网页实例二(附完整代码)
|
1天前
|
移动开发 HTML5
HTML5/CSS3粒子效果进度条代码
HTML5/CSS3进度条应用。这款进度条插件在播放进度过程中出现粒子效果,就像一些小颗粒从进度条上散落下来
17 0
HTML5/CSS3粒子效果进度条代码
|
1天前
|
移动开发 前端开发 JavaScript
:掌握移动端开发:HTML5 与 CSS3 的高效实践
:掌握移动端开发:HTML5 与 CSS3 的高效实践 “【5月更文挑战第6天】”
26 1