因此,我的目標(biāo)是創(chuàng)建一個(gè)與導(dǎo)航欄一起使用的單頁(yè)應(yīng)用程序。這是一個(gè)音樂(lè)評(píng)論網(wǎng)站,因此導(dǎo)航欄上會(huì)顯示“ Pop”,“ HipHop”和“ Jazz”。對(duì)于每種音樂(lè)類(lèi)型,我都有一個(gè)單獨(dú)的html頁(yè)面,但我只想擁有一個(gè)大的html頁(yè)面。我有可以使此功能起作用的Javascript代碼,但是我不確定如何編輯導(dǎo)航欄的內(nèi)容以使這種情況發(fā)生。這是我的javascript文件和導(dǎo)航欄,如下所示:我真的很感謝您的幫助 pages: [], show: new Event('show'), init: function(){ app.pages = document.querySelectorAll('.page'); app.pages.forEach((pg)=>{ pg.addEventListener('show', app.pageShown); }) document.querySelectorAll('.nav-link').forEach((link)=>{ link.addEventListener('click', app.nav); }) history.replaceState({}, 'Home', '#home'); window.addEventListener('popstate', app.poppin); }, nav: function(ev){ ev.preventDefault(); let currentPage = ev.target.getAttribute('data-target'); document.querySelector('.active').classList.remove('active'); document.getElementById(currentPage).classList.add('active'); console.log(currentPage) history.pushState({}, currentPage, `#${currentPage}`); document.getElementById(currentPage).dispatchEvent(app.show); }, pageShown: function(ev){ console.log('Page', ev.target.id, 'just shown'); let h1 = ev.target.querySelector('h1'); h1.classList.add('big') setTimeout((h)=>{ h.classList.remove('big'); }, 1200, h1); }, poppin: function(ev){ console.log(location.hash, 'popstate event'); let hash = location.hash.replace('#' ,''); document.querySelector('.active').classList.remove('active'); document.getElementById(hash).classList.add('active'); console.log(hash) //history.pushState({}, currentPage, `#${currentPage}`); document.getElementById(hash).dispatchEvent(app.show); }}
如何使用導(dǎo)航欄創(chuàng)建單頁(yè)應(yīng)用程序?
元芳怎么了
2021-04-19 16:14:43