Skip to content

Commit

Permalink
add alwaysByViewport
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Mar 13, 2020
1 parent cefc5fd commit 052d921
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# History
----

## 1.11.0

- add alwaysByViewport option

## 1.9.0

- support shadow dom
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ domAlign(sourceNode, targetNode, alignConfig);
</tr>
<tr>
<td>overflow</td>
<td>Object: `{ adjustX: true, adjustY: true }`</td>
<td>if adjustX field is true, then will adjust source node in x direction if source node is invisible.
<td>Object: `{ adjustX: boolean, adjustY: boolean, alwaysByViewport:boolean }`</td>
<td>
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
</td>
</tr>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dom-align",
"version": "1.10.4",
"version": "1.11.0",
"description": "Align DOM Node Flexibly ",
"keywords": [
"dom",
Expand Down
3 changes: 2 additions & 1 deletion src/align/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 转换成数值,支持百分比
Expand Down
4 changes: 2 additions & 2 deletions src/getVisibleRectForElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import isAncestorFixed from './isAncestorFixed';
/**
* 获得元素的显示部分的区域
*/
function getVisibleRectForElement(element) {
function getVisibleRectForElement(element, alwaysByViewport) {
const visibleRect = {
left: 0,
right: Infinity,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 052d921

Please sign in to comment.