单击事件侦听器不会启动转换/转换



我正在用纯HTML和CSS3创建一个移动菜单'汉堡'按钮,使用纯JS来处理点击事件。只有一个问题 - 当单击带有 ID "按钮"的按钮时,没有任何反应。根据内置的Firefox元素检查器,这些类没有被添加,导致我相信我在JS中的某个地方搞砸了,但我可能是错的。

另外,忽略"isX"变量 - 稍后将用于指示按钮的状态。

谢谢。

索引.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="menu.js"></script>
<title>Icon Test</title>
<style>
* {
    margin: 0;
    padding: 0;
    border: 0;
}
.container {
    display: block;
    width: 48px;
    height: 48px;
    background: #999;
}
.line-top {
    position: absolute;
    top: 0;
    left: 0;
    fill: #FFF;
    transition: all 2s ease 3s;
}
.line-top-active {
    transform: translateX(0px) rotate(45deg);
    transform-origin: center;
    -moz-transform-origin: center;
}
.line-bottom {
    position: absolute;
    top: 0;
    left: 0;
    fill: #FFF;
    transition: all 2s ease 3s;
}
.line-bottom-active {
    transform: translateX(0px) rotate(-45deg);
    transform-origin: center;
    -moz-transform-origin: center;
}
.rect-top {
    transition: all 2s ease-in-out 0s;
}
.rect-top-active {
    transform: translateY(8);
    transform-origin: center;
    -moz-transform-origin: center;
}
.rect-bottom {
    transition: all 2s ease-in-out 0s;
}
.rect-bottom-active {
    transform: translateY(-8);
    transform-origin: center;
    -moz-transform-origin: center;
}
</style>
</head>
<body>
<button class="container" id="button">
    <svg id="svg-top" class="line-top" x="0px" y="0px" width="48px" viewBox="0 0 96 96" enable-background="new 0 0 96 96">
        <rect id="rect-top" class="rect-top" width="32" height="4" x="32" y="38"></rect>
    </svg>
    <svg id="svg-bottom" class="line-bottom" x="0px" y="0px" width="48px" viewBox="0 0 96 96" enable-background="new 0 0 96 96">
        <rect id="rect-bottom" class="rect-bottom" width="32" height="4" x="32" y="54"></rect>
    </svg>
</button>
</body>
</html>

菜单.js

var isX = false;
var button = document.getElementById('button');
button.addEventListener('click', function(e) {
    var topSvg = document.getElementById('svg-top');
    var bottomSvg = document.getElementById('svg-bottom');  
    var topLine = document.getElementById('rect-top');
    var bottomLine = document.getElementById('rect-bottom');
    if(isX) {
    } else {
        topLine.classList.add('rect-top-active');
        bottomLine.classList.add('rect-bottom-active');
        topSvg.classList.add('line-top-active');
        bottomSvg.classList.add('line-bottom-active');  
    }
}, false);

编辑#1:适应凯的回应

"addEventListener" 的 'event type' 参数应该是一个字符串。

button.addEventListener('click', function(e) {

DOM 尚未加载 - 它无法找到元素。我使用window.onload来解决这个问题。

function init() {
    alert("Loaded");
    document.getElementById('button').onclick = function() {
        alert("Success");
        var topSvg = document.getElementById('svg-top');
        var bottomSvg = document.getElementById('svg-bottom');  
        var topLine = document.getElementById('rect-top');
        var bottomLine = document.getElementById('rect-bottom');
        topLine.setAttribute('class', 'rect-top-active'); 
        bottomLine.setAttribute('class', 'rect-bottom-active');
        topSvg.setAttribute('class', 'line-top-active');
        bottomSvg.setAttribute('class', 'line-bottom-active');  
    }
};
window.onload = init;

最新更新