S/O!
S / O !
I need a reliable way to detect if $(window).on('resize')
has been fired by user interaction or by a jQuery .trigger
call.
我需要一种可靠的方法来检测$(window).on('resize')是否被用户交互或jQuery .trigger调用触发。
I've tried the following, but I don't seem to get the parameters back in the function call:
我已经尝试过了,但是我似乎没有在函数调用中返回参数:
$(window).trigger('resize',[true]);
function prepareOnResize(e, triggered){ console.dir(triggered); }
$(window).on('resize',prepareOnResize);
Triggered seems to return the width of the screen, so I tried the following, also to no avail:
触发似乎返回屏幕的宽度,所以我尝试了以下,也没有用:
$(window).trigger('resize',[true]);
function prepareOnResize(e, width, height, triggered){ console.dir(triggered); }
$(window).on('resize',prepareOnResize);
In that instance triggered comes back undefined
.
在该实例中,触发返回未定义。
I'm at a loss here -- any help would be greatly appreciated. Thanks!
我在这里损失惨重——任何帮助都将不胜感激。谢谢!
2 个解决方案
#1
1
jQuery puts a property isTrigger
in the event object, which you can test:
jQuery在事件对象中放置一个属性isTrigger,您可以测试:
function prepareOnResize(e){
if (e.isTrigger) {
console.log('resize via jQuery trigger');
} else {
console.log('resize by user');
}
}
Alternatively you can use e.originalEvent
, which jQuery only the native event object has, as mentioned here:
或者你也可以用e。原始事件,jQuery只有本地事件对象,如这里所述:
function prepareOnResize(e){
if (!e.originalEvent)
console.log('resize via jQuery trigger');
} else {
console.log('resize by user');
}
}
#2
2
You could try the following;
你可以试试下面的方法;
Javascript... on dom ready...
Javascript……dom准备好了…
var $window = $(window);
$window.on('resize', function(event, param1) {
console.log("Window resized event triggered by...");
if (typeof param1 === 'undefined') // could also do if(param1) to test for false
console.log("System Event");
else
console.log("User Event");
// Another way to check is to check the originalEvent property
// if (e.originalEvent === undefined) alert ('triggered by code');
// else alert ('triggered by system');
});
setTimeout(function() {
$window.trigger("resize", ["Custom"]);
}, 2000);
This is more or less what you were trying in your code, and it will distinguish the 2 types of events.
这或多或少是您在代码中尝试的内容,它将区分这两种类型的事件。
I also made a jsFiddle so you can see: https://jsfiddle.net/sysdevcode/4w3b8e14/
我还做了一个jsFiddle,以便您可以看到:https://jsfiddle.net/sysdevcode/4w3b8e14/。
To make it work on your code
让它在你的代码上运行。
$(window).trigger('resize',[true]);
function prepareOnResize(e, width, height, triggered){ console.dir(triggered); }
$(window).on('resize',prepareOnResize);
You need to change the order of the params on the prepareOnResize()
function, as $(window).trigger('resize',[true]);
equates to the [true] being sent as the "width" param in your function. you wold need to do, for instance:
您需要将params的顺序更改为prepareOnResize()函数,如$(window).trigger('resize',[true]);将[true]发送为函数中的“宽度”参数。你需要做的,例如:
var $window = $(window);
$window.trigger('resize',[ $window.width(), $window.height(), true ]);
then
然后
function prepareOnResize(e, width, height, triggered){ ... }
will work, but if you don't need to pass any params, then the e.isTrigger
of the code above is probably a bit more elegent.
会起作用,但如果你不需要传递任何参数,那么e。上面代码的isTrigger可能更优雅一些。
#1
1
jQuery puts a property isTrigger
in the event object, which you can test:
jQuery在事件对象中放置一个属性isTrigger,您可以测试:
function prepareOnResize(e){
if (e.isTrigger) {
console.log('resize via jQuery trigger');
} else {
console.log('resize by user');
}
}
Alternatively you can use e.originalEvent
, which jQuery only the native event object has, as mentioned here:
或者你也可以用e。原始事件,jQuery只有本地事件对象,如这里所述:
function prepareOnResize(e){
if (!e.originalEvent)
console.log('resize via jQuery trigger');
} else {
console.log('resize by user');
}
}
#2
2
You could try the following;
你可以试试下面的方法;
Javascript... on dom ready...
Javascript……dom准备好了…
var $window = $(window);
$window.on('resize', function(event, param1) {
console.log("Window resized event triggered by...");
if (typeof param1 === 'undefined') // could also do if(param1) to test for false
console.log("System Event");
else
console.log("User Event");
// Another way to check is to check the originalEvent property
// if (e.originalEvent === undefined) alert ('triggered by code');
// else alert ('triggered by system');
});
setTimeout(function() {
$window.trigger("resize", ["Custom"]);
}, 2000);
This is more or less what you were trying in your code, and it will distinguish the 2 types of events.
这或多或少是您在代码中尝试的内容,它将区分这两种类型的事件。
I also made a jsFiddle so you can see: https://jsfiddle.net/sysdevcode/4w3b8e14/
我还做了一个jsFiddle,以便您可以看到:https://jsfiddle.net/sysdevcode/4w3b8e14/。
To make it work on your code
让它在你的代码上运行。
$(window).trigger('resize',[true]);
function prepareOnResize(e, width, height, triggered){ console.dir(triggered); }
$(window).on('resize',prepareOnResize);
You need to change the order of the params on the prepareOnResize()
function, as $(window).trigger('resize',[true]);
equates to the [true] being sent as the "width" param in your function. you wold need to do, for instance:
您需要将params的顺序更改为prepareOnResize()函数,如$(window).trigger('resize',[true]);将[true]发送为函数中的“宽度”参数。你需要做的,例如:
var $window = $(window);
$window.trigger('resize',[ $window.width(), $window.height(), true ]);
then
然后
function prepareOnResize(e, width, height, triggered){ ... }
will work, but if you don't need to pass any params, then the e.isTrigger
of the code above is probably a bit more elegent.
会起作用,但如果你不需要传递任何参数,那么e。上面代码的isTrigger可能更优雅一些。