Home › Forums › JavaScript › Hide Div if Screen is Smaller than 1024
- This topic is empty.
-
AuthorPosts
-
November 27, 2010 at 11:03 am #30823
dzulfriday
Memberhi there..
i want to hide a floating div if the user screen is < 1024 as it will overlay with my blog content area. i found this jquery on the net i am not sure how to use it.
$(document).ready(function() {
if ((screen.width>1024)) {
// if screen size is 1025px wide or larger
$(".yourClass").css('display', 'none'); // you can also use $(".yourClass").hide();
}
elseif ((screen.width<=1024)) {
// if screen size width is less than 1024px
$(".yourClass").css('display', 'block'); // here you can also use show();
}
});my first question is, if my floating div class name is sharecontent, should i replace the above script like below? it yes, its not working.
$(document).ready(function() {
if ((screen.width>1024)) {
// if screen size is 1025px wide or larger
$(".sharecontent").css('display', 'none'); // you can also use $(".yourClass").hide();
}
elseif ((screen.width<=1024)) {
// if screen size width is less than 1024px
$(".sharecontent").css('display', 'block'); // here you can also use show();
}
});Second, where should i place this code in wordpress header? Above
or below
November 27, 2010 at 11:16 am #72156TT_Mark
MemberHave you included JQuery in your PHP file? The code needs to be placed after the JQuery file inclusion
November 27, 2010 at 11:48 am #72149dzulfriday
Memberyes…now the div is hide no matter what my resolution is…there must’ve been wrong somewhere..
November 27, 2010 at 12:26 pm #72144TT_Mark
MemberHmm….well I’d suggest removing the
elseif ((screen.width<=1024))
and just having
else
Are you setting the div to display or hide by default?
November 27, 2010 at 12:29 pm #72125dzulfriday
Memberhow to set it by default?
November 27, 2010 at 12:33 pm #72126dzulfriday
Membermistype there:
“yes…now the div is hide no matter what my resolution is…there must’ve been wrong somewhere..”i meant to type show
why the jquery is like not working?
By the way, here i attach the class sharecontent css
.sharecontent {
background:none repeat scroll 0 0 #FFFFFF;
border:1px solid #FFFFFF;
left:0;
margin:0 0 0 20px;
position:fixed;
top:221px;
width:60px;
}does the position: fixed cannot be hidden by jquery????
November 27, 2010 at 1:26 pm #72097TT_Mark
MemberDid you remove the elseif and replace with just an else?
November 27, 2010 at 1:36 pm #72098dzulfriday
Memberyup…still not working… sigh :(
November 27, 2010 at 3:18 pm #72101heysep
Memberwhy do you want to know the screen size?
isn’t an approach via window object what you should be doing?$(window).width()
November 28, 2010 at 2:30 am #72020dzulfriday
Memberi replaced my code above using
$(window).width()
you gave above and it still not working.
November 28, 2010 at 11:27 am #71981heysep
Membercan you link to the related working copy of your site?
November 28, 2010 at 1:35 pm #71986jamygolden
MemberI don’t actually think javascript is necessary for this. What about css media queries?
November 28, 2010 at 7:16 pm #71901jfreehill
MemberCSS Media queries are slick, although not supported in IE 8 and lower. Chris made an article that includes a jQuery (& raw Javascript) method for what you’re trying to do that would be supported in any browser (if you care): https://css-tricks.com/resolution-specific-stylesheets/
You’ll want to replace the if statement in this with yours and replace your screen.width with just width . By the way with your code, you’re actually telling it to hide the div if the browser width is above 1024, rather than vice versa, so you’ll want to flip that.
function adjustStyle(width) {
width = parseInt(width);
if (width < 701) {
$("#size-stylesheet").attr("href", "css/narrow.css");
} else if ((width >= 701) && (width < 900)) {
$("#size-stylesheet").attr("href", "css/medium.css");
} else {
$("#size-stylesheet").attr("href", "css/wide.css");
}
}
$(function() {
adjustStyle($(this).width());
$(window).resize(function() {
adjustStyle($(this).width());
});
});November 28, 2010 at 8:16 pm #71902dzulfriday
Memberbut i want this to be applied at all browser, if the css media queries doesnt work in IE, then it will not solve my problem.
by the way jfreehill, i am telling it to hide the div if the browser width is above 1024 on purpose…because i am tired keep changing my monitor resolution for this testing, so if its hidden at the > 1024, then i know its working. i will flip that later.
November 29, 2010 at 12:14 am #71906jamygolden
Member@dzulfriday read the media queries article. It is awesome and it will solve some of your questions. The snippet above that @jfreehill pasted is from this article which was linked to from the media queries article while discussing browser compatibility.
-
AuthorPosts
- The topic ‘Hide Div if Screen is Smaller than 1024’ is closed to new replies.