【发布时间】:2018-07-05 18:58:48
【问题描述】:
我正在尝试制定 Snap-to-Component 指令。在这一点上,我已经使用应用程序组件中的逻辑进行了概念验证(可能会被 Renderer2 HostListeners 和 HostBindings 替换),看起来我必须获取 dom 属性(child1L、child1R ......等.) 的孩子可能正在使用 Renderer2。我的问题是你如何将我到目前为止所做的事情分解为一个指令或一组父子指令?或者至少你会从这里往哪个方向走?我是使用 Renderer2、HostListeners 和 HostBinding 的新手,所以我想知道一个更有经验的人会如何做到这一点。谢谢你。这是我的current stackblitz.
app.component.ts:
import { Component,ElementRef,ViewChild } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
switchDirection = "first";
head: ElementRef;
@ViewChild('child01') child01;
@ViewChild('child02') child02;
@ViewChild('child03') child03;
onScroll(event: Event) {
let viewBoundaryL = (event.target as HTMLElement).scrollLeft;
console.log("viewBoundaryL:" + viewBoundaryL);
//SNAP FROM FIRST TO SECOND CHILD
if(viewBoundaryL >= 50 && this.switchDirection === "first"){
this.child02.el.nativeElement.scrollIntoView({ behavior: 'smooth', block: "center" });
setTimeout(() => {
this.switchDirection = "second";
// console.log(this.switchDirection)
}, 300);
}
//SNAP FROM SECOND TO FIRST CHILD
if(viewBoundaryL <= 310 && this.switchDirection === "second"){
this.child01.el.nativeElement.scrollIntoView({ behavior: 'smooth', block: "center" });
setTimeout(() => {
this.switchDirection = "first";
// console.log(this.switchDirection)
}, 300);
}
//SNAP FROM SECOND TO THIRD CHILD
if(viewBoundaryL >= 370 && this.switchDirection === "second"){
this.child03.el.nativeElement.scrollIntoView({ behavior: 'smooth', block: "center" });
setTimeout(() => {
this.switchDirection = "third";
// console.log(this.switchDirection)
}, 300);
}
//SNAP FROM THIRD TO SECOND CHILD
if(viewBoundaryL <= 615 && this.switchDirection === "third"){
this.child02.el.nativeElement.scrollIntoView({ behavior: 'smooth', block: "center" });
setTimeout(() => {
this.switchDirection = "second";
// console.log(this.switchDirection)
}, 300);
}
}
}
app.component.html:
<div class="container" (scroll)="onScroll($event)" >
<child1 #child01></child1>
<child2 #child02></child2>
<child3 #child03></child3>
</div>
感谢您的见解!
【问题讨论】:
标签: angular typescript angular-directive