练习11_放大镜效果.html 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. body{
  9. margin: 0;
  10. }
  11. .small-img{
  12. width: 400px;
  13. height: 400px;
  14. position: relative;
  15. }
  16. .small-img img{
  17. width: 100%;
  18. height: 100%;
  19. position: absolute;
  20. top:0;
  21. left: 0;
  22. }
  23. .box{
  24. width: 200px;
  25. height: 200px;
  26. background-color: #fff;
  27. position: absolute;
  28. top:0;
  29. left: 0;
  30. opacity: 0.5;
  31. }
  32. .big-img,.small-img{
  33. float: left;
  34. margin-right: 20px;
  35. }
  36. .big-img{
  37. width: 400px;
  38. height: 400px;
  39. overflow: hidden;
  40. position: relative;
  41. }
  42. .big-img img{
  43. width: 800px;
  44. height: 800px;
  45. position: absolute;
  46. top:0;
  47. left: 0;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div class="container">
  53. <div class="small-img">
  54. <img src="./image/niu.png" alt="img">
  55. <div class="box"></div>
  56. </div>
  57. <div class="big-img">
  58. <img class="img-two" src="./image/niu.png" alt="img">
  59. </div>
  60. </div>
  61. <script>
  62. var smallImg = document.getElementsByClassName("small-img")[0];
  63. var oBox = document.getElementsByClassName("box")[0];
  64. var bigImg = document.getElementsByClassName("img-two")[0];
  65. smallImg.onmousemove = function(e){
  66. var _top = e.clientY-100;
  67. var _left = e.clientX-100;
  68. // 控制左侧边界
  69. if(_left <= 0){
  70. _left = 0
  71. }
  72. // 控制顶部边界
  73. if(_top<=0){
  74. _top = 0;
  75. }
  76. // 控制右侧边界
  77. if(_left>=200){
  78. _left = 200;
  79. }
  80. // 控制底部
  81. if(_top>=200){
  82. _top = 200;
  83. }
  84. oBox.style.top = _top + "px";
  85. oBox.style.left = _left + "px";
  86. bigImg.style.top = -2 * _top + "px";
  87. bigImg.style.left = -2 * _left + "px";
  88. }
  89. </script>
  90. </body>
  91. </html>