admin管理员组文章数量:1426388
I am trying to return the location of an element but for some reason the client, scroll and offset values for left and top are always wrong. The goal here is to determine if the user clicked outside of the dropdown element and if so close it so I need an accurate location for the element to pare against the click location.
For example I checked the location of the element using a ruler and it reads a left value of 1013px and a top value of 484px. However when I get the element in code and check offsetLeft the value is 3 and offsetTop is 16. What is going on here? It seems like I'm getting the location relative to the parent instead of the document.
I use angular and get the element using a template reference in the ponent.
dropdownponent.ts
@ViewChild('dropdown') dropdown: any; // Template reference to the element
constructor(
private elementRef: ElementRef // Reference to the :host element
} { }
ngOnDestroy() {
this.elementRef.nativeElement.removeEventListener('click', this.handleClick.bind(this));
}
ngOnInit() {
this.subscribeToClickEvent();
}
private handleClick(event: MouseEvent): void {
// this.dropdown.nativeElement.clientLeft equals 0
// this.dropdown.nativeElement.clientTop equals 0
// this.dropdown.nativeElement.offsetLeft equals 3
// this.dropdown.nativeElement.offsetTop equals 16
// this.dropdown.nativeElement.scrollLeft equals 0
// this.dropdown.nativeElement.scrollTop equals 0
// Left should equal ~1013 and top should equal ~484
}
private subscribeToClickEvent(): void {
this.elementRef.nativeElement.addEventListener('click', this.handleClick.bind(this));
}
I am trying to return the location of an element but for some reason the client, scroll and offset values for left and top are always wrong. The goal here is to determine if the user clicked outside of the dropdown element and if so close it so I need an accurate location for the element to pare against the click location.
For example I checked the location of the element using a ruler and it reads a left value of 1013px and a top value of 484px. However when I get the element in code and check offsetLeft the value is 3 and offsetTop is 16. What is going on here? It seems like I'm getting the location relative to the parent instead of the document.
I use angular and get the element using a template reference in the ponent.
dropdown.ponent.ts
@ViewChild('dropdown') dropdown: any; // Template reference to the element
constructor(
private elementRef: ElementRef // Reference to the :host element
} { }
ngOnDestroy() {
this.elementRef.nativeElement.removeEventListener('click', this.handleClick.bind(this));
}
ngOnInit() {
this.subscribeToClickEvent();
}
private handleClick(event: MouseEvent): void {
// this.dropdown.nativeElement.clientLeft equals 0
// this.dropdown.nativeElement.clientTop equals 0
// this.dropdown.nativeElement.offsetLeft equals 3
// this.dropdown.nativeElement.offsetTop equals 16
// this.dropdown.nativeElement.scrollLeft equals 0
// this.dropdown.nativeElement.scrollTop equals 0
// Left should equal ~1013 and top should equal ~484
}
private subscribeToClickEvent(): void {
this.elementRef.nativeElement.addEventListener('click', this.handleClick.bind(this));
}
Share
Improve this question
edited Jul 11, 2017 at 22:26
efarley
asked Jul 11, 2017 at 22:14
efarleyefarley
8,70113 gold badges45 silver badges67 bronze badges
2 Answers
Reset to default 6You have to use the element.getBoundingClientRect() to get the accurate location.
offsetLeft
and offsetTop
of an element return the offset relative to its offsetParent
. This is either
- a parent element with a non-
static
position - a
td
,th
ortable
parent element (only if the element itself has positionstatic
) - or the document
body
.
Now depending on what you want, make sure to position
your target element or its parent element accordingly.
本文标签:
版权声明:本文标题:javascript - Getting incorrect values for element's position from clientLeft, clientTop, offsetLeft, and offsetTop - Sta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745474319a2659890.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论