繁体   English   中英

onclick事件后不会触发CSS动画

[英]CSS animation doesn't trigger after onclick event

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

我在Javascript中注册了click事件,在事件处理程序中,将.gone类添加到了clicked元素中。

子弹应该向左或向右走,但会消失。

这是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/

谢谢你的帮助

您需要将过渡添加到.gone类,而不是#bullets div

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM