JQUERY+CSS implement tab Bar switching

Source: Internet
Author: User

A few days ago interview met to write a tab bar switch function, thought basically understand, but haven't written for a long time, to realize the effect is really a bit difficult. Back, then the idea of a reason, write a, the basis is still very important.

The final effect to be achieved is as follows:

(1) Click the tab bar to display the corresponding content, and the tab bar style changes. Implementation: The General tab bar if you want to make a more good-looking style, will cut two pictures as a background, a check for the background, a piece for the unselected background. Here for simplicity, just use CSS to set the style. Then for each tab-bound Click event, when the Click event is triggered, the display of the corresponding content div is set to block, otherwise set to none.

(2) When the mouse hovers over the tab bar that is not selected, change the style and return the style when it is moved away. If the tab bar is selected, the mouse hover does not affect the style. Implementation: For the tab bar to add hover event, when the mouse entered, to determine whether the tab bar is selected (You can set a class for the selected tab bar, just to determine whether the class can be), in the case of the hover add the style.

Complete code as follows (code download address):

Html:

<!
  DOCTYPE html>  

Css:

. tab-contain{
    margin:50px Auto;
    width:1000px;
    padding:100px;
    Background: #7F7D7D;
}
#tabs {
    Overflow:hidden;
    width:100%;
    margin:0;
    padding:0;
    List-style:none;
}
#tabs li {
    float:left;
    margin:0;
}
Li a {
    position:relative;
    Background: #ddd;
    padding:10px 50px;
    Float:left;
    Text-decoration:none;
    Color: #444;
    text-shadow:0 1px 0 Rgba (255, 255, 255,. 8);
    border-radius:20px 20px 0 0;
    box-shadow:0 2px 2px rgba (0, 0, 0,. 4);

 Current a{
    outline:0;
    Background: #fff;
     Z-index:4
}
. Hoveritem a{
    background: #AAC8B9;
}
#content {
    background: #fff;
    padding:50px;
    height:220px;
    position:relative;
    border-radius:0 5px 5px 5px;
    box-shadow:0 -2px 3px-2px rgba (0, 0, 0,. 5);
item{
    display:none
}

. show{
    display:block;
}

Js:

 $ (function () {$ (' #tabs a '). Click (function (e) {e.preventdefault ();
          $ (' #tabs Li '). Removeclass ("current"). Removeclass ("Hoveritem");
          $ (this). Parent (). addclass ("current");
          $ ("#content div"). Removeclass ("show");
      $ (' # ' + $ (this). attr (' title ')] AddClass (' Show ');

     }); $ (' #tabs a '). Hover (function () {if (!$ (). Parent (). Hasclass ("current")) {$ (this). Parent (). addclass ("Hov
        Eritem ");
     }},function () {$ (this). Parent (). Removeclass ("Hoveritem");
  }); });
Related Article

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.