<!doctype html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Elastic Motion---elastic menu </title>
<style>
* {padding:0; margin:0;}
Li {list-style:none;}
UL {width:400px; height:30px; position:relative; margin:100px auto 0;}
Li {float:left; width:98px; height:28px; line-height:28px; border:1px solid #ccc; text-align:center; z-index:2; po sition:relative; Cursor:pointer; }
. bg {width:100px; height:5px; overflow:hidden; background:red; border:none; position:absolute; top:24px; left:0; Z-index:1; }
</style>
<script type= "Text/javascript" >
Window.onload=function ()
{
var Oul=document.getelementbyid (' Ul1 ');
var ali=oul.getelementsbytagname (' Li ');
var obg=ali[ali.length-1];
var i=0;
for (i=0;i<ali.length-1;i++)
{
Ali[i].index=i;
Ali[i].onmouseover=function ()
{//Moved element The current object moves the property
Startmove (Obg,this, ' left ');
};
}
};
Move an element The current object moves the property
function Startmove (obj, index, attr)
{
Itarget=get_offset_val (index, attr);
Obj.attr_name=get_offset_val (obj, attr);
if (itarget==obj.attr_name)
Return //If the selection is the current selected element
Obj.ispeed=0;
var ispeed_dis=5;//speed ratio
var mc = 0.7; ///friction size value the larger the element stops moving the longer the value the smaller the element stops moving the faster the time
Prevents multiple timers from turning on
Clearinterval (Obj.timer);
Obj.timer=setinterval (backout, 30);
Elastic motion
function Backout ()
{
//Plus/deceleration motion itarget>obj.offsetleft accelerating motion itarget<obj.offsetleft deceleration movement
obj.ispeed+= (itarget-obj.attr_name)/ispeed_dis;
Friction motion allows elements to stop moving
OBJ.ISPEED*=MC;
Obj.attr_name+=obj.ispeed; //Prevent decimal error ispeed is not an integer, keep the decimal
//Movement speed is 1 and the target point and element distance is less than 1 stop motion
if (Math.Abs (obj.ispeed) <=1 && math.abs (obj.attr_name-itarget) <=1)
{
obj.style[attr]=itarget+ ' px ';
Clearinterval (Obj.timer);
}
Else
{
obj.style[attr]= obj.attr_name+ ' px ';
}
} //------------------backout () End
}
Get offset value
function Get_offset_val (obj,attr)
{
Switch (attr)
{
Case ' left ':
return obj.offsetleft;
Break
Case ' top ':
return obj.offsettop;
Break
Case ' width ':
return obj.offsetwidth;
Break
Case ' height ':
return obj.offsetheight;
Break
}
}
</script>
<body>
<ul id= "UL1" >
<li> Home </li>
<li> About Us </li>
<li> Products </li>
<li> Contact Information </li>
<li class= "BG" ></li>
</ul>
</body>
JS Elastic Motion---elastic menu