Sum*_*hal 24 javascript css angular angular-animations
我正在创建一个具有整页宽度/高度div的网页.向下滚动时我有两种方法.
滚动点击
//HTML
<a (click)="goToDiv('about')"></a>
//JS
goToDiv(id) {
let element = document.querySelector("#"+id);
element.scrollIntoView(element);
}
Run Code Online (Sandbox Code Playgroud)
滚动HostListener
@HostListener("window:scroll", ['$event'])
onWindowScroll($event: any): void {
this.topOffSet = window.pageYOffset;
//window.scrollTo(0, this.topOffSet+662);
}
Run Code Online (Sandbox Code Playgroud)
1.如何添加滚动动画效果?
就像 :
$('.scroll').on('click', function(e) {
$('html, body').animate({
scrollTop: $(window).height()
}, 1200);
});
Run Code Online (Sandbox Code Playgroud)
2.如何使用HostListener滚动到下一个div?
小智 11
You can also use the CSS property
scroll-behavior: smooth
in combination with
var yPosition = 1000;
window.scrollTo(0,yPosition)
Run Code Online (Sandbox Code Playgroud)
Ref: developer.mozilla.org/docs/Web/CSS/scroll-behavior
bry*_*n60 10
这个很有趣.与大多数角度2一样,解决方案是可观察的.
getTargetElementRef(currentYPos: int): ElementRef {
// you need to figure out how this works
// I can't comment much on it without knowing more about the page
// but you inject the host ElementRef in the component / directive constructor and use normal vanillaJS functions to find other elements
}
//capture the scroll event and pass to a function that triggers your own event for clarity and so you can manually trigger
scrollToSource: Subject<int> = new Subject<int>();
@HostListener("window:scroll", ['$event'])
onWindowScroll($event: any): void {
var target = getTargetElementRef(window.pageYOffset);
this.scrollTo(target);
}
scrollTo(target: ElementRef): void {
// this assumes you're passing in an ElementRef, it may or may not be appropriate, you can pass them to functions in templates with template variable syntax such as: <div #targetDiv>Scroll Target</div> <button (click)="scrollTo(targetDiv)">Click To Scroll</button>
this.scrollToSource.next(target.nativeElement.offsetTop);
}
//switch map takes the last value emitted by an observable sequence, in this case, the user's latest scroll position, and transforms it into a new observable stream
this.scrollToSource.switchMap(targetYPos => {
return Observable.interval(100) //interval just creates an observable stream corresponding to time, this emits every 1/10th of a second. This can be fixed or make it dynamic depending on the distance to scroll
.scan((acc, curr) => acc + 5, window.pageYOffset) // scan takes all values from an emitted observable stream and accumulates them, here you're taking the current position, adding a scroll step (fixed at 5, though this could also be dynamic), and then so on, its like a for loop with +=, but you emit every value to the next operator which scrolls, the second argument is the start position
.do(position => window.scrollTo(0, position)) /// here is where you scroll with the results from scan
.takeWhile(val => val < targetYPos); // stop when you get to the target
}).subscribe(); //don't forget!
Run Code Online (Sandbox Code Playgroud)
点击一下,这很容易使用.您只需将scrollTo绑定到单击即可
这仅适用于向一个方向滚动,但这应该可以帮助您入门.你可以让扫描更加智能,所以它减去,如果你需要去了,而是使用内部takeWhile一个函数,计算出,如果上升或下降的基础上,正确的终止条件.
归档时间: |
|
查看次数: |
23602 次 |
最近记录: |