JavaScriptのanimate関数を利用して、cssのposition属性を利用して表示物体の移動先を指定する際に、「aboslute」指定し忘れると、物体の移動がうまくいかないことがある。その場合「margin」属性の設定で物体を移動させることが可能となる。
<html lang="en"> <head> <meta charset="utf-8"> <style> div { position: absolute; margin: 3px; width: 40px; height: 40px; left: 0px; top: 60px; background: green; display: none; } </style> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <div></div> <script> $( "div" ) .show( "slow" ) .animate({ left: "+=200" }, 2000 ) </script> </body> </html
<html lang="en"> <head> <meta charset="utf-8"> <style> div { // position: absolute; ⇐ この記述の削除 margin: 3px; width: 40px; height: 40px; left: 0px; top: 60px; background: green; display: none; } </style> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <div></div> <script> $( "div" ) .show( "slow" ) .animate({ left: "+=200" }, 2000 ) </script> </body> </html>
<html lang="en"> <head> <meta charset="utf-8"> <style> div { //position: absolute; margin: 3px; width: 40px; height: 40px; left: 0px; top: 60px; background: green; display: none; } </style> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <div></div> <script> $( "div" ) .show( "slow" ) .animate({ 'margin-left': "+=200" }, 2000 ) </script> </body> </htm>