use*_*765 1 javascript mobile web
我正在构建一个使用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>
Run Code Online (Sandbox Code Playgroud)
和HTML
<div id="hamburgerMobile" onclick="menuExpand();"></div>
<div id="crossMobile" onclick="menuClose();"></div>
Run Code Online (Sandbox Code Playgroud)
之前,我在函数内部的指定位置顶部看到了声明语句,但每个函数只触发一次.
变量elem,burger并cross在不同的范围内定义,并且从您的函数中看不到.
您应该尝试先声明它:
var elem;
var burger;
var cross;
window.onload = function () {
elem = document.getElementById('navBarMobile');
burger = document.getElementById('hamburgerMobile');
cross = document.getElementById('crossMobile');
}
Run Code Online (Sandbox Code Playgroud)
阅读有关功能范围的信息:http: //www.w3schools.com/js/js_scope.asp
| 归档时间: |
|
| 查看次数: |
80 次 |
| 最近记录: |