Moving Div by button click using animate effect


 

Moving Div by button click using animate effect

In this tutorial , we will discuss how to move 'div' left-right by button click using jQuery.

In this tutorial , we will discuss how to move 'div' left-right by button click using jQuery.

Moving Div by button click using animate effect

In this tutorial , we will discuss how to move 'div' left-right by button click using jQuery. In this example , 'div' is moving left / right by left / right button click.

animateBox.html

<!DOCTYPE html>
<html>
<head>
<style>
div {
position:absolute;
background-color:#abc;
left:50px;
width:90px;
height:90px;
margin:5px;
}
</style>
<script src="jquery-1.4.2.js"></script>
</head>
<body>
<button id="left">&laquo;</button> <button id="right">&raquo;</button>
<div class="block"></div>

<script>
$("#right").click(function(){
$(".block").animate({"left": "+=50px"}, "slow");
});

$("#left").click(function(){
$(".block").animate({"left": "-=50px"}, "slow");
});

</script>
</body>
</html>

OUTPUT

Before click :

After Clicking "left" button  :

Download Source Code

 

Click here to see demo

Ads