2015-09-26 74 views
0

我正在製作具有動畫的懸停按鈕。下面的代碼:動畫播放狀態重置或恢復

CodePen

HTML

<a href="" class="more-link"><span>Hover me &rarr;</span></a> 

CSS

.more-link { 
    display: inline-block; 
    min-height: 2.4em; 
    line-height: 2.4; 
    color: #000; 
    text-decoration: none; 
    text-transform: uppercase; 
    font-weight: bold; 
    position: relative; 
    min-width: 8em; 
    text-align: center; 
} 

.more-link span { 
    display: block; 
    width: 100%; 
    height: 100%; 
    position: relative; 
    box-sizing: border-box; 
    padding: 0 1em; 
} 

@keyframes tl { 
    from { 
     width: 0%; 
    } 
    to { 
     width: 100%; 
    } 
} 

@keyframes tr { 
    from { 
     height: 0%; 
    } 
    to { 
     height: 100%; 
    } 
} 

.more-link:before { 
    content: ' '; 
    display: block; 
    position: absolute; 
    top: 0; 
    left: 0; 
    width: 0%; 
    height: 1px; 
    background: #000; 
    animation: tl 400ms ease-in both; 
    animation-play-state: paused; 
} 

.more-link:hover:before { 
    animation-play-state: running; 
} 

.more-link:after { 
    content: ' '; 
    display: block; 
    position: absolute; 
    top: 0; 
    right: 0; 
    width: 1px; 
    height: 0%; 
    background: #000; 
    animation: tr 400ms ease-in 400ms both; 
    animation-play-state: paused; 
} 

.more-link:hover:after { 
    animation-play-state: running; 
} 

.more-link span:before { 
    content: ' '; 
    display: block; 
    position: absolute; 
    bottom: 0; 
    right: 0; 
    width: 0%; 
    height: 1px; 
    background: #000; 
    animation: tl 400ms ease-in 800ms both; 
    animation-play-state: paused; 
} 

.more-link:hover span:before { 
    animation-play-state: running; 
} 

.more-link span:after { 
    content: ' '; 
    display: block; 
    position: absolute; 
    bottom: 0; 
    left: 0; 
    width: 1px; 
    height: 0%; 
    background: #000; 
    animation: tr 400ms ease-in 1200ms both; 
    animation-play-state: paused; 
} 

.more-link:hover span:after { 
    animation-play-state: running; 
} 

正如你所看到的,我已經設置了animation-play-state: paused任何時候我徘徊了按鈕,但我真正需要的是一個「簡歷」或「重置」的動畫時,我徘徊,我在這裏看一些文檔https://developer.mozilla.org/en-US/docs/Web/CSS/animation-play-state但是沒有這個impl ementation。我的情況有什麼解決方法?

回答

1

爲什麼不用Javascript使用事件監聽器?

喜歡的東西:

(element).addEventListener("mouseover", function(){ 
(Add your animation name and info here that executes ONLY when hovered over.) 
}); 

那麼做到這一點:

(element).addEventListener("mouseout", function(){ 
(Add your animation name and info here that executes ONLY when the mouse leaves the div.) 
});