How can I make it so that each time when user changes the screen resolution size [not the browser window], the page perform a function?
我怎样才能使每次用户改变屏幕分辨率(而不是浏览器窗口)时,页面执行一个函数?
6 个解决方案
#1
35
Ok, so you're using jQuery. So let's make a custom event for it.
使用jQuery。让我们为它创建一个自定义事件。
(function () {
var width = screen.width,
height = screen.height;
setInterval(function () {
if (screen.width !== width || screen.height !== height) {
width = screen.width;
height = screen.height;
$(window).trigger('resolutionchange');
}
}, 50);
}());
Now $(window).bind('resolutionchange', fn)
should do what you want.
现在美元(窗口)。bind('resolutionchange, fn)应该做你想做的事。
#2
16
$(window).resize()
$(窗口).resize()
http://api.jquery.com/resize/
$(window).resize(function() {
alert('window was resized!');
});
#3
8
Try tracking screen.width
and screen.height
. They will return different values when changing the screen resolution. More info here.
试着跟踪屏幕。宽度和screen.height。当改变屏幕分辨率时,它们将返回不同的值。更多的信息在这里。
function doSomething(){
if ( screen.width < 1280 ){
console.log('Too small')
}else{
console.log('Nice!')
}
}
However, as far as i know there are no events triggered when changing the screen resolution; Which means you cannot do this $(screen).resize(function(){/*code here*/});
但是,据我所知,改变屏幕分辨率时没有触发事件;这意味着您不能执行这个$(屏幕).resize(函数(){/*这里的代码*/});
So another way to do it will be using a setTimeout()
such as: [not recommended]
另一种方法是使用setTimeout()例如:[不推荐]
var timer,
checkScreenSize = function(){
if ( screen.width < 1280 ){
console.log('Too small')
}else{
console.log('Nice!')
}
timer = setTimeout(function(){ checkScreenSize(); }, 50);
};
checkScreenSize();
The recommended version will be using the requestAnimationFrame. As described here by Paul Irish. Because if you're running the loop in a tab that's not visible, the browser won't keep it running. For better overall performance.
推荐的版本将使用requestAnimationFrame。正如保罗·爱尔兰所描述的。因为如果你在一个不可见的标签中运行循环,浏览器就不会让它继续运行。为了更好的整体性能。
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
// usage:
// instead of setInterval(checkScreenSize, 50) ....
(function loop(){
requestAnimFrame(loop);
checkScreenSize();
})();
[update]
For those who want to implement requestAnimationFrame in Nathan's answer, there you go; A custom jQuery event that is triggered on resolution change, uses requestAnimationFrame when available for less memory usage:
对于那些想要在Nathan的答案中实现requestAnimationFrame的人来说,就是这样;一个自定义jQuery事件,在分辨率改变时触发,使用requestAnimationFrame,当内存使用较少时:
window.requestAnimFrame = (function(){
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function( callback ){ window.setTimeout(callback, 1000 / 60); };
})();
var width = screen.width,
height = screen.height,
checkScreenSize = function () {
if (screen.width !== width || screen.height !== height) {
width = screen.width;
height = screen.height;
$(window).trigger('resolutionchange');
}
};
(function loop(){
requestAnimFrame(loop);
checkScreenSize();
})();
Usage:
用法:
$(window).bind('resolutionchange', function(){
console.log('You have just changed your resolution!');
});
#4
3
Because you can only from within a specific browser-window check for changes within that same browser-window, it is not possible to know about resolution-changes of the display.
因为您只能在特定的浏览器窗口中检查同一浏览器窗口中的更改,所以不可能知道显示的分辨率更改。
However, if the browser window also changes when the display resolution changes, you can catch this with a listener on the window.width and window.height.
但是,如果浏览器窗口在显示分辨率发生变化时也发生了变化,您可以通过窗口上的侦听器捕捉到这一点。宽度和window.height。
edit: It seems we can obtain the information you want from the global 'window.screen' object. See https://developer.mozilla.org/en/DOM/window.screen.height and https://developer.mozilla.org/en/DOM/window.screen.width for more information!
编辑:似乎我们可以从全局窗口获取您想要的信息。屏幕上的对象。查看https://developer.mozilla.org/en/DOM/window.screen.height和https://developer.mozilla.org/en/DOM/window.screen.width以获得更多信息!
#5
2
Try this js :
试试这个js:
var width = $(window).width();
var height = $(window).height();
var screenTimer = null;
function detectScreen (){
$(window).resize(function() {
height = $(window).height();
width = $(window).width();
getScreen ();
});
function getScreen (){
return { 'height' : getHeight (), 'width': getWidth () };
}
screenTimer = setInterval ( getScreen (), 50 );
}
function getHeight (){
console.log ( 'height: ' + height);
$('#height').text(height);
return height;
}
function getWidth (){
console.log ( 'width: ' + width);
$('#width').text(width);
return width;
}
detectScreen ();
$('#go').click (function (){
detectScreen ();
});
$('#stop').click (function (){
clearInterval(screenTimer);
});
And for html :
对于html:
<span id="stop">Stop</span> | <span id="go">Go</span>
<br>
<div>height: <span id="height"></span> px</div>
<div>width: <span id="width"></span>px </div>
#6
1
The following function fires on window re-sizing as well as resolution change and also has a delay to avoid multiple calls while the user is re-sizing the window.
下面的功能触发了窗口重新调整大小和分辨率的变化,并且在用户重新调整窗口大小时,还会有一个延迟来避免多次调用。
I've set up a fiddle for you here:
我在这里为你们准备了一个小提琴
http://jsfiddle.net/EJK5L/
Change your resolution and function alerts you. you can perform any function, what you want.
改变你的决心和功能提醒你。你可以执行任何你想要的函数。
Hope this helps.
希望这个有帮助。
#1
35
Ok, so you're using jQuery. So let's make a custom event for it.
使用jQuery。让我们为它创建一个自定义事件。
(function () {
var width = screen.width,
height = screen.height;
setInterval(function () {
if (screen.width !== width || screen.height !== height) {
width = screen.width;
height = screen.height;
$(window).trigger('resolutionchange');
}
}, 50);
}());
Now $(window).bind('resolutionchange', fn)
should do what you want.
现在美元(窗口)。bind('resolutionchange, fn)应该做你想做的事。
#2
16
$(window).resize()
$(窗口).resize()
http://api.jquery.com/resize/
$(window).resize(function() {
alert('window was resized!');
});
#3
8
Try tracking screen.width
and screen.height
. They will return different values when changing the screen resolution. More info here.
试着跟踪屏幕。宽度和screen.height。当改变屏幕分辨率时,它们将返回不同的值。更多的信息在这里。
function doSomething(){
if ( screen.width < 1280 ){
console.log('Too small')
}else{
console.log('Nice!')
}
}
However, as far as i know there are no events triggered when changing the screen resolution; Which means you cannot do this $(screen).resize(function(){/*code here*/});
但是,据我所知,改变屏幕分辨率时没有触发事件;这意味着您不能执行这个$(屏幕).resize(函数(){/*这里的代码*/});
So another way to do it will be using a setTimeout()
such as: [not recommended]
另一种方法是使用setTimeout()例如:[不推荐]
var timer,
checkScreenSize = function(){
if ( screen.width < 1280 ){
console.log('Too small')
}else{
console.log('Nice!')
}
timer = setTimeout(function(){ checkScreenSize(); }, 50);
};
checkScreenSize();
The recommended version will be using the requestAnimationFrame. As described here by Paul Irish. Because if you're running the loop in a tab that's not visible, the browser won't keep it running. For better overall performance.
推荐的版本将使用requestAnimationFrame。正如保罗·爱尔兰所描述的。因为如果你在一个不可见的标签中运行循环,浏览器就不会让它继续运行。为了更好的整体性能。
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
// usage:
// instead of setInterval(checkScreenSize, 50) ....
(function loop(){
requestAnimFrame(loop);
checkScreenSize();
})();
[update]
For those who want to implement requestAnimationFrame in Nathan's answer, there you go; A custom jQuery event that is triggered on resolution change, uses requestAnimationFrame when available for less memory usage:
对于那些想要在Nathan的答案中实现requestAnimationFrame的人来说,就是这样;一个自定义jQuery事件,在分辨率改变时触发,使用requestAnimationFrame,当内存使用较少时:
window.requestAnimFrame = (function(){
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function( callback ){ window.setTimeout(callback, 1000 / 60); };
})();
var width = screen.width,
height = screen.height,
checkScreenSize = function () {
if (screen.width !== width || screen.height !== height) {
width = screen.width;
height = screen.height;
$(window).trigger('resolutionchange');
}
};
(function loop(){
requestAnimFrame(loop);
checkScreenSize();
})();
Usage:
用法:
$(window).bind('resolutionchange', function(){
console.log('You have just changed your resolution!');
});
#4
3
Because you can only from within a specific browser-window check for changes within that same browser-window, it is not possible to know about resolution-changes of the display.
因为您只能在特定的浏览器窗口中检查同一浏览器窗口中的更改,所以不可能知道显示的分辨率更改。
However, if the browser window also changes when the display resolution changes, you can catch this with a listener on the window.width and window.height.
但是,如果浏览器窗口在显示分辨率发生变化时也发生了变化,您可以通过窗口上的侦听器捕捉到这一点。宽度和window.height。
edit: It seems we can obtain the information you want from the global 'window.screen' object. See https://developer.mozilla.org/en/DOM/window.screen.height and https://developer.mozilla.org/en/DOM/window.screen.width for more information!
编辑:似乎我们可以从全局窗口获取您想要的信息。屏幕上的对象。查看https://developer.mozilla.org/en/DOM/window.screen.height和https://developer.mozilla.org/en/DOM/window.screen.width以获得更多信息!
#5
2
Try this js :
试试这个js:
var width = $(window).width();
var height = $(window).height();
var screenTimer = null;
function detectScreen (){
$(window).resize(function() {
height = $(window).height();
width = $(window).width();
getScreen ();
});
function getScreen (){
return { 'height' : getHeight (), 'width': getWidth () };
}
screenTimer = setInterval ( getScreen (), 50 );
}
function getHeight (){
console.log ( 'height: ' + height);
$('#height').text(height);
return height;
}
function getWidth (){
console.log ( 'width: ' + width);
$('#width').text(width);
return width;
}
detectScreen ();
$('#go').click (function (){
detectScreen ();
});
$('#stop').click (function (){
clearInterval(screenTimer);
});
And for html :
对于html:
<span id="stop">Stop</span> | <span id="go">Go</span>
<br>
<div>height: <span id="height"></span> px</div>
<div>width: <span id="width"></span>px </div>
#6
1
The following function fires on window re-sizing as well as resolution change and also has a delay to avoid multiple calls while the user is re-sizing the window.
下面的功能触发了窗口重新调整大小和分辨率的变化,并且在用户重新调整窗口大小时,还会有一个延迟来避免多次调用。
I've set up a fiddle for you here:
我在这里为你们准备了一个小提琴
http://jsfiddle.net/EJK5L/
Change your resolution and function alerts you. you can perform any function, what you want.
改变你的决心和功能提醒你。你可以执行任何你想要的函数。
Hope this helps.
希望这个有帮助。