Skip to content

Commit eac77b4

Browse files
committed
use offsetPositions and offsetDimensions to get positions in sliders
1 parent 1d8b781 commit eac77b4

File tree

4 files changed

+37
-33
lines changed

4 files changed

+37
-33
lines changed

Diff for: src/js/control-bar/progress-control/progress-control.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ProgressControl extends Component {
5959
if (seekBar) {
6060
const mouseTimeDisplay = seekBar.getChild('mouseTimeDisplay');
6161
const seekBarEl = seekBar.el();
62-
const seekBarRect = Dom.getBoundingClientRect(seekBarEl);
62+
const seekBarRect = Dom.findPosition(seekBarEl);
6363
let seekBarPoint = Dom.getPointerPosition(seekBarEl, event).x;
6464

6565
// The default skin has a gap on either side of the `SeekBar`. This means

Diff for: src/js/control-bar/progress-control/time-tooltip.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class TimeTooltip extends Component {
3737
* from the left edge of the {@link SeekBar}
3838
*/
3939
update(seekBarRect, seekBarPoint, content) {
40-
const tooltipRect = Dom.getBoundingClientRect(this.el_);
40+
const tooltipRect = Dom.findPosition(this.el_);
4141
const playerRect = Dom.getBoundingClientRect(this.player_.el());
4242
const seekBarPointPx = seekBarRect.width * seekBarPoint;
4343

Diff for: src/js/utils/dom.js

+25-26
Original file line numberDiff line numberDiff line change
@@ -546,34 +546,31 @@ export function getBoundingClientRect(el) {
546546
* The position of the element that was passed in.
547547
*/
548548
export function findPosition(el) {
549-
let box;
550-
551-
if (el.getBoundingClientRect && el.parentNode) {
552-
box = el.getBoundingClientRect();
553-
}
554-
555-
if (!box) {
549+
if (!el || (el && !el.offsetParent)) {
556550
return {
557551
left: 0,
558-
top: 0
552+
top: 0,
553+
width: 0,
554+
height: 0
559555
};
560556
}
557+
const width = el.offsetWidth;
558+
const height = el.offsetHeight;
559+
let left = 0;
560+
let top = 0;
561561

562-
const docEl = document.documentElement;
563-
const body = document.body;
564-
565-
const clientLeft = docEl.clientLeft || body.clientLeft || 0;
566-
const scrollLeft = window.pageXOffset || body.scrollLeft;
567-
const left = box.left + scrollLeft - clientLeft;
562+
do {
563+
left += el.offsetLeft;
564+
top += el.offsetTop;
568565

569-
const clientTop = docEl.clientTop || body.clientTop || 0;
570-
const scrollTop = window.pageYOffset || body.scrollTop;
571-
const top = box.top + scrollTop - clientTop;
566+
el = el.offsetParent;
567+
} while (el);
572568

573-
// Android sometimes returns slightly off decimal values, so need to round
574569
return {
575-
left: Math.round(left),
576-
top: Math.round(top)
570+
left,
571+
top,
572+
width,
573+
height
577574
};
578575
}
579576

@@ -606,14 +603,16 @@ export function findPosition(el) {
606603
*/
607604
export function getPointerPosition(el, event) {
608605
const position = {};
609-
const boxW = el.offsetWidth;
610-
const boxH = el.offsetHeight;
611-
let offsetY = event.offsetY;
612-
let offsetX = event.offsetX;
606+
const boxTarget = findPosition(event.target);
607+
const box = findPosition(el);
608+
const boxW = box.width;
609+
const boxH = box.height;
610+
let offsetY = event.offsetY - (box.top - boxTarget.top);
611+
let offsetX = event.offsetX - (box.left - boxTarget.left);
613612

614613
if (event.changedTouches) {
615-
offsetX = event.changedTouches[0].pageX - el.offsetLeft;
616-
offsetY = event.changedTouches[0].pageY - el.offsetTop;
614+
offsetX = event.changedTouches[0].pageX - box.left;
615+
offsetY = event.changedTouches[0].pageY + box.top;
617616
}
618617

619618
position.y = Math.max(0, Math.min(1, (offsetY + boxH) / boxH));

Diff for: test/unit/utils/dom.test.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -285,26 +285,31 @@ QUnit.test('Dom.findPosition should find top and left position', function(assert
285285
const d = document.createElement('div');
286286
let position = Dom.findPosition(d);
287287

288+
d.style.width = '100px';
289+
d.style.height = '50px';
288290
d.style.top = '10px';
289291
d.style.left = '20px';
290292
d.style.position = 'absolute';
291293

292294
assert.deepEqual(
293295
position,
294-
{left: 0, top: 0},
296+
{left: 0, top: 0, width: 0, height: 0},
295297
'If element isn\'t in the DOM, we should get zeros'
296298
);
297299

298300
document.body.appendChild(d);
299301
position = Dom.findPosition(d);
300-
assert.deepEqual(position, {left: 20, top: 10}, 'The position was not correct');
302+
assert.deepEqual(position.left, 20, 'The position left was not correct');
303+
assert.deepEqual(position.top, 10, 'The position top was not correct');
304+
assert.deepEqual(position.width, 100, 'The dimension width was not correct');
305+
assert.deepEqual(position.height, 50, 'The dimension height was not correct');
301306

302-
d.getBoundingClientRect = null;
307+
d.style.display = 'none';
303308
position = Dom.findPosition(d);
304309
assert.deepEqual(
305310
position,
306-
{left: 0, top: 0},
307-
'If there is no gBCR, we should get zeros'
311+
{left: 0, top: 0, width: 0, height: 0},
312+
'If there is no offsetParent, we should get zeros'
308313
);
309314
});
310315

0 commit comments

Comments
 (0)