2017-08-02 65 views
-3

我有一些在移動時運行的js。當瀏覽器高於768時,此功能不應運行。還有一種方法可以在香草js中恢復附加方法嗎?運行函數的條件語句?

if (window.innerWidth < 768) { 
    mobileNav(); 
} else { 

} 
+0

哪裏是你的'追加()'方法? – Weedoze

+1

嗯,爲什麼dowsnt的工作,請把它放到上下文 –

+1

恢復追加?那麼你只需在你剛添加的元素上使用'.remove()'方法'.append()' – Ivan

回答

0

使用此條件中添加和刪除手機菜單

var breakpoint = matchMedia("(min-width: 400px)") 
 
var message1 = document.createElement('p') 
 
var message2 = document.createElement('p') 
 

 
message1.innerText = 'the viewport is at least 400 pixels wide' 
 
message2.innerText = 'the viewport is less than 400 pixels wide' 
 

 
function render() { 
 
    if (breakpoint.matches) { 
 
     // the viewport is at least 400 pixels wide 
 
     // add mobileNav() 
 
     message2.remove() 
 
     document.body.append(message1) 
 
    } else { 
 
     // the viewport is less than 400 pixels wide 
 
     // remove mobile nav 
 
     message1.remove() 
 
     document.body.append(message2) 
 
    } 
 
} 
 

 
render() // render initial 
 
breakpoint.onchange = render // as well on changes