2 回答

TA貢獻1744條經驗 獲得超4個贊
就像在回調中重置滾動一樣,beforeEnter您還需要從腳本中調用 init 方法,以在新頁面加載時重置它們。afterEnter如果您需要訪問新頁面 dom,您也可以這樣做。來自 barba.js 文檔:
barba.init({
? views: [{
? ? namespace: 'home',
? ? beforeEnter() {
? ? ? // re-init/reset script default function on before page load
? ? ? myScriptInit();
? ? },
? ? afterEnter() {
? ? ? // refresh parallax on new page content
? ? ? parallax.refresh();
? ? }
? }]
});

TA貢獻1786條經驗 獲得超11個贊
就我而言,這有幫助:
barba.hooks.after(() => {
const bottomDOM = document.getElementsByTagName("body")[0]
const newScript = document.createElement("script")
const oldScript = document.querySelector(".main-script")
newScript.src = "js/main-dist.js"
newScript.className = "main-script"
oldScript.remove()
bottomDOM.appendChild(newScript)
})
我<script class="main-script"></script>在 DOM 末尾有一個,并使用 barba.hook 刪除它,然后再次添加它
添加回答
舉報