jQuery .resize() 到特定大小

发布于 2024-12-05 06:04:57 字数 74 浏览 2 评论 0原文

是否可以使用 .resize(); 触发某些内容,但仅在调整到特定宽度或高度时才触发?或者只有当尺寸增加时?

Is it possible to trigger something with .resize(); but only when resized to a particular width or height? Or only when size is increased?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸢与 2024-12-12 06:04:57

如果您向 resize 添加处理程序,您将能够使用 $(this).height()$( 从处理程序内部检查元素的大小this).width()。这样,如果数字不满足某些特征,您可以返回而不需要执行任何操作。

通过记住“最后”值并将它们与处理程序中的当前值进行比较,应该可以检测维度增加,例如:

$("#id").resize(function() {
var height = $(this).height();
var 宽度 = $(this).width();

var heightIncreased = height > lastHeight;
var widthIncreased = width > lastWidth;
var heightDecreased = height < lastHeight;
var widthDecreased = width < lastWidth;

if(heightIncreased || widthIncreased) {
    // whatever
}

lastHeight = height;
lastWidth = width;

});

但是,请注意: 无法保证在调整元素大小的过程中您的调整大小处理程序将被调用多少次(即您可能会连续触发或仅在最后触发一次),如下所示这取决于浏览器。

If you add a handler to resize, you will be able to check the element's size from inside the handler with $(this).height() and $(this).width(). This way you can return without doing anything if the numbers do not satisfy certain characteristics.

Detecting dimension increase should be possible by remembering the "last" values and comparing them with the current ones from within the handler, for example:

$("#id").resize(function() {
var height = $(this).height();
var width = $(this).width();

var heightIncreased = height > lastHeight;
var widthIncreased = width > lastWidth;
var heightDecreased = height < lastHeight;
var widthDecreased = width < lastWidth;

if(heightIncreased || widthIncreased) {
    // whatever
}

lastHeight = height;
lastWidth = width;

});

However, beware: There is no guarantee as to how many times your resize handler will be called during the process of the element being resized (i.e. you may get triggerred continuously or just once at the end), as this depends on the browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文