26

我编写了一个非常基本的脚本来在加载或调整窗口大小时添加/删除一个类。

我只是想知道是否有更好的方法可以做到这一点,或者是否可以减少代码行。

基本上,我希望能够在较小的屏幕上查看网站时更改样式。我认为最好在html标签低于一定宽度时向它添加一个新类......

无论如何,这是我的代码。

<script type="text/javascript">
 $(document).ready( function() {
    /* Check width on page load*/
    if ( $(window).width() < 514) {
     $('html').addClass('mobile');
    }
    else {}
 });

 $(window).resize(function() {
    /*If browser resized, check width again */
    if ($(window).width() < 514) {
     $('html').addClass('mobile');
    }
    else {$('html').removeClass('mobile');}
 });

谢谢

阿娇

4

7 回答 7

56

好吧,我知道我参加聚会迟到了,但我看到一些类似的事情$(document).ready()并不是真正必要的。

如果您一遍又一遍地调用它们,请尝试缓存您的选择器,var $window = $(window);这将有助于提高性能。我使用一个函数表达式来封装我不在全局范围内,但仍然可以访问我的$window$html缓存的 jQuery 选定元素。

(function($) {
    var $window = $(window),
        $html = $('html');

    $window.resize(function resize(){
        if ($window.width() < 514) {
            return $html.addClass('mobile');
        }

        $html.removeClass('mobile');
    }).trigger('resize');
})(jQuery);

http://jsfiddle.net/userdude/rzdGJ/1

可能更干净一点,更容易理解:

(function($) {
    var $window = $(window),
        $html = $('html');

    function resize() {
        if ($window.width() < 514) {
            return $html.addClass('mobile');
        }

        $html.removeClass('mobile');
    }

    $window
        .resize(resize)
        .trigger('resize');
})(jQuery);

http://jsfiddle.net/userdude/rzdGJ/2

于 2012-06-15T09:11:21.300 回答
16

使用媒体类

@media screen and (max-width: 900px) {
  .class {
    width:800px;

  }
}

@media screen and (max-width: 500px) {
      .class {
        width:450px;

  }
}
于 2012-06-15T09:03:50.717 回答
15

首先,使用函数干燥(不要重复自己)您的代码:

function checkWidth(init)
{
    /*If browser resized, check width again */
    if ($(window).width() < 514) {
        $('html').addClass('mobile');
    }
    else {
        if (!init) {
            $('html').removeClass('mobile');
        }
    }
}

$(document).ready(function() {
    checkWidth(true);

    $(window).resize(function() {
        checkWidth(false);
    });
});
于 2012-06-15T09:05:20.027 回答
8
function resize() {
    if ($(window).width() < 514) {
     $('html').addClass('mobile');
    }
    else {$('html').removeClass('mobile');}
}

$(document).ready( function() {
    $(window).resize(resize);
    resize();
});
于 2012-06-15T09:02:48.680 回答
4

你也可以使用这种方法我认为它很清楚:

$(window).on('resize', function(){
      var win = $(this);
      if (win.width() < 514) { 

      $('html').addClass('mobile');

      }
    else
    {
        $('html').removeClass('mobile');
    }

});

简单方法

于 2014-09-26T18:19:03.523 回答
2

这个问题实际上困扰了我一段时间。我通常有多种尺寸用于过渡。我写了这个我想分享的想法:

$(function() {
  var pageTransitions = [['full',1600],['mobile',800],['tiny',400],['micro',0]]; // number shows minimum size - must be from high to low
  function resize() {
    var target = 0,
        w = $(window).width(),
        h = $('html');
    $.each(pageTransitions, function(index, pageTransition) {
        if( w > pageTransition[1]) {
            target = index;
            return false;
        }
    });
    $.each(pageTransitions, function(index, pageTransition) {
        h.removeClass(pageTransition[0]);
    });
    h.addClass(pageTransitions[target][0]);
  }
  resize();
  jQuery(window).on('resize', function() {
    resize();
  });
});

http://jsfiddle.net/mckinleymedia/ERZ3q/7/

于 2014-01-09T00:56:08.827 回答
0

尝试这个。这个对我有用

function resize() {
        if ($(window).width() < 514) {}
        else {}
    }

    $(document).ready( function() {
        $(window).resize(resize);
        resize();
    });
于 2018-07-25T17:12:51.323 回答