2015-09-26 23 views
7

今日はCSSアニメーションで遊んでみたいです。CSSアニメーション - 元の位置への位置

私の基本的なアイデアは4つの円を作成してから、ユーザーがその円をクリックするとページの中心に移動してから別の形になるはずです。

私は、変換プロパティとアニメーションプロパティを使用しました。

これは今まで書いたコードです。

$(".circle").click(function(){ 
 
    if($(this).hasClass('centerOfPage')){ 
 
    $(this).removeClass('centerOfPage'); 
 
    }else{ 
 
    $(this).addClass('centerOfPage'); 
 
    } 
 
});
.circle{ 
 
    width: 100px; 
 
    height: 100px; 
 
    border-radius: 50%; 
 
    margin: 10px; 
 
} 
 
.one{ 
 
    background-color: red; 
 
} 
 
.two{ 
 
    background-color: blue; 
 
} 
 
.three{ 
 
    background-color: yellow; 
 
} 
 
.four{ 
 
    background-color: green; 
 
} 
 

 
.centerOfPage{ 
 
    position: fixed; 
 
    top: 50%; 
 
    left: 50%; 
 
    width: 100%; 
 
    height: 100%; 
 
    border-radius: 5%; 
 
    -webkit-transform: translate(-50%, -50%); 
 
    transform: translate(-50%, -50%); 
 
    animation : centerOfPageAnimate 3s; 
 
    
 
} 
 
@keyframes centerOfPageAnimate { 
 
    0% { 
 
    top: 0%; 
 
    left: 0%; 
 
    width: 100px; 
 
    height: 100px; 
 
    border-radius: 50%; 
 
    transform: translate(0, 0);  
 
    } 
 
    100% { 
 
    top: 50%; 
 
    left: 50%; 
 
    -webkit-transform: translate(-50%, -50%); 
 
    transform: translate(-50%, -50%); 
 
    width: 100%; 
 
    height: 100%; 
 
    border-radius: 5%; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="container"> 
 
    <div class="circle one"></div> 
 
    <div class="circle two"></div> 
 
    <div class="circle three"></div> 
 
    <div class="circle four"></div> 
 
</div>

今ここであなたが気づくでしょういくつかの問題が...ある

  • 何かの円をクリックすると、その後彼らのアニメーションはありません、彼らはどこから上隅から始まります。
  • もう一度divをクリックすると、元の位置に戻りますが、アニメーション化されません。もう一度、アニメーションを他の形から丸めて同じ位置に移動します。

ここにはcodepenがあります。おかげさまで

+0

)たい場合、あなたは位置 'へのすべての前に配置されていなかった要素を、切り替えるとき:fixed' ... – CBroe

+0

@nitz:ここには** [GSAP](http://greensock.com/gsap)**ができますウルのアニメーション:** [Codepen](http://codepen.io/tah_med/pen/pjNReG?editors=001)**。これは簡単なデモですが、ここでは多くのことを改善することができます。しかし、それはあなたにGSAPについての考えを与えるはずです。私はこれがあなたが求めているものではないことを知っていますが、あなたがまだ気づいていないなら、私はあなたにそれを紹介すべきだと思いました。 –

答えて

0

すでにjQueryを使用しているので、100%使用することにしました。私のコードとあなたの主な違いは、ロード時に各サークルの位置が保存され、CSS3のキーフレームに依存しないということです。

jQuery .dataメソッドを使用して、ロード時に円の位置を保存します。今度は 'centerOfPage'クラスを削除すると、jQueryを使用して以前に保存された位置に戻ることができます。

はjQueryのスニペットとCodepen jQueryの@nitz

$('.circle').each(function() { 
 
    $(this).data('circlePosTop', $(this).position().top); 
 
}); 
 
\t \t 
 
$(".circle").click(function(){ 
 
if($(this).hasClass('centerOfPage')){ 
 
    $(this).animate({top:$(this).data('circlePosTop'),left:0,borderRadius:'50%',height:'100px',width:'100px'}).removeClass('centerOfPage'); 
 
} else { 
 
    $(this).addClass('centerOfPage').animate({top:0,left:0,borderRadius:'5%',height:'100%',width:'100%'}); 
 
} 
 
});

http://codepen.io/anon/pen/OyWVyP

+0

私は強く反対します。 JQueryは非常に重く、このタイプの視覚的な変更にはできるだけ使用しないでください。 –

+0

@SofiaFerreira、どの意味で「非常に重い? jQuery(より具体的には、JavaScript)アニメーションは、CSSアニメーションが利用可能になるずっと前から存在していました。ハードウェアは今日のハードウェアよりも優れていますので、以前よりも懸念されることは少ないと主張することができます。 JavaScriptベースのアニメーションの唯一の欠点は、CSSベースのアニメーションから得ることができる最適化である**ハードウェアアクセラレーション**を(通常は)活用しないことです。 –

0

は非常に重く、それは視覚的な変化のこのタイプのため極力使用する必要があります参照してください。あなたはこの権利をしています。

アニメーションをキーフレームで削除する必要があります。これはもっと複雑です。

これを試してください:

$(".circle").click(function(){ 
 
    $(this).toggleClass("centerOfPage"); 
 
});
.circle{ 
 
    width: 100px; 
 
    height: 100px; 
 
    border-radius: 50%; 
 
    position: fixed; 
 
    left: 30px; 
 
    
 
    -moz-transition: all 0.5s ease .2s; 
 
    -o-transition: all 0.5s ease .2s; 
 
    -webkit-transition: all 0.5s ease .2s; 
 
    transition: all 0.5s ease .2s; 
 
} 
 
.one{ 
 
    background-color: red; 
 
    top: 10px; 
 
} 
 
.two{ 
 
    background-color: blue; 
 
    top: 120px; 
 
} 
 
.three{ 
 
    background-color: yellow; 
 
    top: 230px; 
 
} 
 
.four{ 
 
    background-color: green; 
 
    top: 340px; 
 
} 
 

 
.centerOfPage{ 
 
    width: 100%; 
 
    height: 100%; 
 
    top: 0; bottom: 0; 
 
    left: 0; right: 0; 
 
    border-radius: 0; 
 
    z-index: 2; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container"> 
 
    <div class="circle one"></div> 
 
    <div class="circle two"></div> 
 
    <div class="circle three"></div> 
 
    <div class="circle four"></div> 
 
</div>

+3

jqueryをアプリケーションに使用しないと訴えれば、javascriptの部分をjavascriptにするのは論理的に思えるでしょうか?結局のところ小さなコードだ – JohannesB

0

これはあなたが探しているものかもしれないですか?

centerOfPageクラスは、アニメーションを開始し、追加の位置付けを行わず、キーフレーム宣言で%を使用するだけで、最初にページの中央に移動してから大きくすることができます(複数のステップあなたは、それは「彼らがどこから開始しない」もちろん

$(".circle").click(function(){ 
 
    $(this).toggleClass('centerOfPage'); //toggleClass saves you the if else 
 
});
.circle{ 
 
    width: 100px; 
 
    height: 100px; 
 
    border-radius: 50%; 
 
    margin: 10px; 
 
    transition: all 2s linear; /* added transition to animate going back to start state*/ 
 
} 
 
.one{ 
 
    background-color: red; 
 
    
 
} 
 
.two{ 
 
    background-color: blue; 
 
} 
 
.three{ 
 
    background-color: yellow; 
 
} 
 
.four{ 
 
    background-color: green; 
 
} 
 

 
.centerOfPage{ 
 
    
 
    animation: test 2s forwards; /* add "forwards" here to stop the animation at end-frame */ 
 
    } 
 

 

 
    @keyframes test{ 
 
    /* You can use percent to target steps during your animation: 
 
     here it moves to the center at 10% animation duration and gets bigger at 100 % animation duration 
 
    */ 
 
    10%{ 
 
    transform: translate(50%); 
 
    } 
 
    100%{ 
 
    width: 100vw; 
 
    height: 100vh; 
 
    border-radius: 5%; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="container"> 
 
    <div class="circle one"></div> 
 
    <div class="circle two"></div> 
 
    <div class="circle three"></div> 
 
    <div class="circle four"></div> 
 
</div>

関連する問題