简体   繁体   English

使用屏幕分辨率,输出一些内容

[英]Using the screen resolution, output something

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

<script>

  var f = function(){$('#t').text('Window Width: ' + window.innerWidth)};
f();
$(window).resize(function(){
  f();
});

</script>

<span id="t"></span>

So I have this script so I can find the screen size. 所以我有了这个脚本,所以我可以找到屏幕尺寸。 I want to use the screen size to do this: 我想使用屏幕尺寸来做到这一点:

if screen size is 320px then do this(some code). 如果屏幕尺寸为320px,则执行此操作(某些代码)。

if screen size is 768px then do this(some code). 如果屏幕尺寸为768像素,则执行此操作(某些代码)。

if screen size is more than 1200px then do this(some code). 如果屏幕尺寸大于1200px,则执行此操作(某些代码)。

How can this be achieved? 如何做到这一点?

So I guess you want to do it like this: 所以我想你想这样做:

var width = $(window).width();

if(width <= 320){
} else
if(width <= 768){
} else
if(width > 1200){
}

well for this media queries are best choice, check this link: CSS media queries: max-width OR max-height 对于此媒体查询是最佳选择,请检查此链接: CSS媒体查询:最大宽度或最大高度

you can do with jquery as well: 您也可以使用jquery:

$(window).resize(function(){
  var width = $(this).width();
  if( width == 320){
    f1();
  }else if (width == 768){
    f2();
  }else if (width >= 1200){
    f3();
  }  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM