From 052d92107065a3e07af19025fd4af51059c33617 Mon Sep 17 00:00:00 2001 From: yiminghe Date: Fri, 13 Mar 2020 21:22:12 +0800 Subject: [PATCH] add alwaysByViewport --- HISTORY.md | 4 ++++ README.md | 6 ++++-- package.json | 2 +- src/align/align.js | 3 ++- src/getVisibleRectForElement.js | 4 ++-- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index f9466c7..3999730 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,10 @@ # History ---- +## 1.11.0 + +- add alwaysByViewport option + ## 1.9.0 - support shadow dom diff --git a/README.md b/README.md index 9eb2ca8..588c04c 100644 --- a/README.md +++ b/README.md @@ -95,9 +95,11 @@ domAlign(sourceNode, targetNode, alignConfig); overflow - Object: `{ adjustX: true, adjustY: true }` - if adjustX field is true, then will adjust source node in x direction if source node is invisible. + Object: `{ adjustX: boolean, adjustY: boolean, alwaysByViewport:boolean }` + + if adjustX field is true, then will adjust source node in x direction if source node is invisible. if adjustY field is true, then will adjust source node in y direction if source node is invisible. + if alwaysByViewport is true, the it will adjust if node is not inside viewport diff --git a/package.json b/package.json index f93687e..297f2fc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dom-align", - "version": "1.10.4", + "version": "1.11.0", "description": "Align DOM Node Flexibly ", "keywords": [ "dom", diff --git a/src/align/align.js b/src/align/align.js index 69a4e15..039c9da 100644 --- a/src/align/align.js +++ b/src/align/align.js @@ -87,8 +87,9 @@ function doAlign(el, tgtRegion, align, isTgtRegionVisible) { overflow = overflow || {}; const newOverflowCfg = {}; let fail = 0; + const alwaysByViewport = !!(overflow && overflow.alwaysByViewport); // 当前节点可以被放置的显示区域 - const visibleRect = getVisibleRectForElement(source); + const visibleRect = getVisibleRectForElement(source, alwaysByViewport); // 当前节点所占的区域, left/top/width/height const elRegion = getRegion(source); // 将 offset 转换成数值,支持百分比 diff --git a/src/getVisibleRectForElement.js b/src/getVisibleRectForElement.js index 816fd77..3d689ea 100644 --- a/src/getVisibleRectForElement.js +++ b/src/getVisibleRectForElement.js @@ -5,7 +5,7 @@ import isAncestorFixed from './isAncestorFixed'; /** * 获得元素的显示部分的区域 */ -function getVisibleRectForElement(element) { +function getVisibleRectForElement(element, alwaysByViewport) { const visibleRect = { left: 0, right: Infinity, @@ -86,7 +86,7 @@ function getVisibleRectForElement(element) { element.style.position = originalPosition; } - if (isAncestorFixed(element)) { + if (alwaysByViewport || isAncestorFixed(element)) { // Clip by viewport's size. visibleRect.left = Math.max(visibleRect.left, scrollX); visibleRect.top = Math.max(visibleRect.top, scrollY);