1-1 动画

  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. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  8. <style>
  9. .parent {
  10. width: 500px;
  11. position: relative;
  12. border: 1px solid #333;
  13. height: 500px;
  14. }
  15. .center {
  16. position: absolute;
  17. border: 1px solid #ff2d51;
  18. width: 100px;
  19. height: 100px;
  20. top: 50%;
  21. left: 50%;
  22. transform: translate(-50%, -50%);
  23. }
  24. .child {
  25. width: 50px;
  26. height: 50px;
  27. position: absolute;
  28. }
  29. .one {
  30. left: 10px;
  31. top: 10px;
  32. background-color: red;
  33. }
  34. .two {
  35. top: 10px;
  36. right: 10px;
  37. background-color: yellow;
  38. }
  39. .three {
  40. left: 10px;
  41. bottom: 10px;
  42. background-color: green;
  43. }
  44. .four {
  45. bottom: 10px;
  46. right: 10px;
  47. background-color: blue;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div class="parent">
  53. <div class="center">
  54. </div>
  55. <div class="content">
  56. <div class="child three">
  57. </div>
  58. <div class="child four">
  59. </div>
  60. <div class="child one">
  61. </div>
  62. <div class="child two">
  63. </div>
  64. </div>
  65. </div>
  66. <script>
  67. let prevIndex = null;
  68. let prevLeft = null;
  69. let prevTop = null;
  70. $(".content .child").mouseover(function () {
  71. console.log(prevIndex)
  72. if (prevIndex != null) {
  73. $(".content .child").eq(prevIndex).animate({
  74. top: prevTop,
  75. left: prevLeft
  76. })
  77. }
  78. let index = $(this).index();
  79. prevIndex = index;
  80. prevLeft = $(this).position().left;
  81. prevTop = $(this).position().top;
  82. $(this).animate({
  83. top: "50%",
  84. left: "50%"
  85. })
  86. })
  87. </script>
  88. </body>
  89. </html>