I am trying to animate a div when clicking on the document using jQuery by increasing the x-position of that div.
I don't want the div to go out of my sight while it is animating; if the div went out of my screen I want to bring it back.
This is my code:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{
overflow: hidden;
}
div {
position: absolute;
left:10px;
top:10px;
width:20px;
height:20px;
background-color:green;
}
</style>
<script type="text/javascript">
$(function(){
$(document).click(function(){
if($("div").offset().left > (parseInt($(window).width()))){
$("div").animate({"top":'+=20'},1000);
$("div").animate({"left": '8' },1000);
}else{
$("div").animate({"left":'+=400'},1000);
}
});
});
</script>
</head>
<body>
<div></div>
</body>
</html>
</p>