【发布时间】:2014-06-12 20:13:27
【问题描述】:
我正在尝试同时使用媒体查询和 JS。所以我需要对 4 种情况使用调整大小功能。 对于每种情况,我都需要删除图片,因此我必须在调整大小时以动态方式删除它们。 我有 4 个范围:1800+ - 1024 || 1024 - 640 || 640-320 || 320- 问题是即使分辨率在范围内,该功能也会启动并重新启动。 所以我想检测该功能何时启动,然后停止。 这是我所做的:
$(window).on('resize', function() {
// Mosaic > 1024px
var screenW = $(window).width();
if (screenW > 1024) {
var size = true;
....
}
if (screenW < 1024 && screenW > 640) {
...
}
...
}
我不知道在检测到 size = true 时如何停止调整大小..
编辑: 谢谢大家!我现在有这个代码
function checkSize()
{
var container = jQuery('#test');
var screenW = jQuery(window).width();
console.log(screenW);
if (screenW > 1800 && current != 1800)
{
container.empty();
jQuery('div#test').append("1800+\n") ;
console.log('1800');
current = 1800;
}
else if (screenW < 1024 && current != 1024)
{
container.empty();
jQuery('div#test').append("1024+\n") ;
console.log('1024');
current = 1024;
}
else if (screenW < 640 && current != 640)
{
container.empty();
jQuery('div#test').append("640+\n") ;
console.log('640');
current = 640;
}
else if (screenW <= 320 && current != 320)
{
container.empty();
jQuery('div#test').append("320+\n") ;
console.log('320');
current = 320;
}
}
而且我离我想要的错误也不远了,它工作得很好。例如 screenW This this fiddle..
【问题讨论】:
-
$(window).on('resize', function() { ==> 我的代码中已经有一个 .on
-
一个不是on。不同的方法
-
对我来说好吧!但它的工作方式与以前相同,刷新时消失的图片不会在调整大小时消失..
标签: javascript jquery resize media-queries