最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Horizontal mousewheel slider with overflowed content and inner scrollbar - Stack Overflow

programmeradmin1浏览0评论

I'm making fullscreen slider using Swiper, where slide change controlled via mouse wheel. But in some slides, I have overflowed content. And when I try to scroll this content - the slide change to the next.

Is it possible to scroll to next slide only when I scrolled to the end of the slide content? Here is a demo , I want to scroll to the bottom of the first slide via mouse wheel.

//js
var swiperOptions = {
  loop: false,
  speed: 1000,
  grabCursor: false,
  mousewheel: true,
};

var swiper = new Swiper(".swiper-container", swiperOptions);

//css
.swiper-slide {
  overflow: auto;
}

I'm making fullscreen slider using Swiper, where slide change controlled via mouse wheel. But in some slides, I have overflowed content. And when I try to scroll this content - the slide change to the next.

Is it possible to scroll to next slide only when I scrolled to the end of the slide content? Here is a demo https://codepen.io/olegburakov/pen/EdqeLW, I want to scroll to the bottom of the first slide via mouse wheel.

//js
var swiperOptions = {
  loop: false,
  speed: 1000,
  grabCursor: false,
  mousewheel: true,
};

var swiper = new Swiper(".swiper-container", swiperOptions);

//css
.swiper-slide {
  overflow: auto;
}
Share Improve this question edited Nov 1, 2018 at 13:28 Elham Esmaeeli 799 bronze badges asked Nov 1, 2018 at 13:20 Oleg BurakovOleg Burakov 552 gold badges2 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Found a solution. It suits to scrollable blocks that has size smaller than slide size. Because if it has equal size(or almost equal) it will be hard to change slide via mouse wheel, because scrollable block will catch mouse wheel event. The idea is simple: just stopPropagation of the event in scrollable block.

document.querySelector('.scrollable-content').addEventListener('mousewheel', function(e){
    e.stopPropagation();
  });

Demo: https://codepen.io/olegburakov/pen/vVoPBE

Even I found a solution to check if scroll ended, there is no demo, but here is a code:

  const scrollableBlock = document.querySelector('.scrollable-block');
     if ('onwheel' in document) {
        // IE9+, FF17+, Ch31+
        scrollableBlock.addEventListener('wheel', scrollOverflowBlock);
      } else if ('onmousewheel' in document) {
        // for old
        scrollableBlock.addEventListener('mousewheel', scrollOverflowBlock);
      }

  function scrollOverflowBlock(e) {
    const delta = e.deltaY || e.wheelDelta;

    //Scroll up
    if (delta < 0) {
      if (e.target.scrollTop !== 0) {
        e.stopPropagation();
        console.log('Slider scroll stopped (top)');
      }
    }
    //Scroll down
    else {
      //http://qnimate./detecting-end-of-scrolling-in-html-element/
      if (e.target.offsetHeight + e.target.scrollTop !== e.target.scrollHeight) {
        e.stopPropagation();
        console.log('Slider scroll stopped (bottom)');
      }
    }
  }

Maybe it will be helpful someone, or maybe someone propose better solution.

You should add direction={"horizontal"} in swiper.

Here's a partial solution, maybe it will help. Try adding this to your Swiper config

  mousewheel: {
      forceToAxis: true,
  }

It stops swiper from doing horizontal scrolling when the user scrolls vertically with trackpad or mouse wheel, which has the effect of restoring default behavior to the vertical scrolling. It works in the Codepen.

The downside is it doesn't matter if the user has scrolled to the bottom of the content or not.

If you can change a Swiper instance's configuration without re-initializing it, you might be able to add your own event handling to turn forceToAxis on/off based on whether the user has scrolled to the bottom of the current slide's content.

On a side note: it looks like Swiper is designed only with presentations / slideshows in mind. It doesn't look like they've thought about internal scrolling, in which case it might not be the right library for the job.

发布评论

评论列表(0)

  1. 暂无评论