gpt4 book ai didi

javascript - Angular 4 中的 Interactjs 可调整大小功能(ts 文件)

转载 作者:行者123 更新时间:2023-11-30 20:52:47 26 4
gpt4 key购买 nike

我在 Angular 4 中使用交互。

我的可拖动功能完美运行。

在我的可调整大小方法中,事件不能为我提供除 currentTargettarget 之外的任何对象属性。

http://interactjs.io/#resizing 的示例中:

var target = event.target,

x = (parseFloat(target.getAttribute('data-x')) || 0),
y = (parseFloat(target.getAttribute('data-y')) || 0);

// update the element's style
target.style.width = event.rect.width + 'px';
target.style.height = event.rect.height + 'px';

// translate when resizing from top or left edges
x += event.deltaRect.left;
y += event.deltaRect.top;

event.rectevent.deltaRect 无法到达。它表示 Property event.rectevent.deltaRect 不是 InteractEvent 的类型。

问题为什么会这样?


整个代码是:

let msgFrontX,
msgFrontY,
imgFrontX,
imgFrontY,
msgBackX,
msgBackY,
imgBackX,
imgBackY;
interact('.resize-drag')
.draggable({
// enable inertial throwing
inertia: true,
// keep the element within the area of it's parent
restrict: {
restriction: "parent",
endOnly: true,
elementRect: {
top: 0,
left: 0,
bottom: 1,
right: 1
}
},
// enable autoScroll
autoScroll: true,
onstart: function (event) {},
// call this function on every dragmove event
onmove: dragMoveListener,
// call this function on every dragend event
onend: function (event) {
const parentContainer = event.currentTarget.offsetParent;
const elem = event.currentTarget;

function percentageCalc(direction) {
if (direction === 'horizontal') {
return event.clientX0 / event.clientX 100;
} else {
return event.clientY0 / event.clientY 100;
}
}
if (parentContainer.classList.contains('front')) {
if (elem.classList.contains('msg-container')) {
msgFrontX = percentageCalc('horizontal');
msgFrontY = percentageCalc('vertical');
} else {
imgFrontX = percentageCalc('horizontal');
imgFrontY = percentageCalc('vertical');
}
} else {
if (elem.classList.contains('msg-container')) {
msgBackX = percentageCalc('horizontal');
msgBackY = percentageCalc('vertical');
} else {
imgBackX = percentageCalc('horizontal');
imgBackY = percentageCalc('vertical');
}
}
that.setPositions(msgFrontX, msgFrontY, imgFrontX, imgFrontY,
msgBackX, msgBackY, imgBackX, imgBackY);
}
}).resizable({
edges: {
left: true,
right: true,
bottom: true,
top: true
},
restrict: {
restriction: "parent",
endOnly: true,
elementRect: {
top: 0,
left: 0,
bottom: 1,
right: 1
}
}
}).on('resizeend', function (event) {
console.log(event.resizeRects);
var target = event.target,
x = (parseFloat(target.getAttribute('data-x')) || 0),
y = (parseFloat(target.getAttribute('data-y')) || 0);

// update the element's style
target.style.width = event.rect.width + 'px';
target.style.height = event.rect.height + 'px';

// translate when resizing from top or left edges
x += event.deltaRect.left;
y += event.deltaRect.top;

target.style.webkitTransform = target.style.transform =
'translate(' + x + 'px,' + y + 'px)';

target.setAttribute('data-x', x);
target.setAttribute('data-y', y);

});

function dragMoveListener(event) {
const target = event.target,
// keep the dragged position in the data-x/data-y attributes
x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
// translate the element
target.style.webkitTransform =
target.style.transform =
'translate(' + x + 'px, ' + y + 'px)';
// update the position attributes
target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
}

控制台错误

ERROR in C:/Users/usuario/Documents/sitios/tshirtCustom/src/app/customizer/customizer.component.ts (325,34): Property 'rect' does not exist on type 'InteractEvent'.
ERROR in C:/Users/usuario/Documents/sitios/tshirtCustom/src/app/customizer/customizer.component.ts (326,35): Property 'rect' does not exist on type 'InteractEvent'.
ERROR in C:/Users/usuario/Documents/sitios/tshirtCustom/src/app/customizer/customizer.component.ts (329,18): Property 'deltaRect' does not exist on type 'InteractEvent'.
ERROR in C:/Users/usuario/Documents/sitios/tshirtCustom/src/app/customizer/customizer.component.ts (330,18): Property 'deltaRect' does not exist on type 'InteractEvent'.

最佳答案

InteractEvent接口(interface)没有rect和deltaRect。您可以在 ..\node_modules\interactjs\index.d.ts 中查看

这可能是因为生产版本和示例代码不同。

但有一个变通办法。

.on('resizeend', function (ev) {
const event = <any> ev;
console.log(event.resizeRects);
var target = event.target,
x = (parseFloat(target.getAttribute('data-x')) || 0),
y = (parseFloat(target.getAttribute('data-y')) || 0);

//......
}

关于javascript - Angular 4 中的 Interactjs 可调整大小功能(ts 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47986062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com