I have a dynamic box placed inside another box, and I want to restrict its movement within the boundaries of the parent container. How can I achieve this?
In simpler terms: I need the frog to stay within the frogger.
HTML
<div id="frogger"> <!-- Animate start -->
<button id="left">«</button> <button id="right">»</button>
<div id="frog">
</div>
</div> <!-- Animate end -->
CSS
#frogger
{
width: 500px;
height: 500px;
border: solid;
margin: 0 auto;
}
#frog
{
position:relative;
background-color:#abc;
left:50px;
width:90px;
height:90px;
margin:5px;
}
Javascript
$("#right").click(function(){
if ($(':animated').length) {
return false;
} else {
$("#frog").animate({"left": "+=50px"}, {queue: false}, "slow");
}
});
$("#left").click(function(){
if ($(':animated').length) {
return false;
} else {
$("#frog").animate({"left": "-=50px"}, {queue: false}, "slow");
}
});