javascript - 用Javascript修改CSS3动画

标签 javascript css css-transitions

首先,这是我需要帮助的站点的基本代码: 检查底部是否有编辑

我需要一些关于动画本身的帮助,因为如果我在 div 1 并按任意键,它就像一个魅力。但是如果我在 div 3 并且想转到 div 2,例如,它首先加载 div 1 然后转到 div 2。我知道它为什么这样做,但我想不出一种方法来绕过它。有什么想法吗?

如果动画已经在播放,我还想暂停来自输入的功能(这样你就不会发送垃圾邮件并弄乱动画)。我怎样才能轻松做到这一点?

谢谢!

编辑

由于一些非常好的人要求我不要偷懒,OP 提供: http://jsfiddle.net/G4PXj/7

还有; http://codepen.io/anon/pen/zJsBH

此外,按 HTML-box,然后按 2,3 或 4,等待几秒钟让动画加载到 FOV 中。

HTML:

<div id="fourth" class="hidden">Fourth box
    <button onClick="fubar('first');">First page</button>
</div>
<div id="third" class="hidden">Third div</div>
<div id="second" class="hidden">Som text in the second</div>
<div id="first">Some content
    <button onClick="fubar('second');">Second page</button>
</div>

风格:

@charset "utf-8";
body {
    background-color: #fff;
    margin: 0;
    padding: 0;
    overflow: hidden;
}
#first {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    color: #fff;
    box-shadow: 0 0 40px #333;
    background-color: #000;
}
#second {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    background-color: green;
}
#third {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    background-color: red;
}
#fourth {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    background-color: blue;
}
.hidden {
    display: none;
}
.test {
    -moz-animation-name: wat;
    -webkit-animation-name: wat;
    -moz-animation-duration: 4s;
    -webkit-animation-duration: 4s;
    -moz-animation-timing-function: ease-out;
    -webkit-animation-timing-function: ease-out;
    -moz-animation-iteration-count: 1;
    -webkit-animation-iteration-count: 1;
    -moz-animation-direction: normal;
    -webkit-animation-direction: normal;
    z-index: 100;
    display: block;
}
@-moz-keyframes wat {
    0% {margin-left:-3000px;}
    60% {margin-left: 200px;}
    100% {margin-left: 0;}
}
@-webkit-keyframes wat {
    0% {margin-left:-3000px;}
    60% {margin-left: 200px;}
    100% {margin-left: 0;}
}

Javascript:

function clearAll() {
    document.getElementById('first').className = '';
    document.getElementById('second').className = '';
    document.getElementById('third').className = '';
    document.getElementById('fourth').className = '';
}

function troll(objectID) {
    document.getElementById(objectID).className += ' test';
}

function fubar(objectID) {
    clearAll();
    troll(objectID);
}

function KeyCheck(e) {
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    switch (KeyID) {

    case 49:
        clearAll();
        troll('first');
        break;

    case 50:
        clearAll();
        troll('second');
        break;

    case 51:
        clearAll();
        troll('third');
        break;

    case 52:
        clearAll();
        troll('fourth');
        break;
    }
}
document.onkeyup = KeyCheck;

最佳答案

您可以跟踪上一页,以便保留其 z-index

// CSS
.page{
  z-index: 0;
  // your other page settings
}
.previous{
  z-index: 99;
}
.test{
  z-index: 100;
  // your animation settings
}

代码:

document.getElementById('button1').onclick = function(){ gotoPage('first'); }
document.getElementById('button2').onclick = function(){ gotoPage('second'); }
var previous, current;

function gotoPage(id){
    // simplified className manipulation
    if(previous) previous.className = 'page';
    previous = current;
    if(previous) previous.className = 'page previous';
    current = document.getElementById(id);
    current.className = 'page test';
}

// key detection, etc.

jQuery 版本:

// JS
$('#button1').click(function(){ gotoPage('first'); });
$('#button2').click(function(){ gotoPage('second'); });
var $previous, $current;

function gotoPage(id){
  if($previous) $previous.removeClass('previous');
  $previous = $current;
  if($previous) $previous.removeClass('test').addClass('previous');
  $current = $('#' + id).addClass('test').removeClass('hidden');
}

// key detection, etc.

fiddle :http://jsfiddle.net/G4PXj/9/

这确保了具有以下 z-index 的页面布局:当前为 100(即动画页面,您希望它显示在所有其他页面之上),99 为上一个(您要离开的页面:显示在所有其他页面之上) , 但仍落后于动画页面),其余为 0,即与动画无关的页面。

gotoPage() 函数将您要离开的页面(即当前页面)设置为上一页,将您要到达的页面(即 document.getElementById(id))设置为当前页面。然后,布局将应用于这些新页面设置。

至于停顿,这是相关问题中的一个很好的例子: How to pause and resume CSS3 animation using JavaScript?

关于javascript - 用Javascript修改CSS3动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12681528/

相关文章:

javascript - Business Catalyst 中的 jQuery 日期格式

javascript - d3-drag 0.3.0 - "Cannot read property ' button' of null"

php - 如何像 Facebook 那样从网页中提取图像?

javascript - 如果文本位于两个哈希标签之间,则突出显示文本 ##ccc## 文本 ##234##

jquery - 重写 Jquery fadeIn() 以使用 CSS3 过渡

jQuery 用动画改变 DIV 背景

html - 在一个 CSS 类中组合不同的用户代理属性

html - CSS 箭头在 IE 8 中不正确

javascript - 如何使用 Vue.js 在选择选项上使用转换

animation - 鼠标悬停时CSS3背景旋转动画