练习11_放大镜.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. background-image: url("./img/niu.png");
  15. background-size: 400px 400px;
  16. position: relative;
  17. float: left;
  18. }
  19. .bg{
  20. width: 200px;
  21. height: 200px;
  22. background-color: rgba(255,255,255,0.6);
  23. position: absolute;
  24. }
  25. .big-img{
  26. width: 400px;
  27. height: 400px;
  28. background-image: url("./img/niu.png");
  29. float: left;
  30. margin-left: 30px;
  31. background-size: 800px 800px;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <div class="small-img">
  37. <div class="bg"></div>
  38. </div>
  39. <div class="big-img">
  40. </div>
  41. <script>
  42. var oBg = document.getElementsByClassName("bg")[0];
  43. var oSmallImg = document.getElementsByClassName("small-img")[0];
  44. var oBigImg = document.getElementsByClassName("big-img")[0];
  45. oSmallImg.onmousemove = function(event){
  46. var x = event.clientX - 100;
  47. var y = event.clientY - 100;
  48. if(y<=0){
  49. y = 0;
  50. }
  51. if(x<=0){
  52. x = 0;
  53. }
  54. if(y>=200){
  55. y = 200;
  56. }
  57. if(x >= 200){
  58. x = 200;
  59. }
  60. oBg.style.top = y + "px";
  61. oBg.style.left = x + "px";
  62. oBigImg.style.backgroundPosition = (-2*x)+"px " + (-2*y) +"px";
  63. }
  64. </script>
  65. </body>
  66. </html>