Click事件监听器不会启动转换/转换?

时间:2014-11-17 01:16:31

标签: javascript html css3

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

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

感谢。

index.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>

menu.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:容纳Kai的回应

2 个答案:

答案 0 :(得分:2)

&#39;事件类型&#39; &#34; addEventListener&#34;的参数应该是一个字符串。

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

答案 1 :(得分:0)

尚未加载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;
相关问题