简体   繁体   English

内容固定后,可以触摸或滚动浏览

[英]Navigate on touch or scroll when content is fixed

I am trying to add scrolling navigation to a presentation style landing page in React and GatsbyJS . 我正在尝试将滚动导航添加到ReactGatsbyJS中的演示文稿样式登录页面。

The issue lies in that I cannot where each page(slide) is fixed at 100% height and no scrolling occurs. 问题在于,我无法将每个页面(幻灯片) fixed100%高度,并且不会发生滚动。

As you can see from the below code, the keydown listener navigates using the down arrow, but how can I achieve the same action from a mousewheel, scroll, touchpad or swipe down? 从下面的代码中可以看到, keydown侦听器使用向下箭头导航,但是如何通过鼠标滚轮,滚动,触摸板或向下滑动来实现相同的操作?

class Index extends Component {
  NEXT = 40;

  swipeUp = () => {
    this.navigate({ keyCode: this.NEXT });
  };

  navigate = ({ keyCode }) => {
    if (keyCode === this.NEXT) {
      navigate(ROUTES.SLIDE2);
    }
    return false;
  };

  componentDidMount() {
    if (typeof window !== 'undefined') {
      window.addEventListener('keydown', this.navigate);
    }
  }

  componentWillUnmount() {
    if (typeof window !== 'undefined') {
      window.removeEventListener('keydown', this.navigate);
    }
  }

  render() {
    return (
      <PageContent>
        <PageContainer />
      </PageContent>
    );
  }
}

export default Index;

TL;DR https://codepen.io/devanshj/pen/RvmMpM TL; DR https://codepen.io/devanshj/pen/RvmMpM

  1. For the wheel part, you are correct that scroll event won't fire (because there's no content overflowing as the pages are position: fixed ). 对于滚轮部分,您会正确地认为不会触发scroll事件(因为在页面的position: fixed上没有内容溢出position: fixed )。 But the wheel event will fire regardless. 但是无论如何, wheel事件都会触发。 And then we can use the wheelDelta from the wheel event to get the direction of the scrolling. 然后,我们可以使用wheel事件中的wheelDelta来获取滚动方向。

  2. For the swipe part, our problem could have been solved if we had some stuff scrolling and user swiping would fire an scroll event. 对于滑动部分,如果我们进行一些滚动并且用户滑动会触发scroll事件,则本来的问题可能已经解决。 But that's not the case. 但是事实并非如此。 In our case we'll have to figure out swipe event from events like touchstart , touchmove and touchend . 在我们的案例中,我们必须从touchstarttouchmovetouchend类的事件中找出滑动事件。 There's a 7kb library called hammer.js that does all that stuff and we can simply listen to events like swipeup . 有一个7kb的库,名为Hammer.js ,它可以完成所有这些工作,我们可以简单地侦听swipeup事件。

  3. You have not provided an appropriate sample to reproduce the problem, so I have made a basic sample here (same as above link). 您没有提供适当的示例来重现该问题,因此我在这里做了一个基本示例(与上面的链接相同)。 I have not used react because it's irrelevant to the problem, you can implement this is your react app too. 我没有使用react,因为它与问题无关,您也可以实现它也是您的react应用。 (If you can't, provide the react sample and I'll help you out but imo it won't be too hard) (如果不能,请提供react示例,我会帮助您,但是imo不会太困难)

React supports following events and you can use them in similar manner to keydown React支持以下事件,您可以以类似于按键的方式使用它们

  • mousewheel : use onWheel event 鼠标滚轮:使用onWheel事件
  • scroll : use onScroll event scroll:使用onScroll事件
  • touchpad : use onTouchCancel onTouchEnd onTouchMove onTouchStart 触摸板:使用onTouchCancel onTouchEnd onTouchMove onTouchStart

more list of events is here : https://reactjs.org/docs/events.html#touch-events 更多事件列表在这里: https : //reactjs.org/docs/events.html#touch-events

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

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