Reputation: 41
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
Upvotes: 4
Views: 32689
Reputation: 2688
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.
Upvotes: 2
Reputation:
please 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' />
Upvotes: 8
Reputation: 641
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.
Upvotes: 2
Reputation: 237855
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.
Upvotes: 6