Code for menu effects based on jQuery's elastic sliding navigation

Source: Internet
Author: User

Yesterday, I wanted to build an enterprise website to implement an elastic sliding navigation menu, but I couldn't find a lot of JavaScript code. Later I wanted to use jquery to implement it. I will share the Code with you, for more information, see.

The HTML code is as follows:

The Code is as follows: Copy code

<DIV id = nav>
<DIV class = nav-menu>
<A class = current href = "#"> homepage </A>
<A href = "#"> about Camnpr </A>
<A href = "#"> case study </A>
<A href = "#"> Contact Information </A> </DIV>
<DIV class = nav-current> & nbsp; </DIV>

The CSS code is as follows:

The Code is as follows: Copy code


Body {font-size: 100%; font-family: "Microsoft YaHei", "Arial"; background: # fff ;}
# Nav {position: relative; width: 573px; _ width: 576px; margin: 100px auto 0 auto; border-bottom: 2px # ddd solid ;}
# Nav. nav-menu {height: 50px ;}
# Nav. nav-menu a {display: block; float: left; height: 50px; padding: 0 40px; line-height: 50px; color: #666; font-size: 16px; text-decoration: none ;}
# Nav. nav-current {position: absolute; bottom:-2px; height: 2px; overflow: hidden; background: #80b600 ;}


The JS Code is as follows:

Reference jQuery first, for example:

The Code is as follows: Copy code

<Script type = "text/javascript" src = "jquery. min. js"> </script>

$ (Function (){
(Function (){
Var $ navcur = $ (". nav-current ");
Var $ nav = $ ("# nav ");
Var current = ". current ";
Var itemW = $ nav. find (current). innerWidth (); // default width of the current position
Var defLeftW = $ nav. find (current). position (). left; // default Left value of the current position

// Add the default underline
$Navcur.css ({width: itemW, left: defLeftW });

// Hover event
$ Nav. find ("a"). hover (function (){
Var index = $ (this). index (); // you can call this operation to obtain the index value of the element that has slipped through.
Var leftW = $ (this). position (). left; // gets the Left value of the element that slides over.
Var currentW = $ nav. find ("a"). eq (index). innerWidth (); // gets the Width value of the element that slides over.
$ Navcur. stop (). animate ({
Left: leftW,
Width: currentW
},300 );

}, Function (){
$ Navcur. stop (). animate ({
Left: defLeftW,
Width: itemW
}, 300)
})
})();

});

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.