Skip to content

Commit a183fee

Browse files
committed
Update utils.js
1 parent 25a6423 commit a183fee

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

utils.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,64 @@ let moveElToEl = (originEl, destEl, boundryMargin = null, boundryEl = null) => {
476476
let destTop = rect.top + destHeight,
477477
destLeft = rect.left;
478478

479-
// check if menu is outside window
479+
// check if element is outside window
480+
if (maxTop < destTop) destTop = maxTop;
481+
if (minTop > destTop) destTop = minTop;
482+
if (maxLeft < destLeft) destLeft = maxLeft;
483+
if (minLeft > destLeft) destLeft = minLeft;
484+
485+
486+
originEl.style.top = destTop + 'px';
487+
originEl.style.left = destLeft + 'px';
488+
489+
}
490+
491+
492+
// move element to mouse (when element has 'position: fixed')
493+
494+
let moveElToMouse = (originEl, mouseEvent, boundryMargin = null, boundryEl = null) => {
495+
496+
// get bounding box of origin element
497+
const originHeight = originEl.clientHeight,
498+
originWidth = originEl.clientWidth;
499+
500+
501+
// define window constraints
502+
// (stop moving element when it goes offscreen)
503+
let maxTop = window.innerHeight,
504+
minTop = -originHeight,
505+
maxLeft = window.innerWidth,
506+
minLeft = -originWidth;
507+
508+
509+
// if defined boundry element,
510+
// update constraints
511+
if (boundryEl) {
512+
513+
maxTop = boundryEl.clientHeight;
514+
maxLeft = boundryEl.clientWidth;
515+
516+
}
517+
518+
519+
// add margin from boundry edges
520+
if (boundryMargin && !isNaN(boundryMargin)) {
521+
522+
// add vertical margin from screen edges
523+
maxTop -= originHeight + boundryMargin;
524+
minTop = boundryMargin;
525+
526+
// add horizontal margin from screen edges
527+
maxLeft -= originWidth + boundryMargin;
528+
minLeft = boundryMargin;
529+
530+
}
531+
532+
533+
let destTop = mouseEvent.clientY,
534+
destLeft = mouseEvent.clientX;
535+
536+
// check if element is outside window
480537
if (maxTop < destTop) destTop = maxTop;
481538
if (minTop > destTop) destTop = minTop;
482539
if (maxLeft < destLeft) destLeft = maxLeft;

0 commit comments

Comments
 (0)