原生js实现一个tab栏的标签操作
生活随笔
收集整理的這篇文章主要介紹了
原生js实现一个tab栏的标签操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我是歌謠 閑來無事做 就不如寫寫代碼
本次不過多對原生的操作進行說法
我們直接上代碼
效果圖
有一個index.html的文件
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>#my-tab {width: 500px;height: 500px;border: 1px solid #000;margin: 50px auto;}.tab-wrapper {height: 50px;border-bottom: 1px solid #000;}.tab-item {float: left;width: 33.33%;height: 50px;text-align: center;line-height: 50px;}.tab-item.current {background-color: #000;color: #fff;}.page-wrapper {position: relative;height: 450px;}.page-item {display: none;position: absolute;top: 0;left: 0;width: 100%;height: 450px;text-align: center;line-height: 450px;}.page-item.current {display: block;} </style> </head> <body><div id="my-tab" data='[{"tab": "選項1","page": "頁面1"},{"tab": "選項2","page": "頁面2"},{"tab": "選項3","page": "頁面3"}]'><!-- <div>...tab按鈕</div><div>...頁面</div> --></div><script src="js/utils.js"></script><script src="js/tpl.js"></script><script src="js/myTab.js"></script><script>new MyTab('#my-tab'); </script> </body> </html>index.js這個類
(function test (a, b, c, d) {// 1. 可以創建一個與外界沒有任何關聯的作用域,獨立作用域// 2. 執行完成以后,自動銷毀// 3. ES3 ES5 立場上是沒有模塊概念,立即執行函數來模擬模塊化// 封閉作用域、拋出接口// 向外部拋出一系列屬性和方法// window上保存屬性和方法console.log(test);console.log(test.length);console.log(arguments.length);console.log('Hello'); })(1,2,3);MyTab.js
(function (doc, tpl, tools) {function MyTab (el) {this.el = doc.querySelector(el);this.data = JSON.parse(this.el.getAttribute('data'));this._index = 0;this.init();}MyTab.prototype.init = function () {this._render();this._bindEvent();}MyTab.prototype._render = function () {var tabWrapper = doc.createElement('div');var pageWrapper = doc.createElement('div');var oFrag = doc.createDocumentFragment();tabWrapper.className = 'tab-wrapper';pageWrapper.className = 'page-wrapper';this.data.forEach(function (item, index) {tabWrapper.innerHTML += tools.tplReplace(tpl.tab('tab'), {tab: item.tab,current: !index ? 'current' : ''});pageWrapper.innerHTML += tools.tplReplace(tpl.tab('page'), {page: item.page,current: !index ? 'current' : ''});});oFrag.appendChild(tabWrapper);oFrag.appendChild(pageWrapper);this.el.appendChild(oFrag);}MyTab.prototype._bindEvent = function () {var doms = {oTabItems: doc.querySelectorAll('.tab-item'),oPageItems: doc.querySelectorAll('.page-item')}this.el.addEventListener('click', this._handleTabClick.bind(this, doms), false);}MyTab.prototype._handleTabClick = function () {var _doms = arguments[0],tar = arguments[1].target,className = tar.className.trim();if (className === 'tab-item') {_doms.oTabItems[this._index].className = 'tab-item';_doms.oPageItems[this._index].className = 'page-item';this._index = [].indexOf.call(_doms.oTabItems, tar);tar.className += ' current';_doms.oPageItems[this._index].className += ' current';}}window.MyTab = MyTab; })(document, tpl, tools);tql.js
var tpl = (function () {function tab (field) {switch (field) {case 'tab':return (`<div class="tab-item {{ current }}">{{ tab }}</div>`);case 'page':return (`<div class="page-item {{ current }}">{{ page }}</div>`);default:break;}}return {tab: tab} })();util.js
var tools = (function () {function tplReplace (tpl, replaceObj) {return tpl.replace(/\{\{(.*?)\}\}/g, function (node, key) {return replaceObj[key.trim()];})}return {tplReplace: tplReplace} })();總結
以上是生活随笔為你收集整理的原生js实现一个tab栏的标签操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 个人网页制作 大学生个人网页设计 个人网
- 下一篇: Web Components 上手指南