简体   繁体   中英

@keyframes animation (rotate) and :hover (scale)

I have a problem with combinig css animation based on @keyframes (rotate) with :hover effect (scale) - and need some help from somebody more experienced then me (i'm rookie - that's obvious).

I would like to create some kind of animated background, where:
1. without any interaction with user: small symbols/letters/icons (whatever) are moving (some of them spinning around, some moving in X or Y axis);
2: when hover: they are also growing (but still moving);
and finally:
3. There is no pause beetwen @keyframe animations.

Right now I have came up with this code https://jsfiddle.net/56q4b9fg/2/ The problem is that at :hover my basic animations starts from the beginning instead of continuing their track. How can I fix this? And how to resolve my third problem with pause after every sequence?

.pattern {
    height: 100vh;
    width: 100vw;
    margin: 0 auto;
    background-color: rgb(170, 57, 57);
    color: rgb(255,255,255);
}

.grid {
    display: grid;
    grid-template-columns: repeat(12, 1fr); 
    grid-template-rows: repeat(8, 1fr);
    justify-items: center;
    align-items: center;
    overflow: hidden;
}

.symbol-1 {
    grid-area: 2 / 2 / 2 / 2;
    justify-self: center;  
}

.symbol-2 {
    grid-area: 4 / 3 / 3 / 5;
    justify-self: center;
    align-self: start;
}

.downRight {
    animation: downRight 7s infinite;
}

.downRight:hover {
    animation: downRightAndScale 7s infinite;
}

.spin {
    animation: spin 7s infinite;
}

.spin:hover {
    animation: spinAndScale 7s infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

@keyframes downRight {
    0%, 100% { transform: translateX(0) translateY(0); }
    50% { transform: translateX(20px) translateY(20px); }
}

@keyframes spinAndScale {
    0% { transform: rotate(0deg) scale(1.0); }
    10% { transform: rotate(10deg) scale(1.1); }
    50% { transform: rotate(180deg) scale(1.5); }
    100% { transform: rotate(360deg) scale(1.0); }
}

@keyframes downRightAndScale {
    0%, 100% { transform: translateX(0) translateY(0) scale(1.0);}
    50% { transform: translateX(20px) translateY(20px) scale(1.5);}
}

The simplest solution would be to wrap the symbol-1 and symbol-2 in another element and transform this element when hovered. Also use a transition in order to animate it. Take a look at the snippet inserted below.

 .pattern { height: 100vh; width: 100vw; margin: 0 auto; background-color: rgb(170, 57, 57); color: rgb(255,255,255); } .grid { display: grid; grid-template-columns: repeat(12, 1fr); grid-template-rows: repeat(8, 1fr); justify-items: center; align-items: center; overflow: hidden; } .symbol { -webkit-transition: all 1000ms ease; -moz-transition: all 1000ms ease; -o-transition: all 1000ms ease; transition: all 1000ms ease; transform: scale(0.5); font-size: 2em } .symbol:hover { transform: scale(1); } .symbol-1 { grid-area: 2 / 2 / 2 / 2; justify-self: center; } .symbol-2 { grid-area: 4 / 3 / 3 / 5; justify-self: center; align-self: start; } .downRight { animation: downRight 7s infinite; } .spin { animation: spin 7s infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } @keyframes downRight { 0%, 100% { transform: translateX(0) translateY(0); } 50% { transform: translateX(20px) translateY(20px); } } 
 <body> <div class="pattern grid"> <!-- Wrap the symbols in a div with class "symbol" that scales on hover --> <div class="symbol symbol-1"><div class="downRight"> 1 </div></div> <div class="symbol symbol-2"><div class="spin"> 2 </div></div> </div> </body> 

I trust this solves your problem :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM