Maybe it can help
The code below moves a point within the specified boundaries. Please see: that by adjusting the width and height of the same point, you can have a snake like a creature. Look at fiddle
<html> <head> <style> #midDiv{ float:left; width: 100px; height: 100px; background:rgb(0,0,0); } #topDiv,#bottomDiv{ float:left; width: 110px; height:5px; background: red; position:relative; } #leftDiv, #rightDiv{ width:5px; float:left; height: 100px; background: blue; position:relative; } #bodyWrapper{ width: 120px; height: 120px; } #centerDiv{ float:left; } .animateObject{ z-index:2; background: white; } </style> <script src="http://code.jquery.com/jquery-1.10.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function(){ $("#topDiv").on("animcomplete", function(){ $(".animateObject").remove(); var div = document.createElement("div"); $(div).width(5).height(5); div.className = "animateObject"; $("#rightDiv").append(div); $(div).css({position: "absolute"}); $(div).animate({ top: 100 }, 2000, function(){ $("#rightDiv").trigger({type: "animcomplete", time: new Date() }); }); }); $("#rightDiv").on("animcomplete", function(){ $(".animateObject").remove(); var div = document.createElement("div"); $(div).width(5).height(5); div.className = "animateObject"; $("#bottomDiv").append(div); $(div).css({position: "absolute", right: 0}); $(div).animate({ right: 100 }, 2000, function(){ $("#bottomDiv").trigger({type: "animcomplete", time: new Date() }); }); }); $("#bottomDiv").on("animcomplete", function(){ $(".animateObject").remove(); var div = document.createElement("div"); $(div).width(5).height(5); div.className = "animateObject"; $("#leftDiv").append(div); $(div).css({position: "absolute", bottom: -5}); $(div).animate({ bottom: 100 }, 2000, function(){ $("#leftDiv").trigger({type: "animcomplete", time: new Date() }); }); }); $("#leftDiv").on("animcomplete", function(){ $(".animateObject").remove(); var div = document.createElement("div"); $(div).width(5).height(5); div.className = "animateObject"; $("#topDiv").append(div); $(div).css({position: "absolute", left: 0}); $(div).animate({ left: 105 }, 2000, function(){ $("#topDiv").trigger({type: "animcomplete", time: new Date() }); }); }); $("#topDiv").trigger({type: "animcomplete", time: new Date() }); }); </script> </head> <body> <div id="bodyWrapper"> <div id="topDiv"></div> <div id="centerDiv"> <div id="leftDiv"></div> <div id="midDiv"></div> <div id="rightDiv"></div> </div> <div id="bottomDiv"></div> </div> </body>
This moves the point within the specified boundaries. Note: that by adjusting the width and height of the same point, you may have a snake creature
Akshay khandelwal
source share