简体   繁体   English

在 div 上滚动而不触发 angular 中的整页滚动

[英]Scroll on div without triggering full page scroll in angular

I have a website which have one page scroll feature using this - https://alvarotrigo.com/angular-fullpage/ Now in this website, In one page I want to create a division inside which the fullpage scroll feature is disabled and I can scroll that division as normal - like this https://stackblitz.com/edit/angular-kqvraz?file=src%2Fapp%2Fapp.component.html我有一个网站,它具有使用此功能的一页滚动功能 - https://alvarotrigo.com/angular-fullpage/现在在这个网站中,我想在一个页面中创建一个分区,其中禁用全页滚动功能,我可以像往常一样滚动该分区 - 像这样https://stackblitz.com/edit/angular-kqvraz?file=src%2Fapp%2Fapp.component.html

What I have done till now -到现在为止我所做的——

app.component.html app.component.html

<app-navbar></app-navbar>
<div fullpage id="fullpage2" [options]="config" (ref)="getRef($event)">
    <div class="section" id="banner">
        //first section
    </div>
    <div class="section" id="demos">
        //second section
    </div>
    <div class="section" id="prod-solution">
       // third section   
     </div>
    <div class="section" id="scroll-solution">

        <div style="height: 200px; border: 1px solid; overflow: auto;">
        // div where I want to disable full page scroll and enable normal scroll
            <div>
                Please scroll
                <div style="height: 1000px; width: 1000px;"></div>
            </div>
        </div>
    </div>
</div>

app.component.ts app.component.ts

export class AppComponent {
  config: any;
  fullpage_api: any;

  constructor() {

    // for more details on config options please visit fullPage.js docs
    this.config = {

      // fullpage options
      licenseKey: 'YOUR LICENSE KEY HERE',
      anchors: ['firstPage', 'secondPage', 'thirdPage', 'fourthPage', 'lastPage'],
      menu: '#menu',

      // fullpage callbacks
      afterResize: () => {
        console.log("After resize");
      },
      afterLoad: (origin, destination, direction) => {
        console.log(origin.index);
      }
    };
  }

  getRef(fullPageRef) {
    this.fullpage_api = fullPageRef;
  }

}

For scrolling on pages higher than 100vh, We want scrolling to be done normally and when we get to the bottom of the page, do a full scroll.对于高于 100vh 的页面滚动,我们希望正常滚动,当我们到达页面底部时,进行完整滚动。 For this purpose, you can use the fullpage.js package, which requires a license.为此,您可以使用需要许可证的 fullpage.js package。 But by typescript, it can be easily implemented.但是通过typescript,可以轻松实现。

sample in stackblitzstackblitz中采样

in file.html use (mousewheel):在 file.html 使用(鼠标滚轮):

<div class="container" id="main-container" 
(mousewheel)="changeMouseWheel($event)">
    <div class="panel" id="el1"></div>
    <div class="panel" id="el2"></div>
    <div class="panel" id="el3"></div>
    <div class="panel" id="el4"></div>
    <div class="panel" id="el5"></div>
    <div class="panel" id="el6">
      <h1>whit long height</h1>
    </div>
</div>

in fil.css:在 fil.css 中:

.panel{
  width: 100%;
  height: 100vh;
}
#el1 {background-color: antiquewhite}
#el2 {background-color: aliceblue}
#el3 {background-color: beige}
#el4 {background-color: aqua}
#el5 {background-color: #00ffae
}
#el6 {height: 200vh; background-color: #6200ff
}

in file.ts:在 file.ts 中:

  changeMouseWheel(e) {
    const sectionCount = document.querySelectorAll('.panel').length;
    const windowHeight = window.innerHeight;
    if (e.deltaY < 0 && this.sectionNumber > 1) {
      if (this.hold === false) {
        this.hold = true;
        this.sectionNumber -= 1;
        const element = document.getElementById(`el${this.sectionNumber}`);
        this.scroll(element.offsetTop, 0);
        setTimeout(() => {
          this.hold = false;
        }, 500);
        e.preventDefault();
      }
    }
    if (e.deltaY > 0 && this.sectionNumber < sectionCount) {
      const currentElement = document.getElementById(`el${this.sectionNumber}`);
      if (((currentElement.offsetTop + currentElement.offsetHeight) - windowHeight) <= document.documentElement.scrollTop) {
        if (this.hold === false) {
          this.hold = true;
          this.sectionNumber += 1;
          console.log(`#el${this.sectionNumber}`);
          const nextElement = document.getElementById(`el${this.sectionNumber}`);
          this.scroll(nextElement.offsetTop, 0);
          setTimeout(() => {
            this.hold = false;
          }, 500);
          e.preventDefault();
        }
      }
    }
  }


  scroll(topData: number, leftData: number) {
    window.scrollTo({
      top: topData,
      left: leftData,
      behavior: 'smooth'
    });
  }

You should catch the wheel event on the DIV that shouldn't trigger the fullpage scroll and only scroll this element.您应该在不应触发整页滚动的 DIV 上捕获wheel事件,而仅滚动此元素。

Code代码

Modify the section of your code to match the following one:修改代码部分以匹配以下部分:

<div style="height: 200px; border: 1px solid; overflow: auto;">
    <!-- add a scroll event listener -->
    <div (wheel)="blockScroll($event)">
        Please scroll
        <div style="height: 1000px; width: 1000px;"></div>
    </div>
</div>

Add the event listener in your app.component.ts :app.component.ts中添加事件监听器:

blockScroll(e) {
  let delta = e.deltaY || -e.detail;
  e.currentTarget.scrollTop += delta * 30;
  e.stopPropagation();
  e.preventDefault();
}

Demo演示

I added a scrolling container in "Section 2" that will only scroll its own content without triggering the fullpage scroll.我在“第 2 节”中添加了一个滚动容器,它只会滚动自己的内容而不触发整页滚动。

Demo on StackBlitz StackBlitz 上的演示

If you want other scroll events like touch to be handled as well you need to add the relevant event to the <div> as well.如果您还希望处理其他滚动事件(如触摸),则还需要将相关事件添加到<div>中。

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

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