質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

Q&A

解決済

2回答

709閲覧

css animationが途中から適応されなくなる

mitosa

総合スコア7

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

0グッド

0クリップ

投稿2021/05/07 04:12

前提・実現したいこと

HTML,CSS,Javascriptでwebページを作成しています。
仕様としてはスクロールするたびに表示する要素を変えるというものです。
keyframesで透明度を1→0にしてふんわり表示させる演出をしています。

発生している問題・エラーメッセージ

あいうえお→かきくけこ→さしすせそ→かきくけこ→さしすせそ とスクロールすると、さしすせその時点でanimationが効かないです。

該当のソースコード

<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <!-- <link rel="stylesheet" href="./css/style.css"> --> </head> <body> <div id="wrapper"> <div class="box show" id="box-1"><span>あいうえお</span></div> <div class="box fade" id="box-2"><span>かきくけこ</span></div> <div class="box fade" id="box-3"><span>さしすせそ</span></div> <div class="box fade" id="box-4"><span>たちつてと</span></div> <div class="box fade" id="box-5"><span>なにぬねの</span></div> </div> </body> </html> <!-- <script src="./js/scrollFunc.js"></script> --> <style> * { padding: 0; margin: 0; box-sizing: border-box; } .box { width: 100vw; height: 100vh; position: relative; } span { position: absolute; font-size: 28px; top: 50%; left: 50%; transform: translateY(-50%) translateX(-50%); } #wrapper { width: 100vw; height: 100vh; overflow: hidden; } #box-1 { background-color: aliceblue; } #box-2 { background-color: antiquewhite; } #box-3 { background-color: aqua; } #box-4 { background-color: aquamarine; } #box-5 { background-color: indianred; } .fade { display: none; } .fade span { animation: show 1s linear 0s; animation-fill-mode: forwards; animation-direction: reverse } .show { display: block; } .show span { animation: show 1s linear 0s; animation-fill-mode: forwards; } @keyframes show { from { opacity: 0; } to { opacity: 1; } } </style> <script> var vox1 = document.getElementById('box-1'); var vox2 = document.getElementById('box-2'); var vox3 = document.getElementById('box-3'); var vox4 = document.getElementById('box-4'); var vox5 = document.getElementById('box-5'); //スクロール上下判別 var count = 0; const wheel = ((flag = false, timerId = null) => event => { clearTimeout(timerId); timerId = setTimeout(() => flag = false, 100); // 1秒空いたらリセット if (flag) return; if (event.wheelDeltaY < 0 && count < 4) { count = count + 1; } else if (event.wheelDeltaY > 0 && count > 0) { count = count - 1; } flag = true; if (count == 0) { vox1.classList.remove('fade'); vox1.classList.add('show'); vox2.classList.remove('show'); vox2.classList.add('fade'); } else if (count == 1) { vox2.classList.remove('fade'); vox2.classList.add('show'); vox1.classList.remove('show'); vox1.classList.add('fade'); } else if (count == 2) { vox3.classList.remove('fade'); vox3.classList.add('show'); vox2.classList.remove('show'); vox2.classList.add('fade'); } else if (count == 3) { vox4.classList.remove('fade'); vox4.classList.add('show'); vox3.classList.remove('show'); vox3.classList.add('fade'); } else if (count == 4) { vox5.classList.remove('fade'); vox5.classList.add('show'); vox4.classList.remove('show'); vox4.classList.add('fade'); } })(); window.addEventListener("wheel", wheel); </script>

試したこと

上記

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

なかなか再現できませんでしたが、一度なにぬねのまでスクロールダウンして、その後、あいうえおまで戻ると、さしすせそでフェードが機能しない状態になりますね。

これはなにぬねのからあいうえおに移動する際に、.showがremoveされていないことが原因のようです。
前の方のように効率の良い書き方はありますので、とりあえず使えればよいという場合はそちらを利用したほうが良いと思います。勉強的な意味合いと考えて、現状を変更する対策としては以下。
1 .fadeを消さないこと。.fadeはフェードアニメーションするためのclassなので消さないほうが間違いが少ないです。表示非表示は.showのみで制御したほうがシンプルです。
2. スクロールアップ時に前のDOMからも.showを取り除く。

修正コードは以下です。

html

1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <title>Document</title> 8 <!-- <link rel="stylesheet" href="./css/style.css"> --> 9</head> 10<body> 11<div id="wrapper"> 12 <div class="box fade show" id="box-1"><span>あいうえお</span></div> 13 <div class="box fade" id="box-2"><span>かきくけこ</span></div> 14 <div class="box fade" id="box-3"><span>さしすせそ</span></div> 15 <div class="box fade" id="box-4"><span>たちつてと</span></div> 16 <div class="box fade" id="box-5"><span>なにぬねの</span></div> 17</div> 18</body> 19 20</html> 21<!-- <script src="./js/scrollFunc.js"></script> --> 22 23<style> 24 * { 25 padding: 0; 26 margin: 0; 27 box-sizing: border-box; 28 } 29 30 .box { 31 width: 100vw; 32 height: 100vh; 33 position: relative; 34 } 35 36 span { 37 position: absolute; 38 font-size: 28px; 39 top: 50%; 40 left: 50%; 41 transform: translateY(-50%) translateX(-50%); 42 } 43 44 #wrapper { 45 width: 100vw; 46 height: 100vh; 47 overflow: hidden; 48 } 49 50 #box-1 { 51 background-color: aliceblue; 52 } 53 54 #box-2 { 55 background-color: antiquewhite; 56 } 57 58 #box-3 { 59 background-color: aqua; 60 } 61 62 #box-4 { 63 background-color: aquamarine; 64 } 65 66 #box-5 { 67 background-color: indianred; 68 } 69 70 .fade { 71 display: none; 72 } 73 74 .fade span { 75 animation: show 1s linear 0s; 76 animation-fill-mode: forwards; 77 animation-direction: reverse 78 } 79 80 .show { 81 display: block; 82 } 83 84 .show span { 85 animation: show 1s linear 0s; 86 animation-fill-mode: forwards; 87 } 88 89 @keyframes show { 90 from { 91 opacity: 0; 92 } 93 94 to { 95 opacity: 1; 96 } 97 } 98</style> 99 100<script> 101 var vox1 = document.getElementById('box-1'); 102 var vox2 = document.getElementById('box-2'); 103 var vox3 = document.getElementById('box-3'); 104 var vox4 = document.getElementById('box-4'); 105 var vox5 = document.getElementById('box-5'); 106 107 //スクロール上下判別 108 var count = 0; 109 const wheel = ((flag = false, timerId = null) => event => { 110 clearTimeout(timerId); 111 timerId = setTimeout(() => flag = false, 100); // 1秒空いたらリセット 112 if (flag) return; 113 114 if (event.wheelDeltaY < 0 && count < 4) { 115 count = count + 1; 116 } else if (event.wheelDeltaY > 0 && count > 0) { 117 count = count - 1; 118 } 119 flag = true; 120 if (count == 0) { 121 vox1.classList.add('show'); 122 vox2.classList.remove('show'); 123 } else if (count == 1) { 124 vox2.classList.add('show'); 125 vox3.classList.remove('show'); 126 vox1.classList.remove('show'); 127 } else if (count == 2) { 128 vox3.classList.add('show'); 129 vox4.classList.remove('show'); 130 vox2.classList.remove('show'); 131 } else if (count == 3) { 132 vox4.classList.add('show'); 133 vox5.classList.remove('show'); 134 vox3.classList.remove('show'); 135 } else if (count == 4) { 136 vox5.classList.add('show'); 137 vox4.classList.remove('show'); 138 } 139 })(); 140 window.addEventListener("wheel", wheel); 141</script>

投稿2021/05/07 16:33

gamips

総合スコア60

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

元コードのJavascriptがよく解らなかったので、自作。
html css も少し変えました。
参考までにどうぞ

html + css + Javascript

<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { padding: 0; margin: 0; box-sizing: border-box; } .box { width: 100vw; height: 100vh; position: relative; display:none; } span { position: absolute; font-size: 28px; top: 50%; left: 50%; transform: translateY(-50%) translateX(-50%); } #wrapper { width: 100vw; height: 100vh; overflow: hidden; } #box-1 { background-color: aliceblue; } #box-2 { background-color: antiquewhite; } #box-3 { background-color: aqua; } #box-4 { background-color: aquamarine; } #box-5 { background-color: indianred; } .fade span { animation: show 1s linear 0s; animation-fill-mode: forwards; animation-direction: reverse } .show { display: block; } .show span { animation: show 1s linear 0s; animation-fill-mode: forwards; } @keyframes show { from { opacity: 0; } to { opacity: 1; } } </style> </head> <body> <div id="wrapper"> <div class="box show" id="box-1"><span>あいうえお</span></div> <div class="box" id="box-2"><span>かきくけこ</span></div> <div class="box" id="box-3"><span>さしすせそ</span></div> <div class="box" id="box-4"><span>たちつてと</span></div> <div class="box" id="box-5"><span>なにぬねの</span></div> </div> <script> const box = document.getElementsByClassName("box"); let timer = null; const boxLen = box.length; window.addEventListener("wheel",(e)=>{ clearTimeout(timer); timer = setTimeout( () => { if (e.deltaY > 0) changeBoxUp(); if (e.deltaY < 0) changeBoxDown(); }, 300); }); const changeBoxUp = () => { for(let i = 0; i < boxLen; i++){ if(i == boxLen - 1) { box[i].classList.remove("show"); box[0].classList.add("show") } else if(box[i].classList.contains("show")) { box[i].classList.remove("show"); box[i + 1].classList.add("show"); break; } } } const changeBoxDown = () => { for(let i = 0; i < boxLen; i++){ if(i == 0 && box[i].classList.contains("show")) { box[0].classList.remove("show"); box[boxLen - 1].classList.add("show"); break; } else if(box[i].classList.contains("show")){ box[i].classList.remove("show"); box[i - 1].classList.add("show"); break; } } } </script> </body> </html>

投稿2021/05/07 11:04

編集2021/05/07 14:09
Jon_do

総合スコア1373

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問