JavaScript plugin encapsulation for table switching

JavaScript plugin encapsulation for table switching

This article shares the encapsulation code of JavaScript to implement the table switching plug-in for your reference. The specific content is as follows

Effect picture:

HTML part:

<div class="box">
        <div id="tabBox">
            <ul>
                <li class="liStyle">A</li>
                <li>B</li>
                <li>C</li>
            </ul>
            <ol>
                <li class="liStyle">A</li>
                <li>B</li>
                <li>C</li>
            </ol>
        </div>
</div>

CSS part:

#tabBox {
            width: 600px;
            height: 450px;
            border: 3px solid #333;
        }
 
        #tabBox>ul {
            width: 100%;
            height: 50px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
 
        #tabBox>ul>li {
            flex: 1;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #fff;
            background-color: skyblue;
            font-size: 30px;
        }
 
        #tabBox>ul .liStyle {
            background-color: lime;
        }
 
 
        ol {
            flex: 1;
            width: 600px;
            height: 400px;
        }
 
        ol>li {
            width: 100%;
            height: 100%;
            background-color: #ccc;
            color: #fff;
            font-size: 100px;
 
            display: none;
            justify-content: center;
            align-items: center;
        }
 
        ol>li.liStyle {
            display: flex;
        }

JavaScript constructor part:

function fn15() {
            function TabBox(tabbox) {
                this.tabbox = tabbox;
 
                this.tabs = tabbox.querySelectorAll("ul>li")
                this.contents = tabbox.querySelectorAll("ol>li")
            }
 
            TabBox.prototype.startSwitch = function () {
                const tb = this
 
                tb.tabs.forEach(
                    function (tab, index) {
                        tab.onclick = function (e) {
                            tb.tabs.forEach(
                                function (tab) {
                                    tab.classList.remove("liStyle")
                                }
                            )
 
                            tb.contents.forEach(
                                function (con) {
                                    con.classList.remove("liStyle")
                                }
                            )
 
                            tab.classList.add("liStyle")
 
                            tb.contents[index].classList.add("liStyle")
                        }
                    }
                )
            }
            const tabBox = document.querySelector("#tabBox")
 
            const tb = new TabBox(tabBox)
 
            tb.startSwitch()
        }
        // fn15()
 
        // Use class to implement function fn16() {
            class TabBox {
                constructor(tabbox) {
                    this.tabbox = tabbox;
                    this.tabs = tabbox.querySelectorAll("ul>li")
                    this.contents = tabbox.querySelectorAll("ol>li")
                }
                startSwitch() {
                    const tb = this
 
                    tb.tabs.forEach(
                        function (tab, index) {
                            tab.onclick = function (e) {
                                tb.tabs.forEach(
                                    function (tab) {
                                        tab.classList.remove("liStyle")
                                    }
                                )
 
                                tb.contents.forEach(
                                    function (con) {
                                        con.classList.remove("liStyle")
                                    }
                                )
 
                                tab.classList.add("liStyle")
 
                                tb.contents[index].classList.add("liStyle")
                            }
                        }
                    )
                }
            }
 
 
            const tabBox = document.querySelector("#tabBox")
 
            const tb = new TabBox(tabBox)
 
            tb.startSwitch()
        }
fn16()

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:
  • JS implementation of style switching function tableCSS example
  • How to use javascript to implement table color separation and selection highlighting (and dynamically switch data)
  • JavaScript uses the arrow keys to control the cursor to switch between table cells
  • Four ways to implement tab switching in javascript
  • A streamlined JS DIV layer tab switching code
  • Native js to achieve tab switching
  • JS+CSS to achieve sliding switching tab menu effect
  • js implements click to switch TAB tag instance
  • A complete example of tab page switching effect implemented by JS
  • Vue.js component tab implements tab switching

<<:  Simple Implementation of HTML to Create Personal Resume

>>:  Detailed explanation of Navicat's slow remote connection to MySQL

Recommend

Linux automatically deletes logs and example commands from n days ago

1. Delete file command: find the corresponding di...

CSS complete parallax scrolling effect

1. What is Parallax scrolling refers to the movem...

More Ways to Use Angle Brackets in Bash

Preface In this article, we will continue to expl...

Introduction to MySQL overall architecture

The overall architecture of MySQL is divided into...

Several methods of deploying multiple front-end projects with nginx

I have summarized 3 methods to deploy multiple fr...

Analyzing ab performance test results under Apache

I have always used Loadrunner to do performance t...

Use HTML and CSS to create your own warm man "Dabai"

The final result is like this, isn’t it cute… PS:...

Making a simple game engine with React Native

Table of contents Introduction Get started A brie...

How to add fields and comments to a table in sql

1. Add fields: alter table table name ADD field n...

Tutorial on installing Ceph distributed storage with yum under Centos7

Table of contents Preface Configure yum source, e...

Summary of Problems in Installation and Usage of MySQL 5.7.19 Winx64 ZIP Archive

Today I learned to install MySQL, and some proble...

Implementing license plate input function in WeChat applet

Table of contents Preface background Big guess Fi...

MySQL data archiving tool mysql_archiver detailed explanation

Table of contents I. Overview 2. pt-archiver main...

Detailed explanation of Vue's list rendering

Table of contents 1. v-for: traverse array conten...