js development plug-in to achieve tab effect

js development plug-in to achieve tab effect

This article example shares the specific code of the js plug-in to achieve the tab effect for your reference. The specific content is as follows

1. Build the page

<div class="tab" data-config='{ // Customize the configuration in the tag "triggerType": "click",
   "effect": "fade",
   "invoke": 2,
   "auto": 1000
     }'>
  <ul class="tab-nav">
   <li class="active"><a href="javascript:void(0);" >News</a></li>
   <li><a href="javascript:void(0);" >Entertainment</a></li>
   <li><a href="javascript:void(0);" >Movies</a></li>
   <li><a href="javascript:void(0);" >Technology</a></li>
  </ul>
  <div class="content">
   <div class="content-item current">
    <img src="./images/pic1.jpg">
   </div>
   <div class="content-item">
    <img src="./images/pic2.jpg">
   </div>
   <div class="content-item">
    <img src="./images/pic3.jpg">
   </div>
   <div class="content-item">
    <img src="./images/pic4.jpg">
   </div>
  </div>
</div>

2. Basic Style

* {
 margin: 0;
 padding: 0;
}
ul, li {
 list-style: none;
}
body {
 background: #323232;
 font-size: 12px;
 padding: 100px;
}
.tab {
 width: 300px;
}
.tab .tab-nav {
 height: 30px;
}
.tab .tab-nav li {
 display: inline-block;
 margin-right: 5px;
 background: #767676;
 border-radius: 3px 3px 0 0;
}
.tab .tab-nav li a {
 display: block;
 text-decoration: none;
 height: 30px;
 line-height: 30px;
 padding: 0 20px;
 color: #fff;
}
.tab .tab-nav li.active {
 background: #fff;
 color: #777;
}
.tab .tab-nav li.active a {
 color: #777;
}
.tab .content {
 background: #fff;
 height: 200px;
 padding: 5px;
}
.tab .content-item {
 position: absolute;
 display: none;
}
.tab .content img {
 width: 290px;
 height: 200px;
}
.tab .content .current {
 display: block;
}

3. Effect

4. Thinking structure diagram of tab development: objects, parameters, and methods are required

5. js practice

(function () {
 function Tab (tab)
 {
  this.tab = tab;
  var _this_ = this;
  // Default configuration parameters, attribute name is double quotes, otherwise parseJSON escape will fail this.config = {
   // Used to define the trigger type of the mouse, click or mouseover
   "triggerType": "mouseover",
   // Used to define the content switching effect, whether it is direct switching or fade-in and fade-out effect "effect": "default",
   // The default tab to display
   "invoke": 1,
   // Used to define whether the tab switches automatically and specify how long it takes to switch "auto": false
  }
  // If the configuration exists, extend the original configuration and merge with $.extend if (this.getConfig()) {
   $.extend(this.config, this.getConfig());
  }
  // Mouse trigger function var config = this.config; // Storage configuration, this.config will search every time this.liItem = this.tab.find('.tab-nav li'); // Get li
  this.contentItem = this.tab.find('.content div'); // Get content // Determine if it is click. . When operating, execute the invoke method to switch if (config.triggerType === 'click') {
   this.liItem.click(function () {
    _this_.invoke($(this));
   });
  } else {
   this.liItem.mouseover(function () {
    _this_.invoke($(this));
   });
  }

  // Automatic switching function if (this.config.auto) {
   this.timer = null;
   this.count = 0; // counter this.autoplay();
   // Stop when the mouse is above, and continue when it is moved away this.tab.hover(function () {
    clearInterval(_this_.timer); // this is this.tab
   }, function () {
    _this_.autoplay();
   })
  }

  // The default display number if (this.config.invoke > 1) {
   this.invoke(this.liItem.eq(this.config.invoke - 1)); // Direct switch}
 }

 Tab.prototype = {
  // Get configuration parameters getConfig: function () {
   //Take out the contents of data-config on the tab element var config = this.tab.attr('data-config');
   if (config && config != '') {
    return $.parseJSON(config); //Convert json object to js object} else {
    return null;
   }
  },
  // Get the passed li and switch invoke: function (li) {
   var index = li.index(); // Get the index of li var liItem = this.liItem;
   var contentItem = this.contentItem;
   
   li.addClass('active').siblings().removeClass('active'); // Add active to itself and remove other siblings // Fade in and out or default var effect = this.config.effect;
   if (effect === 'default') {
    contentItem.eq(index).addClass('current').siblings().removeClass('active');
   } else {
    contentItem.eq(index).fadeIn().siblings().fadeOut();
   }
   // When automatically switching, change count, otherwise start from the beginning each time this.count = index;
  },
  // Automatically switch autoplay: function () {
   var _this_ = this;
   var length = this.liItem.length; // Get length this.timer = setInterval(function() {  
    _this_.count ++; // count plus one, this is this.timer
    if (_this_.count >= length) {
     _this_.count = 0;
    }
    // Which li item triggers the event_this_.liItem.eq(_this_.count).trigger(_this_.config.triggerType);
   }, this.config.auto);
  }
 }

 window.Tab = Tab; // Register Tab as a window object, otherwise it cannot be accessed from outside})();

6. Call

// Pass the first tab to the Tab class var tab = new Tab($('.tab').eq(0));

7. Optimization: when there are multiple tabs, no need to create multiple new ones

1. The first method is through init

// Initialize through init Tab.init = function (tabs) {
  tabs.each(function () {
   new Tab($(this));
  });
 }

Call

Tab.init($('.tab'));

2. The second method is to register as a jQuery method

// Register as jQuery method $.fn.extend({
  tab: function () {
   this.each(function () { // Who called the tab method new Tab($(this)); // This this is the li that has been each
   });
   return this; // Chain operation}
 })

Call

$('.tab').tab();

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JavaScript realizes the tab switching effect (self-written native js)
  • Use vue.js to write a tab effect
  • Vue.js component tabs to achieve tab switching effect
  • Native js to achieve tab switching
  • js implements tab function code
  • Vue.js component tab implements tab switching
  • js simple method to realize vertical tab
  • JS implements sliding menu effect code (including Tab, tab, horizontal and other effects)
  • 4 JavaScript methods to achieve simple tab switching
  • js to achieve Tab tab switching effect

<<:  Troubleshooting of master-slave delay issues when upgrading MySQL 5.6 to 5.7

>>:  How to install and configure GitLab on Ubuntu 20.04

Recommend

A brief discussion on VUE uni-app template syntax

1.v-bind (abbreviation:) To use data variables de...

MySql quick insert tens of millions of large data examples

In the field of data analysis, database is our go...

Why developers must understand database locks in detail

1.Lock? 1.1 What is a lock? The real meaning of a...

The difference and use of json.stringify() and json.parse()

1. Differences between JSON.stringify() and JSON....

Summary of 50+ Utility Functions in JavaScript

JavaScript can do a lot of great things. This art...

Explanation of the configuration and use of MySQL storage engine InnoDB

MyISAM and InnoDB are the most common storage eng...

How to handle token expiration in WeChat Mini Programs

Table of contents Conclusion first question Solut...

How to use lodop print control in Vue to achieve browser compatible printing

Preface This control will have a watermark at the...

Introduction to the use of alt and title attributes of HTML img tags

When browser vendors bend the standards and take i...

Detailed example of jQuery's chain programming style

The implementation principle of chain programming...

Vue implements DingTalk's attendance calendar

This article shares the specific code of Vue to i...

Detailed description of ffmpeg Chinese parameters

FFMPEG 3.4.1 version parameter details Usage: ffm...

Install CentOS 7 on VMware14 Graphic Tutorial

Introduction to CentOS CentOS is an enterprise-cl...

Network management and network isolation implementation of Docker containers

1. Docker network management 1. Docker container ...