性格悪い人が優勝

jqueryのhideしてfadeInするソースを
jquery抜きで実現したいと考えています。

<style>
#display {
color: white ;
background-color: black ;
}
</style>

<button type="button" id="button">button!</button>
<div id="display">display</div>

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></ …
<script>
document.getElementById("button").onclick = function() {
$('#display').hide().fadeIn(1000);
};
</script>

ボタンを押すと消えてそのあとふわっとしてもとに戻るやつです。

jquery抜きのJavaScript
もしくは
CSS
で書くにはどのようにすればよいでしょうか?

A 回答 (2件)

<style>


#display {
color: white ;
background-color: black ;
}
.fadein {
animation : fadeInAnimate 1s;
animation-fill-mode: both;
}
@keyframes fadeInAnimate {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
</style>

<button type="button" id="button">button!</button>
<div id="display">display</div>

<script>
var elm = document.getElementById('display');
elm.addEventListener('webkitAnimationEnd', function(){
elm.classList.remove('fadein');
});

document.getElementById("button").onclick = function() {
elm.classList.add('fadein');
};
</script>
    • good
    • 0
この回答へのお礼

回答ありがとうございました。

うまくばっちり動きます。

お礼日時:2019/08/13 11:06

こんにちは



方法はいろいろあると思いますので、単純な一例です。
アニメーションそのものはCSSで行い、ボタンクリックに応じてアニメーションのスタイルを与えるスイッチはスクリプトから行っています。

<!DOCTYPE HTML>
<html lang="ja">
<head><title>Sample</title>
<style type="text/css">
#display {
color: white ;
background-color: black ;
animation-duration:1s;
}

@keyframes fadeIn{
from { opacity:0; }
to { opacity:1; }
}
</style>
</head>
<body>

<button type="button" id="button">button!</button>
<div id="display">display</div>


<script type="text/javascript">
document.getElementById("button").addEventListener("click",function(){
let s = document.getElementById("display").style;
s.animationName = "";
setTimeout(function(){ s.animationName = "fadeIn"; }, 5);
});
</script>
</body>
</html>
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!