js實現(xiàn)菜單跳轉(zhuǎn)效果
本文實例為大家分享了js實現(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>熱點</span> <span>本地</span> <span>段子</span> </div> <div class='body'> <div class='active'>關(guān)注:早上吃什么?</div> <div>推薦:中午吃什么?</div> <div>熱點:晚飯吃什么?</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. jscript與vbscript 操作XML元素屬性的代碼2. 不要在HTML中濫用div3. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)4. XML在語音合成中的應(yīng)用5. HTML5實戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)6. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別7. XML入門的常見問題(三)8. 基于PHP做個圖片防盜鏈9. ASP基礎(chǔ)入門第四篇(腳本變量、函數(shù)、過程和條件語句)10. php使用正則驗證密碼字段的復(fù)雜強度原理詳細講解 原創(chuàng)
