前提・実現したいこと
パソコンでは問題なく機能しているslideToggleをスマホでもうまく機能させたい。
発生している問題
要素をslideToggleで開閉させています。開閉させるボタンはクリックすると「read more」から「close」に切り替えるようにしています。
パソコンでは問題なく機能しています。スマホ(Android)では開閉ボタンをクリックして開くところまで良いのですが、開いた状態で画面を上下スライドさせると勝手に閉じてしまいます。
ボタンをクリックしていない状態で閉じるので、開閉ボタンも本来「read more」と表示されるはずが「close」のままになってしまいます。
スマホでスライドしても開いている要素を勝手に閉じないようにしたいこと、
開閉ボタンを押す、または別ページに移ることで閉じるようにしたいです。
追記
・Androidバージョン7.0 機種HUAWEI
・ブラウザchrome
・iosでは確認はできていません。
・loadイベントだけでresizeイベントは行っていない。
該当のソースコード
下のコードにあるshop_boxが7つある状態です。
html
1<div id="shop_main"> 2 <div class="shop_main_inner clearfix"> 3 <div class="shop_box"> 4 <a href="#" target="_blank"> 5 <div class="shop_imag"> 6 <img src="/images/shop01.jpg" alt=""> 7 </div> 8 <div class="shopHover-box"> 9 <div class="shopHover-tet">詳しく見る</div> 10 </div> 11 </a> 12 <div class="shop_name"> 13 <h2 class="shop_name_title"> 14 <a href="#" target="_blank">お店01</a> 15 </h2> 16 </div> 17 </div> 18 </div> 19</div>
css
1#shop_main { 2 width: 100%; 3 padding: 50px 0 20px 0; } 4 5.shop_main_inner { 6 width: 75%; 7 margin: 0 auto; } 8 9.shop_box { 10 float: left; 11 width: -webkit-calc(100% / 3 - 28px); 12 width: -moz-calc(100% / 3 - 28px); 13 width: -o-calc(100% / 3 - 28px); 14 width: calc(100% / 3 - 28px); 15 margin: 0 0 30px 42px; } 16 17.shopLink { 18 display: block; 19 width: 100%; 20 position: relative; } 21 22.shop_imag { 23 width: 100%; 24 height: auto; } 25 .shop_imag img { 26 width: 100%; 27 display: block; } 28 29.shopHover-box { 30 position: absolute; 31 width: 100%; 32 height: 100%; 33 top: 0; 34 left: 0; 35 opacity: 0; 36 background: rgba(61, 61, 61, 0.8); 37 -webkit-transition: all 0.3s ease; 38 transition: all 0.3s ease; } 39 40.shopHover-tet { 41 font-size: 1.4em; 42 color: white; 43 position: absolute; 44 top: 50%; 45 left: 50%; 46 width: 120px; 47 height: 36px; 48 margin: -18px 0 0 -60px; 49 border: solid 2px white; 50 text-align: center; 51 z-index: 1; 52 line-height: 35px; } 53 54.shop_name { 55 padding-top: 17px; } 56 .shop_name .shop_name_title { 57 font-size: 1.5em; 58 text-align: center; } 59 .shop_name .shop_name_title a { 60 text-decoration: underline; } 61 62#shop_button { 63 width: 100%; 64 text-align: center; 65 padding-top: 20px; 66 cursor: pointer; } 67 #shop_button span { 68 display: inline-block; 69 font-size: 1.5em; 70 padding: 8px 0; 71 border: solid 1px black; 72 box-sizing: border-box; 73 width: 13%; 74 -webkit-transition: all 0.3s ease; 75 transition: all 0.3s ease; } 76 #shop_button span:hover { 77 background: black; 78 letter-spacing: 2px; 79 color: white; } 80 81@media screen and (max-width: 768px) { 82 83 #shop_main { 84 padding: 35px 0 20px 0; } 85 86 .shop_main_inner { 87 width: 85%; } 88 89 .shop_box { 90 margin: 0 0 30px 10px; 91 width: -webkit-calc(100% / 2 - 5px); 92 width: -moz-calc(100% / 2 - 5px); 93 width: -o-calc(100% / 2 - 5px); 94 width: calc(100% / 2 - 5px); } 95 .shop_box:nth-child(2n-1) { 96 margin-left: 0; } 97 98 #shop_button span { 99 width: 49%; } 100
javascript
1 2//jQueryバージョン1.11.1 3 4var iwW = $(window).innerWidth(); 5//お店の数によってボタン表示・非表示 6function Shoplist(){ 7 if (iwW > 768) { 8 // PCの処理 9 $("#shop_main").each(function () {//.shop_boxの数をカウント 10 var num = $(this).find('.shop_box').length; 11 if(num > 3){//.shop_boxが3件よりも多かったら 12 $('.shop_box').eq(2).show(); // 追加。 13 $('.shop_box:nth-child(n + 4)').hide(); 14 $(this).find('#shop_button').css('display','block'); 15 } 16 else { 17 $(this).find('#shop_button').css('display','none'); 18 19 } 20 }); 21 } 22 else { 23 // SPの処理 24 $("#shop_main").each(function () {//.shop_boxの数をカウント 25 var num = $(this).find('.shop_box').length; 26 if(num > 2){//.shop_boxが2件よりも多かったら 27 $('.shop_box').eq(1).show(); // 追加。 28 $('.shop_box:nth-child(n + 3)').hide(); 29 $(this).find('#shop_button').css('display','block'); 30 } 31 else { 32 $(this).find('#shop_button').css('display','none'); 33 } 34 }); 35 36 } 37} 38// 開閉の処理 39function Shoplistdoor(){ 40 $('#shop_button').on('click','span',function(){ 41 if (iwW > 768) { 42 $('.shop_box:nth-child(n + 4)').slideToggle(); 43 } 44 else { 45 $('.shop_box:nth-child(n + 3)').slideToggle(); 46 } 47 var self = $(this); 48 self.fadeOut("linear", function(){ 49 if (self.data('opened')) { 50 self.text('read more'); 51 self.data('opened', false); 52 } else { 53 self.text('Close'); 54 self.data('opened', true); 55 } 56 self.fadeIn("linear"); 57 }); 58 }); 59} 60 61$(window).on('load resize',Shoplist); 62$(window).on('load',Shoplistdoor); 63

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/14 16:49
2018/06/15 17:09
2018/06/16 02:27