代码部分

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>鼠标跟随</title>
  6. <!-- IMPORT CSS -->
  7. <link rel="stylesheet" href="css/reset.min.css">
  8. <style>
  9. .product {
  10. box-sizing: border-box;
  11. margin: 20px auto;
  12. width: 460px;
  13. display: flex;
  14. }
  15. .product li {
  16. box-sizing: border-box;
  17. margin-right: 20px;
  18. width: 100px;
  19. height: 76px;
  20. border: 1px solid lightcoral;
  21. }
  22. .product li:nth-last-child(1) {
  23. margin-right: 0;
  24. }
  25. .product li img {
  26. width: 100%;
  27. height: 100%;
  28. }
  29. .detail {
  30. /* 相对于 BODY 定位 */
  31. position: absolute;
  32. top: 0;
  33. left: 0;
  34. box-sizing: border-box;
  35. width: 400px;
  36. height: 300px;
  37. border: 1px solid #DDD;
  38. }
  39. .detail img {
  40. width: 100%;
  41. height: 100%;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <ul class="product">
  47. <!-- li*4>img[src='images/apple_$.jpg'][big-img='images/apple_$_bigger.jpg'] -->
  48. <li>
  49. <img src="images/apple_1.jpg" alt="" big-img="images/apple_1_bigger.jpg">
  50. </li>
  51. <li>
  52. <img src="images/apple_2.jpg" alt="" big-img="images/apple_2_bigger.jpg">
  53. </li>
  54. <li>
  55. <img src="images/apple_3.jpg" alt="" big-img="images/apple_3_bigger.jpg">
  56. </li>
  57. <li>
  58. <img src="images/apple_4.jpg" alt="" big-img="images/apple_4_bigger.jpg">
  59. </li>
  60. </ul>
  61. <!-- <div class="detail">
  62. <img src="images/apple_1_bigger.jpg" alt="">
  63. </div> -->
  64. <!-- IMPORT JS -->
  65. <script src="js/jquery.min.js"></script>
  66. <script>
  67. let $product = $('.product'),
  68. $productList = $product.children('li'),
  69. $body = $('body'),
  70. $detail = null;
  71. function computed(ev) {
  72. $detail.css({
  73. top: ev.pageY + 15,
  74. left: ev.pageX + 15
  75. });
  76. }
  77. $productList.mouseover(function (ev) {
  78. // this:当前操作的LI
  79. let $this = $(this),
  80. bigImg = $this.children('img').attr('big-img');
  81. if (!$detail) {
  82. $body.append(`<div class="detail">
  83. <img src="${bigImg}" alt="">
  84. </div>`);
  85. $detail = $('.detail');
  86. }
  87. computed(ev);
  88. }).mouseout(function (ev) {
  89. if ($detail) {
  90. $detail.remove();
  91. $detail = null;
  92. }
  93. }).mousemove(computed);
  94. </script>
  95. </body>
  96. </html>

图片资源

apple_1.jpgapple_1_bigger.jpgapple_2.jpgapple_2_bigger.jpgapple_3.jpgapple_3_bigger.jpgapple_4.jpgapple_4_bigger.jpg