1. 程式人生 > 其它 >qt tablewidget 實現tab來回切換_原生JS實現TAB選項卡切換效果

qt tablewidget 實現tab來回切換_原生JS實現TAB選項卡切換效果

技術標籤:qt tablewidget 實現tab來回切換

外掛化寫法:

HTML:

tab1 tab2 tab3 tab4 tab5 頁面1 頁面2 頁面3 頁面4 頁面5

css:

.wrap{height:500px;width:80%;margin:50px auto;box-shadow:5px #ccc;}.fl{float:left;}.clearfix:after{content:'';display:table;clear:both;}.tab .t-item{float:left;width:20%;text-align:center;border:1px solid #ccc;box-sizing:border-box;padding:10px 0;cursor:pointer;}.tab .t-item.cur{color:red;background:#efefef;}.page{position:relative;height:450px;}.page .p-item{display:none;line-height:100px;height:100px;text-align:center;position:absolute;top:0px;left:0;height:100%;width:100%;}.page .p-item.cur{display:block;}

效果預覽:


01d66bf706ccb2b75c99b9d1f954e3fc.gif

js:

;(() => { var Tab = function () { this.tab = document.getElementsByClassName('tab')[0] this.tabs = document.getElementsByClassName('t-item') this.pages = document.getElementsByClassName('p-item') } Tab.prototype = { init: function () { this.bindEvent() }, bindEvent: function () { this.tab.addEventListener('click', this.tabClick.bind(this), false) }, tabClick: function (e) { var e = e || window.event, tar = e.target || e.src.Element, className = tar.className, oTabs = this.tabs, oPages = this.pages, len = oTabs.length, thisIndex = Array.prototype.indexOf.bind(oTabs, tar)(), tItem, pItem if (className === 't-item') { for (var i = 0; i < len; i++) { tItem = oTabs[i] pItem = oPages[i] tItem.className = 't-item' pItem.className = 'p-item' } oTabs[thisIndex].className += ' cur' oPages[thisIndex].className += ' cur' } } } return new Tab().init()})()