基本的な質問ですみませんが
ご教授いただけますと幸いです。
実現したいこと
- ページを開いたらモーダル表示
- モーダルで開いた要素内にある画像をタップで切り替え
- 最後の画像をタップでモーダルを閉じる
発生している問題
モーダルを別々にしているのがそもそもダメなのでしょうか・・・
最後の画像判定のところでモーダルを閉じる処理をすればよいのかという
単純なものではなさそうでして。
ソースコード
<div id="change" class="modal"> <img alt=""> </div> ----------------------------------------------------------------- <script> $(function(){ window.onload = function(){ $('.modal').fadeIn(); return false; } $(function(){ const imgs = ['01.png','02.png', '03.png', '04.png', '05.png']; let index = 0; $('.img').attr('src', 'images/' + imgs[index]); $('#change').click(function(){ if(index == imgs.length - 1){ index = 0; }else{ index++; } $('.img').attr('src', 'images/' + imgs[index]); }); }); </script>
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
javascript
1<script> 2window.addEventListener('DOMContentLoaded', ()=>{ 3 const dlg=document.querySelector('dialog'); 4 const img=document.querySelector('dialog img'); 5 const imgs=[ 6 "https://placehold.jp/00ff00/00000/100x100.png?text=2", 7 "https://placehold.jp/0000ff/00000/100x100.png?text=3", 8 "https://placehold.jp/ffff00/00000/100x100.png?text=4", 9 "https://placehold.jp/00ffff/00000/100x100.png?text=5", 10 ]; 11 let count=0; 12 img.addEventListener('click',()=>{ 13 if(count>=imgs.length) dlg.close(); 14 img.src=imgs[count]; 15 count++; 16 }); 17 dlg.showModal(); 18}); 19</script> 20 21<dialog> 22<img src="https://placehold.jp/ff0000/00000/100x100.png?text=1"><br> 23click image! 24</dialog>
スクロールしてからモーダルを開く
javascript
1<style> 2.dummy{ 3height:150%; 4 background-Color:gray; 5} 6#target{ 7background-Color:red; 8} 9</style> 10<script> 11window.addEventListener('DOMContentLoaded', async()=>{ 12 const dlg=document.querySelector('dialog'); 13 const img=document.querySelector('dialog img'); 14 const imgs=[ 15 "https://placehold.jp/00ff00/00000/100x100.png?text=2", 16 "https://placehold.jp/0000ff/00000/100x100.png?text=3", 17 "https://placehold.jp/ffff00/00000/100x100.png?text=4", 18 "https://placehold.jp/00ffff/00000/100x100.png?text=5", 19 ]; 20 let count=0; 21 img.addEventListener('click',()=>{ 22 if(count>=imgs.length){ 23 dlg.close(); 24 return; 25 } 26 img.src=imgs[count]; 27 count++; 28 }); 29 let timerId; 30 let onScroll = ()=>{ 31 clearTimeout(timerId); 32 timerId = setTimeout(()=>{ 33 dlg.showModal(); 34 window.removeEventListener('scroll',onScroll); 35 },100); 36 }; 37 window.addEventListener('scroll',onScroll); 38 window.scrollTo({ 39 top: document.body.scrollTop+target.getBoundingClientRect().top, 40 behavior: "smooth", 41 }); 42}); 43</script> 44<dialog> 45<img src="https://placehold.jp/ff0000/00000/100x100.png?text=1"><br> 46click image! 47</dialog> 48<div class="dummy"></div> 49<div id="target">target</div> 50<div class="dummy"></div>
targetの75%以上が表示されたらモーダルをだす
javascript
1<style> 2.dummy{ 3height:150%; 4 background-Color:gray; 5} 6#target{ 7background-Color:red; 8height:50%; 9} 10</style> 11<script> 12window.addEventListener('DOMContentLoaded', async()=>{ 13 const dlg=document.querySelector('dialog'); 14 const img=document.querySelector('dialog img'); 15 const imgs=[ 16 "https://placehold.jp/00ff00/00000/100x100.png?text=2", 17 "https://placehold.jp/0000ff/00000/100x100.png?text=3", 18 "https://placehold.jp/ffff00/00000/100x100.png?text=4", 19 "https://placehold.jp/00ffff/00000/100x100.png?text=5", 20 ]; 21 let count=0; 22 img.addEventListener('click',()=>{ 23 if(count>=imgs.length){ 24 dlg.close(); 25 document.body.style.overflowY="auto"; 26 return; 27 } 28 img.src=imgs[count]; 29 count++; 30 }); 31 const onScroll = ()=>{ 32 const observer = new IntersectionObserver(changes=>{ 33 for (let change of changes) { 34 if(change.intersectionRatio >= 0.75){ 35 document.body.style.overflowY="hidden"; 36 dlg.showModal(); 37 window.removeEventListener('scroll',onScroll); 38 } 39 } 40 }); 41 observer.observe(document.querySelector('#target')); 42 }; 43 window.addEventListener('scroll',onScroll); 44}); 45</script> 46<dialog> 47<img src="https://placehold.jp/ff0000/00000/100x100.png?text=1"><br> 48click image! 49</dialog> 50<div class="dummy"></div> 51<div id="target">target</div> 52<div class="dummy"></div>
localStorageでページごとにチェックをする
// prev.html
javascript
1<script> 2const checked=new Set(JSON.parse(localStorage.getItem('checked')??"[]")); 3window.addEventListener('DOMContentLoaded', ()=>{ 4 console.log(checked); 5 btn_s.addEventListener('click',()=>{ 6 checked.add(location.href); 7 localStorage.setItem('checked',JSON.stringify([...checked])); 8 console.log(checked); 9 }); 10 btn_r.addEventListener('click',()=>{ 11 checked.delete(location.href); 12 localStorage.setItem('checked',JSON.stringify([...checked])); 13 console.log(checked); 14 }); 15}); 16</script> 17 18<input type="button" id="btn_s" value="set"> 19<input type="button" id="btn_r" value="remove"> 20<a href="next.html">next.html</a>
// next.html
javascript
1<script> 2const checked=new Set(JSON.parse(localStorage.getItem('checked')??"[]")); 3window.addEventListener('DOMContentLoaded', ()=>{ 4 console.log(checked); 5 btn_s.addEventListener('click',()=>{ 6 checked.add(location.href); 7 localStorage.setItem('checked',JSON.stringify([...checked])); 8 console.log(checked); 9 }); 10 btn_r.addEventListener('click',()=>{ 11 checked.delete(location.href); 12 localStorage.setItem('checked',JSON.stringify([...checked])); 13 console.log(checked); 14 }); 15}); 16</script> 17 18<input type="button" id="btn_s" value="set"> 19<input type="button" id="btn_r" value="remove"> 20<a href="prev.html">prev</a>
投稿2023/01/11 03:25
編集2023/01/16 13:19総合スコア114839
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/01/11 18:14
2023/01/12 00:48
2023/01/12 11:48
2023/01/12 12:15
2023/01/12 13:48
2023/01/13 00:19
2023/01/13 04:30
2023/01/13 05:22 編集
2023/01/16 12:42
2023/01/16 12:50
2023/01/16 12:54
2023/01/16 12:57
2023/01/16 13:06 編集
2023/01/16 13:17
2023/01/16 13:24