Javascript 如果屏幕分辨率小于,则更改 div 内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4145190/
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
Change div content if screen resolution is less than?
提问by Mark
I have some floating elements to my site that won't be seen if the screen res is 1024X768 and below. So I want to give people with that screen res a different placement of that content and it will be slightly different code for the content too.
我的网站上有一些浮动元素,如果屏幕分辨率为 1024X768 及以下,则不会看到这些元素。因此,我想为使用该屏幕的人提供该内容的不同位置,并且内容的代码也会略有不同。
Say the people with 1024 and above will get a large square floating down the left hand side of the page and the people below will get a rectangle positioned above the main content.
假设 1024 及以上的人将在页面左侧看到一个大正方形,而下面的人将在主要内容上方看到一个矩形。
There's prob some javascript projects for doing this I guess but I haven't managed to find any...
我猜有一些 javascript 项目可以这样做,但我还没有找到任何......
This is the content I wish to change main div id will also change.
这是我希望更改的内容,主 div id 也会更改。
Here is an example of something like what needs t be done.
这是一个类似于需要做什么的例子。
function shareStuff() {
函数shareStuff() {
if ($(window).width() < 1024) {
<div id="sharepost"><div class="sharer"><a href="http://twitter.com/share" class="twitter-share-button" data-url="<?php the_permalink(); ?>" data-text="<?php the_title(); ?>" data-count="vertical" data-via="code_love" data-related="love:code">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div>
<div class="sharer"><a name="fb_share" type="box_count" href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>&t=<?php the_title(); ?>">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript" defer="defer"></script></span>
</div><div class="sharer"><a title="Post on Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="normal-count" data-url="<?php the_permalink(); ?>"></a><script type="text/javascript" src="http://www.google.com/buzz/api/button.js" defer="defer"></script></div><div class="sharer"><script src="http://www.stumbleupon.com/hostedbadge.php?s=3"></script></div><span class="st_email" ></span>
</div>
} else {
<div id="sharepost"><div class="sharer"><a href="http://twitter.com/share" class="twitter-share-button" data-url="<?php the_permalink(); ?>" data-text="<?php the_title(); ?>" data-count="horizontal" data-via="code_love" data-related="love:code">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div>
<div class="sharer"><a name="fb_share" type="horizontalk" href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>&t=<?php the_title(); ?>">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript" defer="defer"></script></span>
</div><div class="sharer"><a title="Post on Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="normal-count" data-url="<?php the_permalink(); ?>"></a><script type="text/javascript" src="http://www.google.com/buzz/api/button.js" defer="defer"></script></div><div class="sharer"><script src="http://www.stumbleupon.com/hostedbadge.php?s=3"></script></div><span class="st_email" ></span>
</div>
} }
Thanks in advance
提前致谢
回答by
please don't use javascript if not necessary, media queries can help
如果没有必要,请不要使用 javascript,媒体查询可以提供帮助
<!-- here provide css for all (also > 1024px) cases -->
<link rel='stylesheet' type='text/css' href='allresolution.css' />
<!-- here overwrite css for small devices -->
<link rel='stylesheet' type='text/css' media="all and (max-width:1024px)" href='lessthan1024.css' />
回答by lonesomeday
You can't detect screen resolution, but you can detect the browser size:
您无法检测屏幕分辨率,但可以检测浏览器大小:
if ($(window).width() < 1024) {
// code for small viewports
} else {
// code for large viewports
}
This should be sufficient for your needs.
这应该足以满足您的需求。
回答by Siedrix
You youd need to load a different css stylesheet for every situation, you can do this by checking the screen.width on load and on resize.
您需要为每种情况加载不同的 css 样式表,您可以通过在加载和调整大小时检查 screen.width 来做到这一点。
Something like this.
像这样的东西。
if(screen.width==1024)
{
document.write("<link rel='stylesheet' type='text/css' href='style_1024.css' />"); // this css runs in only 1024 resulation
}
else
{
document.write("<link rel='stylesheet' type='text/css' href='style_other.css' />"); // for other resulatiom
}
And you would need to remove the other stylesheet if the window change from one to the other.
如果窗口从一个更改为另一个,您将需要删除另一个样式表。
回答by user2060451
Just had the same problem. I mixed some of the answers above.
刚刚有同样的问题。我混合了上面的一些答案。
<div class="someclass" id="hidden_1" style="display:none"> Something very big for big screen</div>
<div class="someclass" id="hidden_2" style="display:none"> small</div>
@media only screen and (min-width: 1450px)
{
#hidden_1 {display: block !important}
}
@media only screen and (min-width:1281) and (max-width:1450)
{
#hidden_1 {display: block !important}
}
@media only screen and (min-width:981px) and (max-width:1024px)
{
#hidden_2 {display: block !important}
}
You only need to make sure you fill out ALL possible screens ! Otherwise there will be no text. My problem was with 1024px screen....
您只需要确保填写所有可能的屏幕!否则不会有文字。我的问题是 1024px 屏幕....
I tried to have one div normal and then hide it with the CSS !important, and it didn't work on chrome. I am new to computer...
我试图让一个 div 正常,然后用 CSS !important 隐藏它,但它在 chrome 上不起作用。我是电脑新手...
Finally, I had to create one media query for (min1450px). If I put a general section of code that should be overwritten by the specific media query, this general section (with no media query) was overwriting the more specific ones. So I tacked the hidden div in a media query that would target all computers with min query @media only screen and (min-width: 1450px). Than the OS/browsers started to apply the right media query.
最后,我必须为 (min1450px) 创建一个媒体查询。如果我放了一段应该被特定媒体查询覆盖的通用代码部分,这个通用部分(没有媒体查询)会覆盖更具体的代码。因此,我在媒体查询中添加了隐藏的 div,该查询将针对所有具有 min 查询 @media only screen 和 (min-width: 1450px) 的计算机。比操作系统/浏览器开始应用正确的媒体查询。