10 examples of 'get position of element javascript' in JavaScript

Every line of 'get position of element javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
230public getHTMLElementPosition (element: HTMLElement): IHTMLElementPosition {
231 if (sys.platform === sys.WECHAT_GAME) {
232 return {
233 left: 0,
234 top: 0,
235 width: window.innerWidth,
236 height: window.innerHeight,
237 };
238 }
239
240 const docElem = document.documentElement;
241 const leftOffset = window.pageXOffset - docElem.clientLeft;
242 const topOffset = window.pageYOffset - docElem.clientTop;
243 if (element.getBoundingClientRect) {
244 const box = element.getBoundingClientRect();
245 return {
246 left: box.left + leftOffset,
247 top: box.top + topOffset,
248 width: box.width,
249 height: box.height,
250 };
251 } else {
252 if (element instanceof HTMLCanvasElement) {
253 return {
254 left: leftOffset,
255 top: topOffset,
256 width: element.width,
257 height: element.height,
258 };
259 } else {
260 return {
261 left: leftOffset,
262 top: topOffset,
263 width: parseInt(element.style.width || '0', undefined),
264 height: parseInt(element.style.height || '0', undefined),
265 };
266 }
267 }
268}
189function getElementPosition(element) {
190 var elem=element, tagname="", x=0, y=0;
191
192 while((typeof(elem) == "object") && (typeof(elem.tagName) != "undefined")) {
193 y += elem.offsetTop;
194 x += elem.offsetLeft;
195 tagname = elem.tagName.toUpperCase();
196
197 if(tagname == "BODY")
198 elem=0;
199
200 if(typeof(elem) == "object") {
201 if(typeof(elem.offsetParent) == "object")
202 elem = elem.offsetParent;
203 }
204 }
205
206 return {x: x, y: y};
207}
11function getElementPosition(element, siblings) {
12 var position = 0,
13 i = 0;
14
15 for (i = 0; i < siblings.length; i += 1) {
16 if (element === siblings[i]) {
17 position = i;
18 break;
19 }
20 }
21
22 return position;
23}
91function getElementPosition (obj) {
92 var curleft = curtop = 0;
93 if (obj.offsetParent) {
94 do {
95 curleft += obj.offsetLeft;
96 curtop += obj.offsetTop;
97 } while (obj = obj.offsetParent);
98 return {x:curleft,y:curtop};
99 }
100 return undefined;
101}
188function getPosition (element){
189 let position = {x:0, y:0, z:0};
190 if(element.attributes.position == null)
191 return position;
192
193 let myPos = element.attributes.position.value.split(" ");
194 if(myPos[0] !== "" && myPos[0] != null)
195 position['x'] = myPos[0];
196 if(myPos[1] !== "" && myPos[1] != null)
197 position['y'] = myPos[1];
198 if(myPos[2] !== "" && myPos[2] != null)
199 position['z'] = myPos[2];
200 return position;
201}
51function getElementPosition(element) {
52 var elem=element, tagname="", x=0, y=0;
53 while((typeof(elem) == "object") && (typeof(elem.tagName) != "undefined")) {
54 y += elem.offsetTop;
55 x += elem.offsetLeft;
56 tagname = elem.tagName.toUpperCase();
57
58 if(tagname == "BODY")
59 elem=0;
60
61 if(typeof(elem) == "object") {
62 if(typeof(elem.offsetParent) == "object")
63 elem = elem.offsetParent;
64 }
65 }
66 return {x: x, y: y};
67}
155setElement(element: Element, position: AbsolutePosition): void {
156 (element as HTMLElement).style.top = `${position.top}px`;
157 (element as HTMLElement).style.left = `${position.left}px`;
158}
143private getElementPosition(position: Point): { point: Point; line: Point[]; text: Point } {
144 const { x, y } = position;
145 const lineLength = this.get('line').display ? this.get('line').lineLength : 0;
146 const direction = this.get('direction');
147 const textStyle = this.get('text').style;
148 textStyle.textBaseline = direction === 'upward' ? 'bottom' : 'top';
149 const dir = direction === 'upward' ? -1 : 1;
150 const pointPoisition = { x, y };
151 const lineStart = { x, y };
152 const lineEnd = { x, y: lineLength * dir + y };
153 const textPosition = { x, y: (lineLength + 2) * dir + y };
154
155 return {
156 point: pointPoisition,
157 line: [ lineStart, lineEnd ],
158 text: textPosition,
159 };
160}
92function getElementPosition (el: Element, offset: Object): Object {
93 const docEl: any = document.documentElement
94 const docRect = docEl.getBoundingClientRect()
95 const elRect = el.getBoundingClientRect()
96 return {
97 x: elRect.left - docRect.left - offset.x,
98 y: elRect.top - docRect.top - offset.y
99 }
100}
66function getPos(e)
67 {
68 //For handling events in ie vs. w3c.
69 curEvent = ((typeof event == "undefined")? e: event);
70 //Sets mouse flag as down.
71 mouseButtonPos = "down";
72 //Gets position of click.
73 curX = curEvent.clientX;
74 //Get the width of the div.
75 curwidth = document.getElementById("treeView").offsetWidth;
76
77 curwidth1 = document.getElementById("bodyView").offsetWidth - document.getElementById("splitterBar").offsetWidth + 1;
78/*
79 if( mouseButtonPos == "down" )
80 {
81 document.onselectstart = function () { return false; } // ie
82 document.onmousedown = function () { return false; } // mozilla
83 }
84 else
85 {
86 document.onselectstart = def_select;
87 document.onmousedown = def_down;
88 }
89*/
90/*document.blur();*/
91
92 }

Related snippets