开发者

Get browser width and hide element depending on size?

Is it possible to detect what width a browser window is and hide an element on page if its lower than 1024?

In JQuery perhaps? :-)开发者_开发百科


You can accomplish this using a CSS media query.

@media (max-width: 1024px) {
  #someElement { display:none; }
}

Some older browsers don't support media queries, so you could use jQuery to accomplish this as well. This example will show/hide the element when the window resizes

<div id="someElement">SOME ELEMENT</div>

<script>
  $(function() {
    $(window).bind("resize", function() {
      $('#someElement').toggle($(this).width() >= 1024);    
    }).trigger("resize");
  });
</script>


if ($(window).width() < 1024) {
    $('#someElement').hide();
}

or:

$('#someElement').toggle($(window).width() >= 1024);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜