2017-03-22 8 views
2

Ich bin auf der Suche nach der Keyframe-Animation von drei verschiedenen Elementen zu synchronisieren.css Synchronisieren Keyframe Animationen

Sie sind im Grunde drei Herzen und ich möchte, dass sie einer Herzschlaganimation folgen, wenn sie angeklickt werden.

Wenn zwei oder mehr angeklickt werden, möchte ich, dass sie synchron "schlagen".

können Sie einen JSbin überprüfen here

Was ich bisher habe, ist:

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

Was die gute Art und Weise synchron zu erreichen ist?

Danke

EDIT

Nach Rounin Antwort, hier ist die CSS aktualisiert, die die Arbeit erledigt wird:

#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

Mögliche Duplikat von [Wie CSS Animationen über mehrere Elemente synchronisieren?] (Http://stackoverflow.com/questions/4838972/how-to-sync-css-animations-across (multiple-elements) – gyre

+2

Warum machst du nicht 3 Zeilen für jede Art von Bewertung, zB 1 Herz, 2 Herzen und 3 Herzen? Dann können Sie sie mithilfe von Kontrollkästchen wechseln. – Anson

+0

Hi @gyre, habe gerade die "bouncywrap" -Technik von deinem Link ausprobiert, aber die Herzen stoßen jetzt auch, wenn sie nicht überprüft werden. Plus die Position: relative scheint mit der Alignemnent der Herzen zu vermasseln. – mattvent

Antwort

1

Sie einrichten können 3 identische Animationen und Ihre Eingaben stylen und Labels, so dass bei Auswahl eines neuen Optionsfelds die alte Animation stoppt und die neue beginnt. Auf diese Weise die Herzen immer synchron sein wird:

Arbeitsbeispiel:

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

Danke. Du hast einige Änderungen vorgenommen, die ich nicht wollte, also behielt ich nur das, was ich vermisste, und fügte es meinem ursprünglichen Beitrag hinzu. – mattvent

+1

Sie sind herzlich willkommen @matventvent - Ich habe nicht genau das gleiche Markup und Styling verwendet, weil ich Ihnen einen Ansatz gezeigt habe, anstatt "Ihre Hausaufgaben zu machen". : D – Rounin

+1

Klingt fair! Vielen Dank;) – mattvent

1

@ Rounin Antwort Geändert, um Ihnen die Ausgabe, die Sie benötigen.

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

Ernsthaft? Du empfindest das nicht als Plagiat? – Rounin

+0

Ich habe dir die Komplimente dafür gegeben. Sie können gerne die Animation aktualisieren, die Sie mit der Lösung bereitgestellt haben sollten. :) – CodeMonkey

Verwandte Themen