onclick事件后CSS动画不会触发

时间:2014-12-10 16:02:40

标签: javascript css

我有css块,当他们获得.gone类时必须离开页面。

我在Javascript中注册了一个click事件,在事件处理程序中我将.gone类添加到被点击的元素。

子弹应该向左或向右移动,但它会消失。

以下是HTML代码:

<div id="firstPage">
    <div id="bullets">
        <div data-href="#projects"    class="top left">Projects</div>
        <div data-href="#skills"      class="top right">Skills</div>
        <div data-href="#experiences" class="bottom left">Experiences</div>
        <div data-href="#contact"     class="bottom right">Contact</div>
    </div>
</div>

javascript代码:

var bullets = [];

function openPage(e) {
    e.preventDefault();
    this.classList.add('gone');
}

var tmpBullets = document.querySelectorAll('#bullets div');
for(var i = 0 ; i < tmpBullets.length ; i++) {
    tmpBullets[i].addEventListener('click', openPage, true);
    bullets.push(tmpBullets[i]);
}

CSS代码:

html {
    font-family: QuattrocentoSans;
    overflow: hidden;
}

#firstPage {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: url('../images/noise.png');
}

#firstPage h1 {
    display: block;
    margin: auto;
    text-align: center;
    margin-top: 100px;
    font-family: Pacifico;
    font-size: 50px;
    color: #fff;
    text-shadow: 0 0 3px #000;
}

#bullets {
    display: block;
    width: 320px;
    margin: auto;
}

#bullets div {
    position: absolute;
    display: inline-block;
    width: 150px;
    height: 150px;
    line-height: 150px;
    border-radius: 50%;
    background-color: #333;
    text-align: center;
    color: white;
    text-decoration: none;
    margin-top: 10px;
    margin-right: 5px;
    margin-left: 5px;
    text-shadow: 0 0 3px #999;
    font-size: 1.2rem;
    transition: box-shadow 500ms, left 1000ms, right 1000ms;
}

#bullets div.top {
    top: 100px;
}

#bullets div.bottom {
    top: 270px;
}

#bullets div.left {
    left: calc(50% - 165px);
}

#bullets div.right {
    right: calc(50% - 165px);
}

#bullets div:hover {
    box-shadow: 0 0 10px #555;
    transition: box-shadow 500ms;
}

#bullets div.left.gone {
    left: -160px;
}

#bullets div.right.gone {
    right: -160px;
}

请参阅jsfiddle了解实时演示:http://jsfiddle.net/8u9j6n6x/

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您需要将转换添加到.gone类而不是#bullets div

#bullets div.gone {
    transition: box-shadow 500ms, left 1000ms, right 1000ms;
}

更新了小提琴http://jsfiddle.net/8u9j6n6x/1/