実現したいこと
メインビジュアルから次のセクションまでの間のみ、スクロール量に関わらず、1スクロールで移動させたいです。参考サイトのように、メインビジュアル部分でスクロールイベントが起こった際、連続したスクロールをしても1スクロールで次のセクションまでスクロールされるようにしたいです。
また、参考サイトではメインビジュアルから次のセクションへスクロールすると、表示上は次のセクションにスクロールされているのですが、スクロールバーは動かないという現象がおこっていますが、この仕組みもよくわからないので、併せてお聞きしたいです。
参考サイト:https://www.yadohouse.jp/
発生している問題・分からないこと
メインビジュアルからスクロールを行うと、クラスを付与しスムーズスクロールされるようにはできたのですが、スクロールを連続して行うと、カクついてしまいます。
また、メインビジュアルから次のセクションにスクロールすると、通常のスクロールと同じ挙動をしてしまいます。
こちらにソースコードを記述しますが、CodePenのURLも念のため記述します。
CodePen:https://codepen.io/k_2024/pen/NPWXLbj
該当のソースコード
html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4<title></title> 5 6<!-- Javascript --> 7<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" ></script> 8</head> 9 10<body id="page"> 11 12<div class="pageWrap"> 13 <div class="mv"> 14 <div class="inner"> 15 <figure> 16 <img src="https://placehold.jp/1920x700.png"> 17 </figure> 18 </div> 19 </div> 20 <div class="wrapper"> 21 <div class="page-content"> 22 <div class="section"> 23 直下コンテンツの内容がここに入ります。 24 </div> 25 </div> 26 </div> 27</div> 28 29</body> 30</html> 31
scss
1.mv{ 2 position: relative; 3 4 .inner{ 5 display: flex; 6 flex-wrap: nowrap; 7 8 figure{ 9 height: 100vh; 10 11 img{ 12 width: 100%; 13 object-fit: contain; 14 } 15 } 16 } 17} 18 19.wrapper{ 20 height: 200vh; 21 background: #ccc; 22} 23
jQuery
1$(function () { 2 var $mainVisual = $('.mv'); 3 var $section = $('.section'); 4 var $wrapper = $('.wrapper'); 5 var isAnimating = false; 6 var touchStartY = 0; 7 var touchMoveY = 0; 8 9 /* スムーズスクロール */ 10 function smoothScroll($target) { 11 if (isAnimating) return; 12 isAnimating = true; 13 14 $('html, body').stop().animate( 15 { 16 scrollTop: $target.offset().top, 17 }, 18 800, 19 function () { 20 isAnimating = false; 21 } 22 ); 23 } 24 25 $(window).on('wheel', function (event) { 26 if (isAnimating) return; 27 28 var deltaY = event.originalEvent.deltaY; 29 var scrollTop = $(window).scrollTop(); 30 var mainVisualBottom = $mainVisual.offset().top + $mainVisual.outerHeight(true); 31 var conceptTop = $section.offset().top; 32 var conceptBottom = conceptTop + $section.outerHeight(true); 33 var windowHeight = $(window).height(); 34 35 if (scrollTop < mainVisualBottom && deltaY > 15) { 36 smoothScroll($wrapper); 37 } else if (deltaY < -80) { 38 if (scrollTop < conceptTop || scrollTop > conceptBottom) { 39 return; 40 } 41 if (scrollTop + windowHeight <= conceptBottom) { 42 return; 43 } 44 if (scrollTop <= conceptTop + windowHeight * 0.1) { 45 smoothScroll($mainVisual); 46 } 47 } 48 }); 49 50 $(window).on('touchstart', function (event) { 51 touchStartY = event.originalEvent.touches[0].pageY; 52 }); 53 54 $(window).on('touchmove', function (event) { 55 touchMoveY = event.originalEvent.touches[0].pageY - touchStartY; 56 }); 57 58 $(window).on('touchend', function () { 59 var scrollTop = $(window).scrollTop(); 60 var mainVisualBottom = $mainVisual.offset().top + $mainVisual.outerHeight(true); 61 var conceptTop = $section.offset().top; 62 var conceptBottom = conceptTop + $section.outerHeight(true); 63 var windowHeight = $(window).height(); 64 65 if (scrollTop < mainVisualBottom && touchMoveY < -45) { 66 smoothScroll($wrapper); 67 } else if (scrollTop < conceptTop || scrollTop > conceptBottom) { 68 return; 69 } 70 if (scrollTop + windowHeight <= conceptBottom) { 71 return; 72 } 73 if (scrollTop <= conceptTop + windowHeight * 0.1) { 74 smoothScroll($mainVisual); 75 } 76 }); 77}); 78
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
条件を厳格にしているのですが、通常のスムーズスクロールになってしまいました。
補足
特になし
