【发布时间】:2015-05-20 13:02:32
【问题描述】:
我正在构建一个使用 JavaScript 使顶部菜单下拉和关闭的移动页面,但它不起作用。 这是JS
<script>
window.onload = function () {
var elem = document.getElementById('navBarMobile');
var burger = document.getElementById('hamburgerMobile');
var cross = document.getElementById('crossMobile');
}
function menuExpand() {
elem.style.transition = "height 1s linear 0s";
elem.style.height = "292px";
burger.style.transition = "opacity 0.5s linear 0s";
burger.style.opacity = "0";
burger.style.zIndex = "0";
cross.style.transition = "opacity 0.5s linear 0.5s"
cross.style.opacity = "1";
cross.style.zIndex = "1";
}
function menuClose() {
elem.style.transition = "height 1s linear 0s";
elem.style.height = "87px";
burger.style.transition = "opacity 0.5s linear 0.5s";
burger.style.opacity = "1";
burger.style.zIndex = "1";
cross.style.transition = "opacity 0.5s linear 0s";
cross.style.opacity = "0";
burger.style.zIndex = "0";
}
</script>
还有html
<div id="hamburgerMobile" onclick="menuExpand();"></div>
<div id="crossMobile" onclick="menuClose();"></div>
以前,我在函数内部指定位置的顶部看到声明语句,但每个函数只触发一次。
【问题讨论】:
-
直接声明变量而不是window.load函数
标签: javascript mobile web