JavaScript Bootstrap switchable label list and scroll listener

Source: Internet
Author: User

There is a navigation bar list and the corresponding content section. In addition to being able to locate the specified part, you also need to highlight the clicked navigation entries.

One switchable TAB/switchable tab list
This method is based on the switchable tab without writing any JavaScript code, simply specify the data-toggle = "tab" or data-toggle = "pill" attribute for the page element to activate the tab or capsule tab. Add the nav and nav-tabs classe for ul to add the Bootstrap tab style for it.

  1. <Ul class = "nav-tabs">
  2. <Li> <a href = "# home" data-toggle = "tab"> homepage </a> </li>
  3. <Li> <a href = "# profile" data-toggle = "tab"> Profile </a> </li>
  4. <Li> <a href = "# messages" data-toggle = "tab"> Messages </a> </li>
  5. <Li> <a href = "# settings" data-toggle = "tab"> Settings </a> </li>
  6. </Ul>
You can also use js Code to control:
  1. <Ul class = "nav-tabs" id = "myTab">
  2. <Li class = "active"> <a href = "# home">... </a> </li>
  3. <Li> <a href = "# profile"> Profile </a> </li>
  4. <Li> <a href = "# messages"> Messages </a> </li>
  5. <Li> <a href = "# settings"> Settings </a> </li>
  6. </Ul>

  7. <Div>
  8. <Div id = "home">... </div>
  9. <Div id = "profile">... </div>
  10. <Div id = "messages">... </div>
  11. <Div id = "settings">... </div>
  12. </Div>

  13. <Script>
  14. $ (Function (){
  15. $ ('# MyTab A'). click (function (e ){
  16. E. preventDefault ();
  17. $ (This). tab ('show ');
  18. })
  19. })
  20. </Script>
However, the same effect cannot be applied to the label list, because the corresponding parts of the list are displayed, but the positions of the scroll bars are different. So I made some modifications and added the code for locating the corresponding section.
  1. $ ('# Navi_tabs A'). click (function (e ){
  2. E. preventDefault ();
  3. $ (This). tab ('show ');
  4. Var id = $ (this). attr ("href ");
  5. $. ScrollTo (id, 100, {offset:-50 });
  6. })

Secondary rolling listener
Compared with the first method, rolling listening is much easier to implement. JS implementation is as follows:
  1. $ ('# Nav_tabs'). scrollspy ()
However, the implementation may be because the content is within the entire page range. The above method is invalid. You can only change the attributes of the body Tag:
  1. <Body data-spy = "scroll" data-target = ". navbar">... </body>

  2. Or

  3. $ ('Body'). attr ('data-spy', 'scroll ');
  4. $ ('Body'). attr ('data-target', "# navi_div ");

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.