js實現菜單跳轉效果
本文實例為大家分享了js實現菜單跳轉效果的具體代碼,供大家參考,具體內容如下
想要的效果圖:
直接上代碼
<!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'>關注</span> <span>推薦</span> <span>熱點</span> <span>本地</span> <span>段子</span> </div> <div class='body'> <div class='active'>關注:早上吃什么?</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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
