我有一些js在移动时运行。当浏览器高于768时,此功能不应运行。还有办法在vanilla js中恢复一个追加方法吗?
if (window.innerWidth < 768 ) {
mobileNav();
} else {
}
答案 0 :(得分: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
&#13;