问题描述
我已经用jQuery编写了这段代码,并且运行良好
// dedect position on load,resize,change
jQuery.fn.onPositionChanged = function(trigger,millis) {
if (millis == null) millis = 10;
let o = $(this[0]); // our jquery object
if (o.length < 1) return o;
let lastPos = null;
let lastOff = null;
setInterval(function() {
if (o == null || o.length < 1) return o; // abort if element is non existend eny more
if (lastPos == null) lastPos = o.position();
if (lastOff == null) lastOff = o.offset();
let newPos = o.position();
let newOff = o.offset();
if (lastPos.top != newPos.top || lastPos.left != newPos.left) {
$(this).trigger('onPositionChanged',{
lastPos: lastPos,newPos: newPos
});
if (typeof(trigger) == "function") trigger(lastPos,newPos);
lastPos = o.position();
}
if (lastOff.top != newOff.top || lastOff.left != newOff.left) {
$(this).trigger('onOffsetChanged',{
lastOff: lastOff,newOff: newOff
});
if (typeof(trigger) == "function") trigger(lastOff,newOff);
lastOff = o.offset();
}
},millis);
return o;
};
// need to change #pos wih the elemnts from the array
function onChangeElem() {
for(let i of selectors){
$(i).onPositionChanged(function(i){
move_mark(dots)
})
}
}
但是随后我们删除了jQuery并更改了整个代码,因此我们使用了getBoundingClientRect()而不是使用offset()。left和offset()。top
现在的事情是尝试仅使用Javascript来触发x,y,w,h的更改。
我在Google上搜索了很多,但找不到任何有用的东西。
我写的这只是检查,而不是触发更改
function onChangeElem() {
for(let i of selectors){
let s = document.querySelector(i);
let r = s.getBoundingClientRect();
let offY = window.pageYOffset-5;
let offX = window.pageXOffset-5;
let x,y,w,h;
setInterval(function() {
if ( r.x != x || r.y != y || r.width != w || r.height != h) {
x = r.left + window.pageXOffset - 5,y = r.top + window.pageYOffset - 5,w = r.width,h = r.height
//move_mark(dots,x,h);
}
},100);
return s;
}
}
谢谢大家
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)