2017-03-22 12 views
2

私は3つの異なる要素のキーフレームアニメーションを同期させたいと考えています。cssキーフレームアニメーションの同期

これらは基本的に3つのハートで、クリックするとハートビートアニメーションに従います。

2つ以上がクリックされたときに、それらを同期させて「ビート」します。

.rating { 
 
    position: relative; 
 
    top: 0; 
 
    left: 0; 
 
    right: 0; 
 
    bottom: 0; 
 
    width: 140px; 
 
    height: 50px; 
 
    padding: 5px 10px; 
 
    margin: auto; 
 
    border-radius: 30px; 
 
    background: #FFF; 
 
    display: block; 
 
    overflow: hidden; 
 
    unicode-bidi: bidi-override; 
 
    direction: rtl; 
 
} 
 

 
.rating:not(:checked)>input { 
 
    display: none; 
 
} 
 

 

 
/* - - - - - LIKE */ 
 

 
#like { 
 
    bottom: -65px; 
 
} 
 

 
#like:not(:checked)>label { 
 
    cursor: pointer; 
 
    float: right; 
 
    width: 30px; 
 
    height: 30px; 
 
    display: block; 
 
    margin-right: 7.5px; 
 
    color: rgba(233, 54, 40, .4); 
 
    line-height: 42px; 
 
    text-align: center; 
 
    transition: color 0.2s; 
 
} 
 

 
#like:not(:checked)>label:hover, 
 
#like:not(:checked)>label:hover~label { 
 
    color: rgba(233, 54, 40, .6); 
 
} 
 

 
#like>input:checked+label:hover, 
 
#like>input:checked+label:hover~label, 
 
#like>input:checked~label:hover, 
 
#like>input:checked~label:hover~label, 
 
#like>label:hover~input:checked~label { 
 
    color: rgb(233, 54, 40); 
 
} 
 

 
#like>input:checked~label { 
 
    color: rgb(233, 54, 40); 
 
    animation: 1s heartbeat infinite; 
 
} 
 

 
@keyframes heartbeat { 
 
    from { 
 
    transform: scale(1); 
 
    transform-origin: center center; 
 
    animation-timing-function: ease-out; 
 
    } 
 
    10% { 
 
    transform: scale(1.2); 
 
    animation-timing-function: ease-in; 
 
    } 
 
    15% { 
 
    transform: scale(1); 
 
    animation-timing-function: ease-out; 
 
    } 
 
    25% { 
 
    transform: scale(1.15); 
 
    animation-timing-function: ease-in; 
 
    } 
 
    35% { 
 
    transform: scale(1); 
 
    animation-timing-function: ease-out; 
 
    } 
 
}
<section id="like" class="rating"> 
 

 
    <!-- THIRD HEART --> 
 
    <input type="radio" id="heart_3" name="like" value="3" /> 
 
    <label for="heart_3" class="heart-slider">&#9829;</label> 
 

 
    <!-- SECOND HEART --> 
 
    <input type="radio" id="heart_2" name="like" value="2" /> 
 
    <label for="heart_2" class="heart-slider">&#9829;</label> 
 

 
    <!-- FIRST HEART --> 
 
    <input type="radio" id="heart_1" name="like" value="1" /> 
 
    <label for="heart_1" class="heart-slider">&#9829;</label> 
 

 
</section>

同期を達成するための良い方法は何ですか:

あなたは、私がこれまで持っていることであるJSbin here

を確認することができますか?

は浪人の答えに続き

EDIT

が、ここで仕事を取得し、更新CSSだありがとう:

#like { 
} 
#like:not(:checked) > label { 
    cursor:pointer; 
    float: right; 
    width: 30px; 
    height: 30px; 
    display: inline-block; 

    color: (255, 255, 255); 

    transition: color 0.2s; 
} 

#like:not(:checked) > label:hover, 
#like:not(:checked) > label:hover ~ label { 
    color: rgba(252, 108, 133, 1); 
} 
#like > input:checked + label:hover, 
#like > input:checked + label:hover ~ label, 
#like > input:checked ~ label:hover, 
#like > input:checked ~ label:hover ~ label, 
#like > label:hover ~ input:checked ~ label { 
color: rgb(252, 108, 133); 

} 
#like > input:checked ~ label { 
    color: rgb(252, 108, 133); 

} 

#heart_1:checked ~ label { 
animation: heartbeat1 1s infinite; 
} 

#heart_2:checked ~ label { 
animation: heartbeat2 1s infinite; 
} 

#heart_3:checked ~ label { 
animation: heartbeat3 1s infinite; 
} 


@keyframes heartbeat1 { 
    from { 
    transform: scale(1); 
    transform-origin: center center; 
    animation-timing-function: ease-out; 
    } 
    10% { 
    transform: scale(1.2); 
    animation-timing-function: ease-in; 
    } 
    15% { 
    transform: scale(1); 
    animation-timing-function: ease-out; 
    } 
    25% { 
    transform: scale(1.15); 
    animation-timing-function: ease-in; 
    } 
    35% { 
    transform: scale(1); 
    animation-timing-function: ease-out; 
    } 
} 

@keyframes heartbeat2 { 
    from { 
    transform: scale(1); 
    transform-origin: center center; 
    animation-timing-function: ease-out; 
    } 
    10% { 
    transform: scale(1.2); 
    animation-timing-function: ease-in; 
    } 
    15% { 
    transform: scale(1); 
    animation-timing-function: ease-out; 
    } 
    25% { 
    transform: scale(1.15); 
    animation-timing-function: ease-in; 
    } 
    35% { 
    transform: scale(1); 
    animation-timing-function: ease-out; 
    } 
} 

@keyframes heartbeat3 { 
    from { 
    transform: scale(1); 
    transform-origin: center center; 
    animation-timing-function: ease-out; 
    } 
    10% { 
    transform: scale(1.2); 
    animation-timing-function: ease-in; 
    } 
    15% { 
    transform: scale(1); 
    animation-timing-function: ease-out; 
    } 
    25% { 
    transform: scale(1.15); 
    animation-timing-function: ease-in; 
    } 
    35% { 
    transform: scale(1); 
    animation-timing-function: ease-out; 
    } 
} 
+1

の可能性のある重複した[複数の要素間でCSSアニメーションを同期する方法は?](http://stackoverflow.com/questions/4838972/how-to-sync-css-animations-across複数の要素) – gyre

+2

評価の種類ごとに3行、つまり1つの心臓、2つの心臓、3つの心臓を準備してみませんか?その後、チェックボックスを使用してそれらを切り替えることができます。 – Anson

+0

こんにちは@gyre、ちょうどあなたのリンクから "bouncywrap"テクニックを試みたが、心は今チェックされていなくてもぶつかっている。プラスのポジション:相対的なものは、心のアライメントと混乱しているようです。 – mattvent

答えて

1

あなたは3つの同一のアニメーションを設定し、入力のスタイルを設定することができますし、新しいラジオボタンが選択されたときに、古いアニメーションが停止し、新しいアニメーションが開始されるようなラベルが表示されます。そうすれば心は常に同期されます:

実施例:あなたが必要と出力を与えるために浪人の答え@修正

div { 
 
display: block; 
 
position: relative; 
 
width: 125px; 
 
height: 60px; 
 
box-shadow: 3px 3px 3px rgba(127, 127, 127, 0.4); 
 
} 
 

 
input { 
 
display: none; 
 
} 
 

 
label { 
 
position: absolute; 
 
display: inline-block; 
 
top: 0; 
 
font-size: 40px; 
 
line-height: 60px; 
 
color: rgba(255, 127, 127, 0.75); 
 
} 
 

 

 
label:hover, 
 
label:hover ~ label { 
 
color: rgba(255, 0, 0, 0.75); 
 
} 
 

 
.like1 { 
 
left: 10px; 
 
} 
 

 
.like2 { 
 
left: 50px; 
 
} 
 

 
.like3 { 
 
left: 90px; 
 
} 
 

 
#like1:checked ~ label { 
 
animation: heartbeat1 0.8s infinite; 
 
} 
 

 
#like2:checked ~ label { 
 
animation: heartbeat2 0.8s infinite; 
 
} 
 

 
#like3:checked ~ label { 
 
animation: heartbeat3 0.8s infinite; 
 
} 
 

 
@keyframes heartbeat1 { 
 
    0% { 
 
    color: rgba(255, 0, 0, 0.5); 
 
    } 
 

 
    50% { 
 
    color: rgba(255, 0, 0, 1); 
 
    } 
 
} 
 

 
@keyframes heartbeat2 { 
 
    0% { 
 
    color: rgba(255, 0, 0, 0.5); 
 
    } 
 

 
    50% { 
 
    color: rgba(255, 0, 0, 1); 
 
    } 
 
} 
 

 
@keyframes heartbeat3 { 
 
    0% { 
 
    color: rgba(255, 0, 0, 0.5); 
 
    } 
 

 
    50% { 
 
    color: rgba(255, 0, 0, 1); 
 
    } 
 
}
<div> 
 
<input type="radio" id="like3" name="likes" value="3" /> 
 
<label class="like3" for="like3">&#9829;</label> 
 

 
<input type="radio" id="like2" name="likes" value="2" /> 
 
<label class="like2" for="like2">&#9829;</label> 
 

 
<input type="radio" id="like1" name="likes" value="1" /> 
 
<label class="like1" for="like1">&#9829;</label> 
 
</div>

+1

ありがとうございます。あなたは私が欲しがたいいくつかの変更を加えたので、私が欠けていたものだけを残して元の投稿に追加しました。 – mattvent

+1

あなたは歓迎です@mattvent - 私はまさに同じマークアップとスタイリングを使用しませんでした。なぜなら、私はあなたに「あなたの宿題」をするのではなく、アプローチを示すために出発したからです。 :D – Rounin

+1

フェアなサウンド!ありがとうございます;) – mattvent

1

div { 
 
    display: block; 
 
    position: relative; 
 
    width: 125px; 
 
    height: 60px; 
 
    box-shadow: 3px 3px 3px rgba(127, 127, 127, 0.4); 
 
} 
 

 
input { 
 
    display: none; 
 
} 
 

 
label { 
 
    position: absolute; 
 
    display: inline-block; 
 
    top: 0; 
 
    font-size: 40px; 
 
    line-height: 60px; 
 
    color: rgba(255, 127, 127, 0.75); 
 
} 
 

 
label:hover, 
 
label:hover~label { 
 
    color: rgba(255, 0, 0, 0.75); 
 
} 
 

 
.like1 { 
 
    left: 10px; 
 
} 
 

 
.like2 { 
 
    left: 50px; 
 
} 
 

 
.like3 { 
 
    left: 90px; 
 
} 
 

 
#like1:checked~label { 
 
    animation: heartbeat1 0.8s infinite; 
 
} 
 

 
#like2:checked~label { 
 
    animation: heartbeat2 0.8s infinite; 
 
} 
 

 
#like3:checked~label { 
 
    animation: heartbeat3 0.8s infinite; 
 
} 
 

 
@keyframes heartbeat1 { 
 
    0% { 
 
    transform: scale(.75); 
 
    } 
 
    20% { 
 
    transform: scale(1); 
 
    } 
 
    40% { 
 
    transform: scale(.75); 
 
    } 
 
    60% { 
 
    transform: scale(1); 
 
    } 
 
    80% { 
 
    transform: scale(.75); 
 
    } 
 
    100% { 
 
    transform: scale(.75); 
 
    } 
 
} 
 

 
@keyframes heartbeat2 { 
 
    0% { 
 
    transform: scale(.75); 
 
    } 
 
    20% { 
 
    transform: scale(1); 
 
    } 
 
    40% { 
 
    transform: scale(.75); 
 
    } 
 
    60% { 
 
    transform: scale(1); 
 
    } 
 
    80% { 
 
    transform: scale(.75); 
 
    } 
 
    100% { 
 
    transform: scale(.75); 
 
    } 
 
} 
 

 
@keyframes heartbeat3 { 
 
    0% { 
 
    transform: scale(.75); 
 
    } 
 
    20% { 
 
    transform: scale(1); 
 
    } 
 
    40% { 
 
    transform: scale(.75); 
 
    } 
 
    60% { 
 
    transform: scale(1); 
 
    } 
 
    80% { 
 
    transform: scale(.75); 
 
    } 
 
    100% { 
 
    transform: scale(.75); 
 
    } 
 
}
<div> 
 
    <input type="radio" id="like3" name="likes" value="3" /> 
 
    <label class="like3" for="like3">&#9829;</label> 
 

 
    <input type="radio" id="like2" name="likes" value="2" /> 
 
    <label class="like2" for="like2">&#9829;</label> 
 

 
    <input type="radio" id="like1" name="likes" value="1" /> 
 
    <label class="like1" for="like1">&#9829;</label> 
 
</div>

+0

真剣に?あなたはこれがどんな意味での剽窃ではないと感じていますか? – Rounin

+0

私はそれをあなたに褒めてくれました。あなたは、ソリューションを提供していたはずのアニメーションを更新することを大歓迎します。 :) – CodeMonkey

関連する問題