Skip to content

Commit

Permalink
fixbug: 页面有zoom放大缩小时,自动定位位置不符合预期 yiminghe#55
Browse files Browse the repository at this point in the history
  • Loading branch information
houlinjiang committed Dec 22, 2022
1 parent 7110744 commit dfaab23
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ domAlign(sourceNode, targetNode, alignConfig);
<tr>
<td>offset</td>
<td>Number[2]</td>
<td>offset source node by offset[0] in x and offset[1] in y.
<td>offset source node by offset[0] in x and offset[1] in y.
If offset contains percentage string value, it is relative to sourceNode region.</td>
</tr>
<tr>
<td>targetOffset</td>
<td>Number[2]</td>
<td>offset target node by offset[0] in x and offset[1] in y.
<td>offset target node by offset[0] in x and offset[1] in y.
If targetOffset contains percentage string value, it is relative to targetNode region.</td>
</tr>
<tr>
Expand Down
4 changes: 2 additions & 2 deletions examples/point.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { alignPoint } from '../src';
import ReactDOM from 'react-dom';
import createReactClass from 'create-react-class';

class Demo extends React.Component {
state = {
Expand Down Expand Up @@ -99,5 +98,6 @@ class Demo extends React.Component {
);
}
}

document.body.style.transform = 'scale(0.8)'
document.body.style.transformOrigin = 'top left'
ReactDOM.render(<Demo />, document.getElementById('__react-content'));
4 changes: 4 additions & 0 deletions examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const div = (
style={{
width: 400,
height: 400,
marginLeft: 100,
overflow: 'auto',
border: '1px solid green',
position: 'relative',
Expand Down Expand Up @@ -154,3 +155,6 @@ const div = (
);

ReactDOM.render(div, $id('__react-content'));

document.body.style.transform = 'scale(0.9)'
document.body.style.transformOrigin = 'top left'
1 change: 1 addition & 0 deletions src/propertyUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function getTransitionProperty(node) {
return node.style.transitionProperty || node.style[getTransitionName()];
}

// 获取偏移量
export function getTransformXY(node) {
const style = window.getComputedStyle(node, null);
const transform =
Expand Down
8 changes: 6 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,15 @@ function setTransform(elem, offset) {
const originalOffset = getOffset(elem);
const originalXY = getTransformXY(elem);
const resultXY = { x: originalXY.x, y: originalXY.y };

// https://github.com/yiminghe/dom-align/issues/55
// 当元素的 scale 不为 1 就距离会等比例缩放
const scale = elem.getBoundingClientRect().width / elem.clientWidth;
if ('left' in offset) {
resultXY.x = originalXY.x + offset.left - originalOffset.left;
resultXY.x = originalXY.x + (offset.left - originalOffset.left) / scale;
}
if ('top' in offset) {
resultXY.y = originalXY.y + offset.top - originalOffset.top;
resultXY.y = originalXY.y + (offset.top - originalOffset.top ) / scale;
}
setTransformXY(elem, resultXY);
}
Expand Down

0 comments on commit dfaab23

Please sign in to comment.