简体   繁体   English

平滑滚动事件滚动

[英]smooth scroll an event scroll

I want to make a smooth scroll to the anchor which will be activated not by clicking, but just by an event scroll. 我想平滑滚动到锚点,而不是通过单击来激活,而只是通过事件滚动来激活。

Moving the scroll itself will cause the scroll to go down to the section, and moving the scroll up will give the same return effect to the first section. 移动滚动条本身将导致滚动条下降到该部分,而向上移动滚动条将使第一部分具有相同的返回效果。

I'm trying to do it in jQuery, but I miss something. 我正在尝试在jQuery中进行操作,但是我错过了一些东西。 I manage to pull down to the bottom section, but the function blocks me. 我设法下拉至底部,但该功能阻止了我。 Sure there is some simple tip for it, but I do not know what to look for. 当然有一些简单的提示,但我不知道要寻找什么。

function transitionScroll() {
    if ($(window).width() <= 767) {

      let scrollWatch = window.pageYOffset;
      let positionOfElement = $("#small-carousel").offset().top;

      if ((scrollWatch <= positionOfElement) && (scrollWatch != 0)) {
        $([document.documentElement, document.body]).animate({ scrollTop: $("#small-carousel").offset().top }, 1000);

      }
    }

    $(window).resize(function () {
      if ($(window).width() <= 767) {

        let scrollWatch = window.pageYOffset;
        let positionOfElement = $("#small-carousel").offset().top;

        if (scrollWatch <= positionOfElement) {
          $([document.documentElement, document.body]).animate({ scrollTop: $("#small-carousel").offset().top }, 1000);

        }
      }
    });
  };

  transitionScroll();

  window.addEventListener('scroll', transitionScroll);

Finally, I wrote a working code. 最后,我编写了一个工作代码。

https://codepen.io/toMuchMike/pen/JVWppq?editors=0011 https://codepen.io/toMuchMike/pen/JVWppq?editors=0011

let windowOnSecond = false;
let touchStart;
const positionOfFirst = $(".first-section").offset().top;
const positionOfSecond = $(".second-section").offset().top;


$(document).ready(function () {

  function doScrolling() {
  $(document).on('touchstart', function (e) {
   touchStart = e.originalEvent.touches[0].clientY;
});


$(document).on('touchend', function (e) {
   var touchEnd = e.originalEvent.changedTouches[0].clientY;
   if (touchStart > touchEnd + 5) {
      let scrollPosition = window.pageYOffset;

     //slide_down
     if ((scrollPosition = positionOfFirst) && (windowOnSecond === false)) {
      $("html, body").animate({ scrollTop: $(".second-section").offset().top }, 1000);
      windowOnSecond = true;
       console.log("DOWN");
      } 


   } else if (touchStart < touchEnd - 5) {
      let scrollPosition = window.pageYOffset;

     //slide_up
     if (scrollPosition <= positionOfSecond) {
      $("html, body").animate({ scrollTop: $(".first-section").offset().top }, 1000);
      windowOnSecond = false;   
       console.log("UP");
    }  
   }
});
};

  if ($(window).width() <= 767) {
    doScrolling();
  }

  $(window).resize(function () {
    if ($(window).width() <= 767) {
      doScrolling();
    }
  });

});

The website behaves as I want but for some reason the function loads with a delay, it can block scrolling for a second before the code is executed. 该网站的行为符合我的要求,但是由于某些原因,该功能会延迟加载,因此在执行代码之前它可能会阻塞滚动一秒钟。 The event that runs the code is the detection of the direction of touch as here: Determine vertical direction of a touchmove This is best seen on the touch screen, or the development tools in the mobile mode. 运行代码的事件是按以下方式检测触摸方向: 确定触摸移动的垂直方向这最好在触摸屏或移动模式下的开发工具上看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM