练习11_放大镜.html 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .small-img{
  9. width: 600px;
  10. height: 600px;
  11. position: relative;
  12. float: left;
  13. }
  14. .small-img img{
  15. width: 600px;
  16. height: 600px;
  17. position: absolute;
  18. top: 0;
  19. left: 0;
  20. }
  21. .small-img .box{
  22. width: 300px;
  23. height: 300px;
  24. background-color: #fff;
  25. opacity: 0.5;
  26. position: absolute;
  27. top: 0;
  28. left:0;
  29. }
  30. .big-img{
  31. margin-left: 100px;
  32. float: left;
  33. width: 600px;
  34. height: 600px;
  35. overflow: hidden;
  36. position: relative;
  37. }
  38. .big-img img{
  39. width: 1200px;
  40. height: 1200px;
  41. position: absolute;
  42. top: 0;
  43. left: 0;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <div class="small-img">
  49. <img src="./image/niu.png" alt="img">
  50. <div class="box"></div>
  51. </div>
  52. <div class="big-img">
  53. <img class="big-pic" src="./image/niu.png" alt="img">
  54. </div>
  55. <script>
  56. var oSmallImg = document.getElementsByClassName("small-img")[0];
  57. var oBox = document.getElementsByClassName("box")[0];
  58. var oBigImg = document.getElementsByClassName("big-pic")[0];
  59. oSmallImg.onmousemove = function(e){
  60. var x = e.clientX - 150;
  61. var y = e.clientY - 150;
  62. // 控制半透明蒙版左侧最小值范围
  63. if(x<0){
  64. x = 0;
  65. }
  66. // 控制半透明蒙版顶部最小值范围
  67. if(y<0){
  68. y = 0;
  69. }
  70. // 控制半透明蒙版右侧最大值范围
  71. if(x>=300){
  72. x = 300
  73. }
  74. // 控制半透明蒙版顶部最大值范围
  75. if(y>=300){
  76. y = 300
  77. }
  78. oBox.style.left = x + "px";
  79. oBox.style.top = y + "px";
  80. oBigImg.style.left = -2*x + "px";
  81. oBigImg.style.top = -2*y + "px";
  82. }
  83. </script>
  84. </body>
  85. </html>