2017-03-22 137 views
2

我正在尋找同步三個不同元素的關鍵幀動畫。css同步關鍵幀動畫

它們基本上是三顆心,我希望它們在點擊時遵循心跳動畫。

當兩個或更多的點擊時,我希望他們「同步」擊敗。

可以檢查JSbin here

我至今是:

.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>

什麼是實現同步的好辦法嗎?

謝謝

編輯

繼Rounin的答案,這裏是更新後的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個相同的動畫和風格你的投入和標籤,以便當選擇一個新的單選按鈕時,舊的動畫停止並且新的動畫開始。這樣,心中始終處於同步:

工作實例:改良@ Rounin的回答

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