jQuery调整大小在FireFox,Chrome和Safari上不起作用 [英] jQuery resize not working at FireFox, Chrome and Safari

查看:129
本文介绍了jQuery调整大小在FireFox,Chrome和Safari上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$("#dvMyDIV").bind("resize", function(){
    alert("Resized");
});

$("#dvMyDIV").resize(function(){
    alert("Resized");
});

问题

  1. 为什么这在FireFox,Chrome和Safari上不起作用?
  2. 由于其他浏览器无法处理调整大小,这是否可以视为jQuery错误?
  3. 唯一的解决方法是调用SetTimeout函数来检查clientHeight和clientWidth吗?
  4. 是否有使用jQuery的变通办法?

推荐答案

我相信JavaScript调整大小事件仅适用于框架或窗口,不适用于DIV.

I believe the JavaScript resize event only applies to frames or windows, not to DIVs.

例如参见此页面:

onResize偶数处理程序用于在用户或脚本调整窗口或框架的大小时执行指定的代码.这使您可以查询窗口元素的大小和位置,动态重置SRC属性等.

The onResize even handler is use to execute specified code whenever a user or script resizes a window or frame. This allows you to query the size and position of window elements, dynamically reset SRC properties etc.

因此,如果要检测何时调整窗口大小,则在jQuery中应该使用$(window).resize(function() { });

So if you want to detect when the window is resized, in jQuery you should probably use $(window).resize(function() { });

如果您要观看DIV的大小,则取决于您的意图.如果您要使用JavaScript调整大小,则可以实现一种方法来执行调整大小并让其处理任何其他调整大小代码.

if you want to watch the size of a DIV, it depends on what your intention is. If you're resizing with JavaScript then you could implement a method to perform the resize and have that handle calling any other resize code.

否则,如果您只是在有人调整窗口大小时看着DIV调整大小,那么将调整大小的侦听器附加到窗口上,然后检查DIV是否已调整大小(即存储旧的)就不行了吗?的宽度/高度值,并在调整大小时检查它们)?

Otherwise, if you're just watching for the DIV to resize when someone resizes the window, wouldn't it just work to attach the resize listener to the window and then check if the DIV had been resized (i.e. store the old values of width / height and check them on resize)?

最后,您可以考虑在宽度上使用手表 /height属性,尽管我不知道这是否与浏览器完全兼容(认为这可能仅适用于Mozilla).我确实找到了此jQuery插件,看起来它可能会做同样的事情但是(稍作修改).

Finally, you could consider using watch on the width / height properties, although I don't know whether this is fully browser-compatible (think this might be Mozilla-only). I did find this jQuery plugin which looks like it might do the same thing though (with some slight modification).

这篇关于jQuery调整大小在FireFox,Chrome和Safari上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆