1-1 动画
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script> <style> .parent { width: 500px; position: relative; border: 1px solid #333; height: 500px; } .center { position: absolute; border: 1px solid #ff2d51; width: 100px; height: 100px; top: 50%; left: 50%; transform: translate(-50%, -50%); } .child { width: 50px; height: 50px; position: absolute; } .one { left: 10px; top: 10px; background-color: red; } .two { top: 10px; right: 10px; background-color: yellow; } .three { left: 10px; bottom: 10px; background-color: green; } .four { bottom: 10px; right: 10px; background-color: blue; } </style></head><body> <div class="parent"> <div class="center"> </div> <div class="content"> <div class="child three"> </div> <div class="child four"> </div> <div class="child one"> </div> <div class="child two"> </div> </div> </div> <script> let prevIndex = null; let prevLeft = null; let prevTop = null; $(".content .child").mouseover(function () { console.log(prevIndex) if (prevIndex != null) { $(".content .child").eq(prevIndex).animate({ top: prevTop, left: prevLeft }) } let index = $(this).index(); prevIndex = index; prevLeft = $(this).position().left; prevTop = $(this).position().top; $(this).animate({ top: "50%", left: "50%" }) }) </script></body></html>