admin管理员组文章数量:1203411
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.
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.
There's prob some javascript projects for doing this I guess but I haven't managed to find any...
This is the content I wish to change main div id will also change.
Here is an example of something like what needs t be done.
function shareStuff() {
if ($(window).width() < 1024) {
<div id="sharepost"><div class="sharer"><a href="" 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=".js"></script></div>
<div class="sharer"><a name="fb_share" type="box_count" href=".php?u=<?php the_permalink(); ?>&t=<?php the_title(); ?>">Share</a><script src=".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="" data-button-style="normal-count" data-url="<?php the_permalink(); ?>"></a><script type="text/javascript" src=".js" defer="defer"></script></div><div class="sharer"><script src=".php?s=3"></script></div><span class="st_email" ></span>
</div>
} else {
<div id="sharepost"><div class="sharer"><a href="" 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=".js"></script></div>
<div class="sharer"><a name="fb_share" type="horizontalk" href=".php?u=<?php the_permalink(); ?>&t=<?php the_title(); ?>">Share</a><script src=".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="" data-button-style="normal-count" data-url="<?php the_permalink(); ?>"></a><script type="text/javascript" src=".js" defer="defer"></script></div><div class="sharer"><script src=".php?s=3"></script></div><span class="st_email" ></span>
</div>
} }
Thanks in advance
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.
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.
There's prob some javascript projects for doing this I guess but I haven't managed to find any...
This is the content I wish to change main div id will also change.
Here is an example of something like what needs t be done.
function 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
Share Improve this question edited Nov 10, 2010 at 18:33 Mark asked Nov 10, 2010 at 13:55 MarkMark 412 gold badges2 silver badges5 bronze badges 3- contents in square and in rectangle are the same? It's only a matter of positioning? – fcalderan Commented Nov 10, 2010 at 13:59
- It's generally a good idea to go with fluid layouts in this case – HyderA Commented Nov 10, 2010 at 14:20
- The rectangle and square contents are completely different HTMl codes. Thanks – Mark Commented Nov 10, 2010 at 14:24
4 Answers
Reset to default 8please don't use javascript if not necessary, media queries can help
<!-- 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' />
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.
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.
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.
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....
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...
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.
本文标签: javascriptChange div content if screen resolution is less thanStack Overflow
版权声明:本文标题:javascript - Change div content if screen resolution is less than? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738661098a2105427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论