JS Elastic Motion---elastic menu

Source: Internet
Author: User

<!doctype html>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

<title> Elastic Motion---elastic menu </title>


* {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; }


<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].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


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.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);




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;


Case ' top ':

return obj.offsettop;


Case ' width ':

return obj.offsetwidth;


Case ' height ':

return obj.offsetheight;






<ul id= "UL1" >

<li> Home </li>

<li> About Us </li>

<li> Products </li>

<li> Contact Information </li>

<li class= "BG" ></li>



JS Elastic Motion---elastic menu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.