js實(shí)現(xiàn)菜單跳轉(zhuǎn)效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)菜單跳轉(zhuǎn)效果的具體代碼,供大家參考,具體內(nèi)容如下
想要的效果圖:
直接上代碼
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Document</title> <style> .box { width: 500px; height: 300px; /* border: 1px solid #ccc; */ margin: 50px auto; } span { width: 80px; height: 30px; border: 1px solid #ccc; display: inline-block; text-align: center; line-height: 30px; } .body { margin-top: 5px; width: 450px; border: 1px solid #ccc; height: 250px; background: #eee; } .current { color: red; background: #555; } .body div { display: none; text-align: center; line-height: 250px; } .body .active { display: block } </style></head><body> <div class='box'> <div class='header'> <span class='current'>關(guān)注</span> <span>推薦</span> <span>熱點(diǎn)</span> <span>本地</span> <span>段子</span> </div> <div class='body'> <div class='active'>關(guān)注:早上吃什么?</div> <div>推薦:中午吃什么?</div> <div>熱點(diǎn):晚飯吃什么?</div> <div>本地:宵夜吃什么?</div> <div>段子:半夜吃什么?</div> </div> </div> <script> var oSpan = document.querySelectorAll(’span’) var oDiv = document.querySelector(’.body’).children for (let i = 0; i < oSpan.length; i++) { oSpan[i].index = i console.log(oSpan[i].index); oSpan[i].onclick = function () {for (let k = 0; k < oSpan.length; k++) { oSpan[k].classList.remove(’current’)}this.classList.add(’current’)console.log(oSpan[i].index);for (let k = 0; k < oSpan.length; k++) { oDiv[k].classList.remove(’active’)}oDiv[this.index].classList.add(’active’) } } </script></body></html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP.NET MVC實(shí)現(xiàn)樹形導(dǎo)航菜單2. 多級聯(lián)動下拉選擇框,動態(tài)獲取下一級3. 如何封裝一個Ajax函數(shù)4. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲5. Ajax常用封裝庫——Axios的使用6. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))7. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera8. WML學(xué)習(xí)之七 CGI編程9. Vue實(shí)現(xiàn)用戶沒有登陸時,訪問后自動跳轉(zhuǎn)登錄頁面的實(shí)現(xiàn)思路10. 微信開發(fā) 網(wǎng)頁授權(quán)獲取用戶基本信息
