Windows滚动上的Angular EventListener如何工作?

我正在尝试找出如何在导航页下方跟踪导航栏的位置,以便导航栏到达顶部时,它会粘在顶部,直到您返回为止。我在stackoverflow和其他示例中尝试了很多选项,但是没有运气。

这是我的代码。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Mainactivity">

    <LinearLayout
        android:id="@+id/no_internet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone"
        android:background="#e8eaf6"
        android:layout_centerInParent="true"
        android:gravity="center" >

        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/no_wifi" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#404852"
            android:gravity="center"
            android:textSize="25dp"
            android:text="Internet indisponible" />

    </LinearLayout>
</RelativeLayout>

以及导航组件的html

import {
 Component,OnInit,HostListener,ElementRef,ViewChild,AfterViewInit
} from "@angular/core";
import { ScrollDispatcher } from "@angular/cdk/scrolling";

@Component({
selector: "app-nav",templateUrl: "./nav.component.html",styleUrls: ["./nav.component.css"]
})
export class NavComponent implements OnInit,AfterViewInit {
 @ViewChild("stickyMenu",{ static: true }) menuElement: ElementRef;

 sticky: boolean = false;
 elementPosition: any;

   constructor(
  private el: ElementRef,private scrollDispatcher: ScrollDispatcher
) {}

 ngOnInit() {
  window.addEventListener("scroll",this.onWindowScroll,true);
}

ngAfterViewInit() {
this.scrollDispatcher
  .ancestorScrolled(this.el)
  .subscribe(event => this.onWindowScroll());
this.elementPosition = this.menuElement.nativeElement.offsetTop;
}

@HostListener("window:scroll",['$event'])
onWindowScroll() {
const windowScroll = window.pageYOffset;
console.log("sss",windowScroll,this.elementPosition);
}
 }

jgkzh 回答:Windows滚动上的Angular EventListener如何工作?

将类似这样的内容添加到导航组件控制器中:

@HostListener("window:scroll",[])
onWindowScroll() {
 // logic here to fix to top
}

或者如果您有某种全高/全宽容器,实际上是滚动的内容(而不是窗口):

<div class="scrolling-container" (scroll)="scrollHandler()"></div>

如果这不容易做到,则角材料具有有用的滚动模块:

在安装角形材料之后,将ScrollingModule导入到您需要的任何模块中:

import {ScrollingModule} from '@angular/cdk/scrolling';

imports: [... ScrollingModule ...]

然后将指令附加到容器:

<div class="scrolling-container" cdkScrollable></div>

然后在您的导航组件中注入调度程序:

constructor(private el: ElementRef,private scrollDispatcher: ScrollDispatcher) {
  this.scrollDispatcher.ancestorScrolled(this.el).subscribe(event => this.scrollHandler())
}

docs:https://material.angular.io/cdk/scrolling/api

,

Check the mdn of getBoundingClientRect

Check working example on stackblitz

在html

    let string = "There is a string and string"

    let result = string.components(separatedBy: " ").map({ str -> Int in
        if str == "string" {
            return 2
        }else {
            return 0
        }
    })

print(result)

**Output -** [0,2,2]

还有ts。

>>> b = a = ['hell','word']
>>> c = ['hell','word']

>>> id(a),id(b),id(c)
(4424020872,4424020872,4423979272) 
     |           |
      -----------

>>> id(a[0]),id(b[0]),id(c[0])
(4424018328,4424018328,4424018328) # all referring to same 'hell'
     |           |           |
      -----------------------

>>> id(a[0][0]),id(b[0][0]),id(c[0][0])
(4422785208,4422785208,4422785208) # all referring to same 'h'
     |           |           |
      -----------------------

>>> a[0] += 'o'
>>> a,b,c
(['hello','word'],['hello',['hell','word'])  # b changed too
>>> id(a[0]),id(c[0])
(4424018384,4424018384,4424018328) # augmented assignment changed a[0],b[0]
     |           |
      -----------

>>> b = a = ['hell','word']
>>> id(a[0]),4424018328) # the same hell
     |           |           |
      -----------------------

>>> import gc
>>> gc.get_referrers(a[0]) 
[['hell','word']]  # one copy belong to a,the another for c
>>> gc.get_referrers(('hell'))
[['hell',('hell',None)] # ('hello',None) 
本文链接:https://www.f2er.com/3044532.html

大家都在问