15
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptでCSSアニメーションの@keyframesを使う

Last updated at Posted at 2015-12-05

JavaScriptから@keyframesを使うには

element.styleなどでは指定できないので、
head内にstyleタグを作成し、その中に@keyframesを追加します。

たとえばfadein/fadeoutを作成する



(function() {
	var css, rules, fadein, fadeout;
  
	// styleタグを作成
	css = document.createElement('style');
	css.media = 'screen';
	css.type = 'text/css';
  
	// フェードイン
	fadein = '@keyframes fade-in{' + [
		'0% {opacity: 0}',
		'100% {opacity: 1.0}'
	].join(' ') + '}';
  
	// フェードアウト
	fadeout = '@keyframes fade-out{' + [
 		'0% {opacity: 1.0}',
		'100% {opacity: 0}'
	].join('') + '}';
  
	// ルールをstyleタグに追加
	rules = document.createTextNode([fadein,fadeout].join('\n'));
	css.appendChild(rules);
	
	// head内に作成
	document.getElementsByTagName('head')[0].appendChild(css);
}());
15
13
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?