\r\n \r\n \r\n \r\n \r\n \r\n\r\n```\r\n\r\ncssコード\r\n```ここに言語を入力\r\n#test1{\r\n position: absolute;\r\n left: 0;\r\n right: 0;\r\n margin: auto;\r\n top:6.38vw;\r\n width:30.27vw;\r\n height:9.93vw;\r\n z-index:4;\r\n}\r\n#test2{\r\n position: absolute;\r\n left: 0;\r\n right: 0;\r\n margin: auto;\r\n top:19.65vw;\r\n width:59.2vw;\r\n height:10.77vw;\r\n z-index:3;\r\n}\r\n#test3{\r\n position: absolute;\r\n left: 0;\r\n right: 0;\r\n margin: auto;\r\n top:32.3vw;\r\n width:57.71vw;\r\n height:7.4vw;\r\n z-index:2;\r\n}\r\n#test4{\r\n position: absolute;\r\n left: 0;\r\n right: 1vw;\r\n margin: auto;\r\n top:43.54vw;\r\n width:50.21vw;\r\n height:6.04vw;\r\n z-index:1;\r\n}\r\n```\r\n\r\njsコード\r\n```ここに言語を入力\r\n $(function(){\r\n function animation(){\r\n $('#test4').each(function(){\r\n //ターゲットの位置を取得\r\n var target = $('#test4').offset().top;\r\n //スクロール量を取得\r\n var scroll = $(window).scrollTop();\r\n //ウィンドウの高さを取得\r\n var windowHeight = $(window).height();\r\n //ターゲットまでスクロールするとフェードインする\r\n if (scroll > target - windowHeight){\r\n $('#test4').animate({'top':'32.3vw'},1000);\r\n $('#test2').animate({'top':'32.3vw'},1000);\r\n setTimeout(function(){\r\n $('#test1').animate({'top':'32.3vw'},1000);},1000);\r\n \r\n \r\n }\r\n \r\n });\r\n }\r\n animation();\r\n $(window).scroll(function (){\r\n animation();\r\n });\r\n});\r\n```\r\n\r\n試したこと\r\n1.if文のあとにelse文を書いて、それぞれを、もとのtopの位置に戻すことをしたのですが、動いてくれません。\r\nエラー文は特に出ておりませんでした。\r\n```ここに言語を入力\r\nelse{\r\n $('#test4').animate({'top':'43.54vw'},1000);\r\n $('#test2').animate({'top':'19.65vw'},1000);\r\n}\r\n```\r\n2.elseにanimateではなくcssを使ったのですがこれもうまくいきませんでした\r\n```ここに言語を入力\r\nelse{\r\n$('#test4').css('top','43.54vw');\r\n $('#test2').css('top','19.65vw');\r\n}\r\n```\r\n\r\nif文の中は動作するのですが一回実装されるのみでそのあとはいくらスクロールしても移動後の位置に止まったままです.\r\n\r\nどなたかわかる方おりましたら、ご回答宜しくお願い致します。","answerCount":1,"upvoteCount":0,"datePublished":"2020-06-28T09:26:45.223Z","dateModified":"2020-06-29T01:27:12.850Z","acceptedAnswer":{"@type":"Answer","text":"補足依頼をしたのですが、応答がないようなので、仮定が正しい前提で回答します。\r\n\r\n---\r\n\r\nご提示の「試したこと1」の通りで一応動作するのですが、アニメーションのキューが渋滞しているので、実際に動き出すまでに数十秒かかります。\r\n\r\nなので、キューをクリアするようにすれば、まあ、なんとか動きます。\r\n\r\n```js\r\n if (scroll > target - windowHeight){\r\n $('#test4').stop().animate({'top':'32.3vw'},1000);\r\n $('#test2').stop().animate({'top':'32.3vw'},1000);\r\n setTimeout(function(){\r\n $('#test1').stop().animate({'top':'32.3vw'},1000);},1000);\r\n \r\n \r\n }else{\r\n $('#test4').stop().animate({'top':'43.54vw'},1000);\r\n $('#test2').stop().animate({'top':'19.65vw'},1000);\r\n }\r\n```\r\n\r\nただ、イベントの拾い方を少し工夫した方がいいと思いますよ。\r\n(もう、見ていないのかもしれないけど)","dateModified":"2020-07-02T04:36:51.476Z","datePublished":"2020-07-02T04:36:51.476Z","upvoteCount":1,"url":"https://teratail.com/questions/273527#reply-391460"},"suggestedAnswer":[],"breadcrumb":{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://teratail.com","name":"トップ"}},{"@type":"ListItem","position":2,"item":{"@id":"https://teratail.com/tags/jQuery","name":"jQueryに関する質問"}},{"@type":"ListItem","position":3,"item":{"@id":"https://teratail.com/questions/273527","name":"jQueryで移動させた要素をもとに戻したい"}}]}}}
質問するログイン新規登録

Q&A

解決済

1回答

4561閲覧

jQueryで移動させた要素をもとに戻したい

kenshou

総合スコア9

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

HTML

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

CSS

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

0グッド

0クリップ

投稿2020/06/28 09:26

編集2020/06/29 01:27

0

0

jQueryのanimateメソッドを利用して、要素を動かそうしております。

やり方としては、指定した要素の位置をoffsetで取得し、スクロールしてその位置まで来たらモノが動き出すということをしております。

試したところ、実際にその位置までスクロールしたときに要素は動くのですが、、
希望としてはその位置を超えてないときはまた元の位置に戻しときたいです。

つまりその位置にスクロールで到達するたびに、アニメーションが実装されるようにしたいのです

<!doctype html> <html lang="jp"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous"> <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+JP::wght@400;500;700&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+JP:wght@400;500;600;700&display=swap" rel="stylesheet"> <link rel="stylesheet" href="common/style.css"> <title>test</title> </head> <body> ここから下に要素があります↓      <img id="test1" src="common/img/test1.png"> <img id="test2" src="common/img/test2.png"> <img id="test3" src="common/img/test3.png"> <img id="test4" src="common/img/test4.png"> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> <script src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js'></script> <script src="common/script.js"></script> </body> </html>

cssコード

#test1{ position: absolute; left: 0; right: 0; margin: auto; top:6.38vw; width:30.27vw; height:9.93vw; z-index:4; } #test2{ position: absolute; left: 0; right: 0; margin: auto; top:19.65vw; width:59.2vw; height:10.77vw; z-index:3; } #test3{ position: absolute; left: 0; right: 0; margin: auto; top:32.3vw; width:57.71vw; height:7.4vw; z-index:2; } #test4{ position: absolute; left: 0; right: 1vw; margin: auto; top:43.54vw; width:50.21vw; height:6.04vw; z-index:1; }

jsコード

$(function(){ function animation(){ $('#test4').each(function(){ //ターゲットの位置を取得 var target = $('#test4').offset().top; //スクロール量を取得 var scroll = $(window).scrollTop(); //ウィンドウの高さを取得 var windowHeight = $(window).height(); //ターゲットまでスクロールするとフェードインする if (scroll > target - windowHeight){ $('#test4').animate({'top':'32.3vw'},1000); $('#test2').animate({'top':'32.3vw'},1000); setTimeout(function(){ $('#test1').animate({'top':'32.3vw'},1000);},1000); } }); } animation(); $(window).scroll(function (){ animation(); }); });

試したこと
1.if文のあとにelse文を書いて、それぞれを、もとのtopの位置に戻すことをしたのですが、動いてくれません。
エラー文は特に出ておりませんでした。

else{ $('#test4').animate({'top':'43.54vw'},1000); $('#test2').animate({'top':'19.65vw'},1000); }

2.elseにanimateではなくcssを使ったのですがこれもうまくいきませんでした

else{ $('#test4').css('top','43.54vw'); $('#test2').css('top','19.65vw'); }

if文の中は動作するのですが一回実装されるのみでそのあとはいくらスクロールしても移動後の位置に止まったままです.

どなたかわかる方おりましたら、ご回答宜しくお願い致します。

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

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

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

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

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

Lhankor_Mhy

2020/06/30 05:13

jQueryが読み込まれていないようです。
Lhankor_Mhy

2020/06/30 05:26

#test4が絶対配置されていますので、常に画面内にあると思います。 なので、 scroll > target - windowHeight が成立しない、ということはないはずです。 #test4の外側にrelativeにしている要素はありませんか?
guest

回答1

0

ベストアンサー

補足依頼をしたのですが、応答がないようなので、仮定が正しい前提で回答します。


ご提示の「試したこと1」の通りで一応動作するのですが、アニメーションのキューが渋滞しているので、実際に動き出すまでに数十秒かかります。

なので、キューをクリアするようにすれば、まあ、なんとか動きます。

js

1 if (scroll > target - windowHeight){ 2 $('#test4').stop().animate({'top':'32.3vw'},1000); 3 $('#test2').stop().animate({'top':'32.3vw'},1000); 4 setTimeout(function(){ 5 $('#test1').stop().animate({'top':'32.3vw'},1000);},1000); 6 7 8 }else{ 9 $('#test4').stop().animate({'top':'43.54vw'},1000); 10 $('#test2').stop().animate({'top':'19.65vw'},1000); 11 }

ただ、イベントの拾い方を少し工夫した方がいいと思いますよ。
(もう、見ていないのかもしれないけど)

投稿2020/07/02 04:36

Lhankor_Mhy

総合スコア37634

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

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

kenshou

2020/08/06 04:37

返信が遅くなってしまい大変申し訳ございません! あれから、代案でaddClass,removeClassを実装することで自己解決することができました! Lhankor_Mhyさんのやり方もあるのですね! またweb制作を請け負う時に是非参考にさせていただきます! 本当にありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.29%

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

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

質問する

関連した質問