原生js实现tab栏切换效果
生活随笔
收集整理的這篇文章主要介紹了
原生js实现tab栏切换效果
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我是歌謠 放棄很容易 但是堅持一定很酷 微信公眾號關注小歌謠一起學習前后端知識
運行效果
首先我們來看一下原生js實現的效果
下面就開始直接上代碼了
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>util.js
var tools = (function () {function tplReplace (tpl, replaceObj) {return tpl.replace(/\{\{(.*?)\}\}/g, function (node, key) {return replaceObj[key.trim()];})}return {tplReplace: tplReplace} })();tpl.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} })();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);index.js
// 立即執行函數 // IIFE Immediately Invoked Function Expression // 立即地 調用 (函數 表達式)// 函數聲明 !== 函數表達式 // function test1 () { // console.log('Function Declaration'); // }// // 把一個(匿名)函數(函數聲明式)賦值給一個變量的形式 函數表達式 // var test2 = function () { // console.log('Function Expression'); // }// // () 對于函數名后面的括號,叫做執行符號 // test1(); // test2();// 語法錯誤,執行符號只能跟在函數表達式后面 // function test () { // console.log('Function Declaration'); // }();// 立即地 調用 (函數 表達式) // 當一個函數需要立即執行的情況,該函數比較形成表達式形式// 1 //(1)//+1 //!1// ~1// W3C推薦的立即執行函數的編寫規范 // (function () { // console.log('Function Expression'); // }());// 實踐中 // ;(function () { // console.log('Function Expression'); // })()// ;(function () { // console.log('Function Expression'); // })()(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);我是歌謠 放棄很容易 但是堅持一定很酷
總結
以上是生活随笔為你收集整理的原生js实现tab栏切换效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tomcat编码配置gbk_Tomcat
- 下一篇: 你提交代码前没有校验?巧用gitHook