MUI 底部導航 (選中高亮改變icon)
阿新 • • 發佈:2019-02-03
底部導航欄應該是現在主流APP最常用的一種顯示方式,我個人也覺得底部導航欄最清晰直觀,一般以4-5個為宜。這裡我做了點小東西,就是點選某個選項卡的時候,它自動高亮,並且改變icon,這樣整體感覺就非常動感,比如資訊選項卡,未點選的時候是一個封閉的信封,一點就變成開啟的信封,要的就是這個效果。這裡主要用到如下幾種東西:【底部選項卡】,【外部icon引用】,【icon自動變換】,見程式碼和註釋,上面的都是廢話,一般的東西我都直接寫在註釋裡,這樣更加直觀。
//html部分,底部五個選項卡,public-icon-style這種css都是本人自己的css,設定一些icon的樣式, //因為在MUI開發裡,不管是自帶icon還是外部引用的icon都是以字型樣式來處理,比如顏色,color, //大小,font-size,如果引用自帶的icon非常簡單,直接兩個CSS:mui-icon和具體icon名稱,但是有些時 //候一些特殊的業務需求,比如找不到你需要的icon,比如你想像我一樣實現兩個狀態的icon動態切換,那 //就只能引用外部icon了,很簡單,方法如下,百度搜:阿里向量圖,或者直接輸入iconfont.cn/home, //註冊,搜尋需要的icon然後,下載原始碼,這時解壓,我們需要兩個檔案,一個是iconfont.ttf檔案,也 //就是icon的字型標準檔案,放在我們font資料夾下面,還有一個iconfont.css檔案,也就是我們下載的 //icon樣式檔案,放在css資料夾下面,裡面有一段程式碼url('../fonts/iconfont.ttf?t=1533048640652') // format('truetype'),地址改成剛才font資料夾下,其實你做一個專案,直接在阿里向量網站建一個 //專案,每次找icon都直接加入購物車,然後下載原始碼,這樣每次去下載好的檔案,解壓覆蓋即可 <nav class="mui-bar mui-bar-tab public-white-bg"> <a id="defaultTab" class="mui-tab-item mui-active" href="html/home.html"> <span class="mui-icon iconfont icon-shouye2 public-icon-style" id="home"></span> <span class="mui-tab-label public-icon-style" id="hometa">首頁</span> </a> <a class="mui-tab-item" href="html/message.html"> <span class="mui-icon iconfont icon-icon-xinfeng" id="message"><span style="display: none;" class="mui-badge" >1</span></span> <span class="mui-tab-label" id="messageta">資訊</span> </a> <a class="mui-tab-item" href="html/bigdata.html"> <span class="mui-icon iconfont icon-data" id="bigdata"></span> <span class="mui-tab-label" id="bigdatata">大資料</span> </a> <a class="mui-tab-item" href="html/find.html"> <span class="mui-icon iconfont icon-pengyouquan" id="find"></span> <span class="mui-tab-label" id="findta">發現</span> </a> <a class="mui-tab-item" href="html/know.html"> <span class="mui-icon iconfont icon-iconxuexi" id="know"></span> <span class="mui-tab-label" id="knowta">知道</span> </a> </nav> //script部分 //mui初始化 mui.init(); //我的五個子頁面 var subpages = ['html/home.html', 'html/message.html', 'html/bigdata.html','html/find.html','html/know.html']; var subpage_style = { top:'0px', bottom: '51px' }; var aniShow = {}; //建立子頁面,首個選項卡頁面顯示,其它均隱藏; mui.plusReady(function() { var self = plus.webview.currentWebview(); for (var i = 0; i < subpages.length; i++) { var temp = {}; var sub = plus.webview.create(subpages[i], subpages[i], subpage_style); if (i > 0) { sub.hide(); }else{ temp[subpages[i]] = "true"; mui.extend(aniShow,temp); } self.append(sub); } }); //當前啟用選項 var activeTab = subpages[0]; //選項卡點選事件 mui('.mui-bar-tab').on('tap', 'a', function(e) { var targetTab = this.getAttribute('href'); if (targetTab == activeTab) { return; } //顯示目標選項卡 //將目標選項卡的icon替換 changeIcon(targetTab); //若為iOS平臺或非首次顯示,則直接顯示 if(mui.os.ios||aniShow[targetTab]){ plus.webview.show(targetTab); }else{ //否則,使用fade-in動畫,且儲存變數 var temp = {}; temp[targetTab] = "true"; mui.extend(aniShow,temp); plus.webview.show(targetTab,"fade-in",300); } //隱藏當前; plus.webview.hide(activeTab); //更改當前活躍的選項卡 activeTab = targetTab; }); //自定義事件,模擬點選“首頁選項卡”,在有需要的地方可以直接呼叫這種自定義事件 document.addEventListener('gohome', function() { var defaultTab = document.getElementById("defaultTab"); //模擬首頁點選 mui.trigger(defaultTab, 'tap'); //切換選項卡高亮 var current = document.querySelector(".mui-bar-tab>.mui-tab-item.mui-active"); //console.log(current); if (defaultTab !== current) { current.classList.remove('mui-active'); defaultTab.classList.add('mui-active'); } }); //點選底部選項卡,替換icon圖示 function changeIcon(targetTab){ var home = document.getElementById('home'); var message = document.getElementById('message'); var bigdata = document.getElementById('bigdata'); var find = document.getElementById('find'); var know = document.getElementById('know'); var hometa = document.getElementById('hometa'); var messageta = document.getElementById('messageta'); var bigdatata = document.getElementById('bigdatata'); var findta = document.getElementById('findta'); var knowta = document.getElementById('knowta'); if(targetTab=='html/home.html'){ home.classList.remove('icon-shouye'); home.classList.add('icon-shouye2'); home.classList.add('public-icon-style'); hometa.classList.add('public-icon-style'); message.classList.remove('icon-icon-xinfeng2'); message.classList.add('icon-icon-xinfeng'); message.classList.remove('public-icon-style'); messageta.classList.remove('public-icon-style'); bigdata.classList.remove('public-icon-style'); bigdatata.classList.remove('public-icon-style'); find.classList.remove('public-icon-style'); findta.classList.remove('public-icon-style'); know.classList.remove('public-icon-style'); knowta.classList.remove('public-icon-style'); bigdata.classList.remove('icon-data2'); bigdata.classList.add('icon-data'); find.classList.remove('icon-pengyouquan2'); find.classList.add('icon-pengyouquan'); know.classList.remove('icon-iconxuexi2'); know.classList.add('icon-iconxuexi'); }else if(targetTab=='html/message.html'){ message.classList.remove('icon-icon-xinfeng'); message.classList.add('icon-icon-xinfeng2'); message.classList.add('public-icon-style'); messageta.classList.add('public-icon-style'); bigdata.classList.remove('public-icon-style'); bigdatata.classList.remove('public-icon-style'); find.classList.remove('public-icon-style'); findta.classList.remove('public-icon-style'); know.classList.remove('public-icon-style'); knowta.classList.remove('public-icon-style'); home.classList.remove('public-icon-style'); hometa.classList.remove('public-icon-style'); home.classList.remove('icon-shouye2'); home.classList.add('icon-shouye'); bigdata.classList.remove('icon-data2'); bigdata.classList.add('icon-data'); find.classList.remove('icon-pengyouquan2'); find.classList.add('icon-pengyouquan'); know.classList.remove('icon-iconxuexi2'); know.classList.add('icon-iconxuexi'); }else if(targetTab=='html/bigdata.html'){ bigdata.classList.remove('icon-data'); bigdata.classList.add('icon-data2'); bigdata.classList.add('public-icon-style'); bigdatata.classList.add('public-icon-style'); home.classList.remove('icon-shouye2'); home.classList.add('icon-shouye'); message.classList.remove('icon-icon-xinfeng2'); message.classList.add('icon-icon-xinfeng'); find.classList.remove('icon-pengyouquan2'); find.classList.add('icon-pengyouquan'); know.classList.remove('icon-iconxuexi2'); know.classList.add('icon-iconxuexi'); message.classList.remove('public-icon-style'); messageta.classList.remove('public-icon-style'); home.classList.remove('public-icon-style'); hometa.classList.remove('public-icon-style'); find.classList.remove('public-icon-style'); findta.classList.remove('public-icon-style'); know.classList.remove('public-icon-style'); knowta.classList.remove('public-icon-style'); }else if(targetTab=='html/find.html'){ find.classList.remove('icon-pengyouquan'); find.classList.add('icon-pengyouquan2'); find.classList.add('public-icon-style'); findta.classList.add('public-icon-style'); home.classList.remove('icon-shouye2'); home.classList.add('icon-shouye'); message.classList.remove('icon-icon-xinfeng2'); message.classList.add('icon-icon-xinfeng'); bigdata.classList.remove('icon-data2'); bigdata.classList.add('icon-data'); know.classList.remove('icon-iconxuexi2'); know.classList.add('icon-iconxuexi'); message.classList.remove('public-icon-style'); messageta.classList.remove('public-icon-style'); home.classList.remove('public-icon-style'); hometa.classList.remove('public-icon-style'); bigdata.classList.remove('public-icon-style'); bigdatata.classList.remove('public-icon-style'); know.classList.remove('public-icon-style'); knowta.classList.remove('public-icon-style'); }else if(targetTab=='html/know.html'){ know.classList.remove('icon-iconxuexi'); know.classList.add('icon-iconxuexi2'); know.classList.add('public-icon-style'); knowta.classList.add('public-icon-style'); home.classList.remove('icon-shouye2'); home.classList.add('icon-shouye'); message.classList.remove('icon-icon-xinfeng2'); message.classList.add('icon-icon-xinfeng'); bigdata.classList.remove('icon-data2'); bigdata.classList.add('icon-data'); find.classList.remove('icon-pengyouquan2'); find.classList.add('icon-pengyouquan'); message.classList.remove('public-icon-style'); messageta.classList.remove('public-icon-style'); home.classList.remove('public-icon-style'); hometa.classList.remove('public-icon-style'); bigdata.classList.remove('public-icon-style'); bigdatata.classList.remove('public-icon-style'); find.classList.remove('public-icon-style'); findta.classList.remove('public-icon-style'); } }//其實做這種動態原理很簡單,就是你點選某個選項卡的時候,把它原來圖示CSS給 //刪除了,然後重新新增一個圖示CSS,其實我們做APP,主要還是做原理,做頁面,比如第一篇文章說的 //做那種縮放效果,你知道了原理就好做了,並不是什麼神奇的不得了的東西,但是你加上了,整個APP感 //覺就動態了,高階了,APP後臺互動不就是你傳引數,後臺返回你資料,這麼簡單,重點還 //是看HTML+CSS+JS+MUI(封裝框架),一點個人心得,大家見笑
程式碼留下了,當然需要幾張圖來直觀感受下,是我自己正在做的APP,一點點完成,一點點記錄,希望能幫到有需要的人
大家看我的資訊選項卡就是這種效果,未點選就是信封閉合狀態,一點選就開啟,是不是很有意思,也許你覺得P用沒有,那就PASS咯,只是個人喜好,希望APP能動感一點,好了,今天就記錄到這。