C
C
chippolino2932018-03-01 15:46:58
JavaScript
chippolino293, 2018-03-01 15:46:58

Block scrolling in the middle of the landing page. How to do?

Good afternoon. There is a landing page, in the middle there is a section, it is 100vh, when you reach it, block-by-block scrolling is done. is obtained in this section vertical scrolling of the slides. When a person has reached this section, the scroll is turned off and only the slides are hidden, when the last slide is reached, the scroll is on and you can continue to scroll through the landing page. Duck, the question is how to do this, when you scroll from bottom to top, you again scroll through the slides from the last to the first. that is, the focus was again on this block.
here is a piece of js code -

// ---------- поблочная прокрутка старт ----------
var swiper = new Swiper('.stages_swiper', {
  direction: 'vertical',
  slidesPerView: 1,
  spaceBetween: 30,
  pagination: {
    el: '.swiper-pagination',
    clickable: true,
    renderBullet: function (index, className) {
      return '<span class="stages_head__circle ' + className + '"></span>';
    },
  },
});

// методы слайдера swiper
var lastSlide = true;
swiper.on('reachEnd', function () {
  swiper.mousewheel.disable();
  enableScroll();
});
swiper.on('reachBeginning', function(){
  swiper.mousewheel.disable();
  enableScroll();
  animateFlag = 1;
})

var scroll = true; // Флаг прокрутки, если false, то отключена.
var animateFlag = 1;

window.onscroll = function() {
    
    if (animateFlag) {
        var scrolled = window.pageYOffset || document.documentElement.scrollTop;

        if (scrolled > $('.stages_swiper').offset().top && scrolled < $('.stages_swiper').offset().top + $('.stages_swiper').width()) {
            $('body,html').animate({
                scrollTop: $('.stages_swiper').offset().top
            }, 700);   
            animateFlag = 0;
            disableScroll();
            swiper.mousewheel.enable();
        } 
  }
    
}


var keys = {37: 1, 38: 1, 39: 1, 40: 1};

function preventDefault(e) {
  e = e || window.event;
  if (e.preventDefault)
      e.preventDefault();
  e.returnValue = false;  
}

function preventDefaultForScrollKeys(e) {
    if (keys[e.keyCode]) {
        preventDefault(e);
        return false;
    }
}

function disableScroll() {
  if (window.addEventListener) // older FF
      window.addEventListener('DOMMouseScroll', preventDefault, false);
  window.onwheel = preventDefault; // modern standard
  window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
  window.ontouchmove  = preventDefault; // mobile
  document.onkeydown  = preventDefaultForScrollKeys;
}

function enableScroll() {
    if (window.removeEventListener)
        window.removeEventListener('DOMMouseScroll', preventDefault, false);
    window.onmousewheel = document.onmousewheel = null; 
    window.onwheel = null; 
    window.ontouchmove = null;  
    document.onkeydown = null;  
}
// ---------- поблочная прокрутка конец ----------


Link to sandbox

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question