javascript 如果浏览器宽度大于 1000 像素,则显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5520981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:28:21  来源:igfitidea点击:

If browser width is greater that 1000px, show images

javascriptbrowserwidth

提问by rabbitt

Is it possible to get the width of the browser web view and act on it if it is greater than 1000px?

如果大于 1000 像素,是否可以获取浏览器 Web 视图的宽度并对其进行操作?

Example (in pseudocode): If browser width is greater that 1000px;

示例(伪代码):如果浏览器宽度大于 1000px;

<div style="position:fixed; top:0; left:0; z-index:1; visibility:visible;">
<img src="images/borderleft.png">
</div>

回答by Alan

Modern Way: Media Queries

现代方式:媒体查询

<div class="showWide">
<img src="images/borderleft.png">
</div>

// in your css file
.showWide {
   position:fixed; 
   top:0; 
   left:0; 
   z-index:1;
   visibility: hidden;
}

@media only screen and (min-width: 1000px) {
    .showWide {
        visibility: visible;
     }
}

2011 Way: JQuery

2011 方式:JQuery

// returns width of browser viewport
if( $(window).width() > 1000)
{
  //add or unhide image.
}  

And for more information...jquery width() documentation.

有关更多信息... jquery width() 文档