【问题标题】:Window resize function stops working on resize窗口调整大小功能停止调整大小
【发布时间】:2019-01-18 11:33:30
【问题描述】:

我正在使用以下 jQuery 脚本来保持 div 在不同窗口大小上的纵横比。

$(window).resize(function() {
var ratio = 0.53;
$('.uitgelicht-item').width($(window).width() * ratio).height($(window).height() * ratio);
// instead of directly using "$(window).width() * ratio", you can call a method to
// calculate width and height each time window is resized.

});

当我加载页面时它可以工作,但是一旦我调整窗口大小(或转动手机/平板电脑屏幕),比例就会丢失。

如何让这个脚本在我的窗口调整大小时保持活动状态?

【问题讨论】:

  • 为什么不直接使用纯css,这样会干净得多?
  • 此调整大小事件应该始终有效,但您可能想知道某些设备(您提到移动设备和平板电脑)确实会根据方向返回“错误”的宽度或高度,因此可能是您的尺寸不变。这是一篇冗长但值得一读的文章:tripleodeon.com/2011/12/first-understand-your-screen
  • 我正在使用 3 个浮动 div:.uitgelicht-item { width: 30%;溢出:隐藏;右边距:2%;背景:#FFF;填充:3px;向左飘浮; } 这可能是问题吗?

标签: jquery html aspect-ratio


【解决方案1】:

您的脚本在调整窗口大小时保持活动状态。请检查此fiddle 我无法重现您所说的问题。

$(window).on('resize', function() {
var ratio = 0.53, div = $('.uitgelicht-item');

div.width($(window).width() * ratio).height($(window).height() * ratio);
div.text('width = ' + div.width() + ',height = ' + div.height())
// instead of directly using "$(window).width() * ratio", you can call a method to
// calculate width and height each time window is resized.

});

【讨论】:

  • 脚本确实有效,结果我不需要它。加载时我的 css 很好,但是当调整 $(window).resize(function() 脚本的大小时,脚本开始工作,又搞砸了。几周前我添加了这个脚本。不知道为什么我认为我需要它,但我觉得现在很愚蠢。抱歉,感谢您的意见
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 2019-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多