【问题标题】:jquery background resizejquery背景调整大小
【发布时间】:2012-01-14 06:39:46
【问题描述】:

由于某种原因,如果页面被刷新,这将无法正常运行。否则工作正常。 id.background 在 body 标签上。

    var theWindow = $(window),
      $bg = $("#background"),
      aspectRatio = $bg.width() / $bg.height();

    function resizeBg() {
      if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
        $bg.removeClass()
           .addClass('bgheight');
      } else {
        $bg.removeClass()
           .addClass('bgwidth');
      }
    }

    theWindow.resize(function() {
      resizeBg();
    }).trigger("resize");

【问题讨论】:

  • 您是否在文档就绪事件中触发调整大小?例如,$( document ).ready( function() { theWindow.trigger( 'resize' ); } );
  • 是的,prob 应该添加的。

标签: jquery background resize refresh


【解决方案1】:

你可能想看看这个插件,我已经用过几次了。

http://bavotasan.com/2011/full-sizebackground-image-jquery-plugin/

你有你在$('document').ready(function(){});中写的代码吗?

【讨论】:

  • 是的,prob 应该添加的。我去看看链接,谢谢。
  • 考虑尝试“窗口”
  • @mkprogramming - OP 将 $( window ) 分配给 theWindow。如果您认为 $( window ).ready() 会有所帮助,那就不会了 - 窗口会引发 onload 事件
  • 谢谢,没看懂那部分。添加 $('document').ready() 有帮助吗?
猜你喜欢
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多