jquery背景调整大小

时间:2011-12-06 16:54:34

标签: jquery background resize refresh

由于某些原因,如果刷新页面,则无法正常运行。否则工作正常。 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");

1 个答案:

答案 0 :(得分:1)

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

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

您是否拥有在$('document').ready(function(){});内编写的代码?

相关问题