Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157445
  • 博文数量: 42
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 399
  • 用 户 组: 普通用户
  • 注册时间: 2015-09-23 11:47
个人简介

程序猿啊程序猿

文章分类

全部博文(42)

文章存档

2016年(28)

2015年(14)

我的朋友

分类: HTML5

2016-05-27 16:03:50

本文使用的是400*300分辨率的图片,重要部分的思路都在代码注释里,如果还有不懂得地方,请留言,必回!代码如下:

点击(此处)折叠或打开

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>CSS实现截图和放大镜</title>
  6.     <style>
  7.        *{
  8.            padding:0;
  9.            margin:0;
  10.        }
  11.        #view1{
  12.            width: 400px;
  13.            height: 300px;
  14.            position: absolute;
  15.            /*top:100px;
  16.            left:200px;*/
  17.        }
  18.        #view2{
  19.            width: 400px;
  20.            height: 300px;
  21.            position: absolute;
  22.            left:450px;
  23.            display: none;
  24.        }
  25.        #view2 img{
  26.            width: 100%;
  27.        }
  28.        #magnifier{
  29.            position: absolute;
  30.            width: 80px;
  31.            height: 60px;
  32.            border:1px solid red;
  33.            left: 160px;
  34.            top: 110px;
  35.            cursor: pointer;
  36.            display: none;
  37.        }
  38.     </style>
  39. </head>
  40. <body>
  41.     <p id="intend"></p>
  42.     <div id="view1">
  43.         <img src="images/2.jpg" alt="">
  44.         <div id="magnifier"></div>
  45.     </div>
  46.     
  47.     <div id="view2">
  48.         <img src="images/2.jpg" alt="">
  49.     </div>
  50.     
  51.     <script>
  52.        window.onload=function(){
  53.           var view=document.getElementById('view1');
  54.           var view_mag=document.getElementById('view2');
  55.           var inner=document.getElementById("intend");
  56.           var magnifier=document.getElementById("magnifier");
  57.           view.addEventListener('mouseover',function(){magnifier.style.display="block";},true);
  58.           view.addEventListener('mouseout',function(){magnifier.style.display="none";
  59.               view_mag.style.display="none";},true);//给view增加鼠标移出监听事件
  60.           view.onmousemove=function(){
  61.              var e=e||window.event;
  62.              var mouseX=e.clientX;//获得鼠标横坐标
  63.              var mouseY=e.clientY;//获得鼠标纵坐标
  64.              var magL=e.clientX-40;
  65.              var magT=e.clientY-30;
  66.              if(magL<0){magL=0}//不让之溢出
  67.              if(magL>320){magL=320}
  68.              if(magT<0){magT=0}
  69.              if(magT>240){magT=240}
  70.              magnifier.style.top=magT+"px";//鼠标滑动时放大镜的位置坐标
  71.              magnifier.style.left=magL+"px";
  72.              // inner.innerHTML=mouseX+" "+mouseY;
  73.              view_mag.style.display="block";
  74.              view_mag.style.clip="rect("+magT+"px"+","+(magL+80)+"px"+","+(magT+60)+"px"+","+magL+"px"+")";
  75.              //**clip:rect(top,width,height,left),clip为CSS截图属性,(top,left)为所截图的左上角坐标,(width-left,height-top)分别为所截图的宽和高**

  76.              //1:1放大镜
  77.              // view_mag.style.zoom=1;//正常比例
  78.              // view_mag.style.left=(450-magL)+"px";//放大后的图片在固定位置显示,magL为显示的区域的左上角横坐标
  79.              // view_mag.style.top=-magT+"px";//magT为显示区域的左上角纵坐标

  80.              // //1:2放大镜
  81.              // view_mag.style.zoom=2;
  82.              // view_mag.style.left=450/2-magL+"px";
  83.              // view_mag.style.top=-magT+"px";

  84.              // //1:3放大镜
  85.              // view_mag.style.zoom=3;
  86.              // view_mag.style.left=450/3-magL+"px";
  87.              // view_mag.style.top=-magT+"px";

  88.              // //1:4放大镜
  89.              // view_mag.style.zoom=4;
  90.              // view_mag.style.left=450/4-magL+"px";
  91.              // view_mag.style.top=-magT+"px";

  92.              //1:5放大镜
  93.              view_mag.style.zoom=5;
  94.              view_mag.style.left=450/5-magL+"px";
  95.              view_mag.style.top=-magT+"px";
  96.           }
  97.        }
  98.     </script>
  99. </body>
  100. </html>

阅读(1452) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~