Easily implement the jquery tab toggle effect _jquery

Source: Internet
Author: User

Early want to learn jquery plug-in writing, has been dragged to now, while no work busy, search some data learning, wrote a relatively simple tab effect.

At first there was a very popular example: theAlert dialog box .

Jquery.alertMsg.js

/**
 * [description]
 * @param {[type]} $ [description]
 * @return {[type]} [description] * *
( Function ($) {

 $.fn.alertmsg = function (options) {

 var defaults = {
  mouseevent: ' Clcik ',
  msg: ' Hello World '
 var options = $.extend (defaults, options);
 var $this = $ (this);

 $this. On (options.mouseevent, function (e) {
  alert (options.msg);
 }}

) (JQuery)

Call Mode:

<span id= "Test" >test</span>

$ (function () {
 $ (' #test '). Alertmsg ({
  mouseevent: "Click", 
  msg: "First write plugin!" 
 });

jquery Plug-in Structure

(function ($) {
 //Tabs custom plug-in name
 $.fn.tabs = function (options) {

  //set default parameter
  var defaults = {
   Activeclass: ' Active '
   ... 
  }
  Object extension
  var options = $.extend (defaults, options);

  Return $ (a). each (function () {
   //write corresponding implementation code}}

) (jQuery)

tab Implementation:

1, HTML structure

<div id= "tab" >
 <ul>
  <li> options 1</li>
  <li> option 2</li>
  <li > Options 3</li>
  <li> options 4</li>
 </ul>
 <div id= "Tabcon" class= "Tab-con" > Content
  of <div>1 </div>
  <div>2 </div>
  <div>3 content </div>
  < Div>4 content </div>
 </div>
</div>


2, Jquery.tabs.js

(function ($) {

 $.fn.tabs = function (options) {

   var defaults = {
    Event: ' Click ',
    activeclass: ' Active '
   }
   var options = $.extend (defaults, options);

   Return $ (a). each (function () {

    var $thisTab = $ (this). Find (' ul ');
    var $tabCon = $thisTab. Siblings (' div ');

    $tabCon. Find (' div '). each (the function () {
     $ (this). Hide ();
    });

    $thisTab. Find (' Li:first '). addclass (Options.activeclass);
    $tabCon. Find (' Div:first '). Show ();

    $thisTab. Find (' Li '). each (the function (index) {

     $ (this). On (options. Event, function () {

      $ (this). Siblings (). Removeclass (Options.activeclass);
      $ (this). addclass (Options.activeclass);
      $tabCon. Find (' div '), eq (index), show (). siblings (). Hide ();

     });
 
(JQuery)


3. Call

 $ (' #tab '). Tabs ({
  activeclass: ' Active '
 });

Summary: the first knowledge of jquery plug-ins, the feeling should continue to optimize and expand, continue to learn!

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Wonderful topic Sharing:JavaScript Tab Action method Summary jquery Tab action Method Rollup

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.