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

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

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

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

Q&A

0回答

1007閲覧

jQueryを使用してのフリック操作 2枚目以降、うまく反応しない

yae

総合スコア0

jQuery

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

0グッド

0クリップ

投稿2021/07/17 04:18

前提・実現したいこと

jQueryを使用してフリック操作を実装したいです。
指を動かしている間は .swipe-photo-container が移動し、離したときに一番近い要素の位置までアニメーションします。

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

左右に50px以上フリックしないと画像が動かないように作成しました。 一枚目の画像はうまくいくのですが、二枚目以降はうまく反応せずクリックするだけでも 画像が流れて行っていまいます。

該当のソースコード

<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="format-detection" content="telephone=no"> <title></title> <link rel="stylesheet" href="css/reset.css"> <link rel="stylesheet" href="css/base.css"> <link rel="stylesheet" href="css/style.css"> </head> <body> <div class="swipe-photo"> <ul class="swipe-photo-container clearfix"> <li class="swipe-photo-item"><img src="img/0.jpg" class="swipe-photo-thumbnail"></li> <li class="swipe-photo-item"><img src="img/1.jpg" class="swipe-photo-thumbnail"></li> <li class="swipe-photo-item"><img src="img/2.jpg" class="swipe-photo-thumbnail"></li> <li class="swipe-photo-item"><img src="img/3.jpg" class="swipe-photo-thumbnail"></li> <li class="swipe-photo-item"><img src="img/4.jpg" class="swipe-photo-thumbnail"></li> <li class="swipe-photo-item"><img src="img/5.jpg" class="swipe-photo-thumbnail"></li> </ul> </div> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript"> "use strict"; $(function() { var $swipe = $(".swipe-photo-container"), $swipeList = $swipe.find(".swipe-photo-item"), $swipeImg = $swipe.find(".swipe-photo-thumbnail"), swipeImgLength = $swipeImg.length, swipeImgWidth = $swipeImg.width(), direction = '', swipe = 0, undefined = { x:undefined, thisX:undefined, startX:undefined, moveX:undefined }; $swipe.width(swipeImgLength * swipeImgWidth); $swipeList.on( { touchstart: function(e) { undefined.$thisX = parseInt($swipe.css("left")); undefined.$startX = e.originalEvent.changedTouches[0].pageX - undefined.$thisX; }, touchmove: function(e) { undefined.$moveX = e.originalEvent.changedTouches[0].pageX - undefined.$thisX; directionImg(); }, touchend: function() { swipeImg(direction); } }); function directionImg() { if (undefined.$startX - undefined.$moveX > 50) { //50px以上右にフリックした場合 direction = "right"; console.log(direction); } else if (undefined.$startX - undefined.$moveX < -50) { //50px以上左にフリックした場合 direction = "left"; console.log(direction); } }; function swipeImg(direction) { if (direction == "right") { swipe++; if(swipe < swipeImgLength) { $swipe.stop(true, false).animate( { left: swipe * -swipeImgWidth }, 500); } else { swipe = 0; $swipe.animate( { left: 0 }, 500); } } else if (direction == "left") { swipe--; if (swipe < 0) { swipe = swipeImgLength - 1; $swipe.animate( { left: swipe * -swipeImgWidth }, 500); } else { $swipe.stop(true, false).animate( { left: swipe * -swipeImgWidth }, 500); } } } }); </script> </body> </html>

試したこと

console.logにて50px以上フリックしないとdirectionに挿入されないことは確認できているのですが、
画像は流れて行っていまいます。

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

JavaScrpt,jQueryでお願いできましたら幸いです。
宜しくお願い致します。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問