7
I need a javascript code to change the class menu
for the class responsivemenu
when the screen resolution is less than 750px
.
7
I need a javascript code to change the class menu
for the class responsivemenu
when the screen resolution is less than 750px
.
15
Note that I am checking the size inside the block window.addEventListener('resize', function() {..});
, the function will be called every time the screen resolution is changed.
window.addEventListener('resize', function () {
//var altura = window.innerHeight;
var largura = window.innerWidth;
if (largura < 750)
document.getElementsByClassName('menu')[0].className = 'responsivemenu';
});
The simplest way using Media Query is to define specific style sheets for each device type, so just add the property media
to set to what type of device your css
will be used.
Utilize handheld
for handheld devices, mobile phones and other devices of this profile. Usually with small screens and limited bandwidth.
<link rel="stylesheet" href="estilo.css" type="text/css" media="handheld">
Now the way I believe to be more correct, in your style file you will use conditionals to define the style for each resolution you want.
/* Estilo padrão */
menu {
color: blue;
}
/* Estilo para resolução menor que 750 pixels */
@media screen and (max-width: 750) {
menu {
color: green;
}
}
Inside the block you will define the specific style for the resolution.
Source: Introduction about Media Queries
That should have been the one
This is the right solution!
great answer! I will use more js without jquery.
great answer, you could include an alternative using media query
, perhaps the AP does not know this resource and this could benefit him (as well as the community).
Bah @Tobymosque media query
just know it exists. I’ll give a read on and anything edits the answer.
3
There are several ways to do this.
With Jquery, for example, it can be done like this:
if ($(window).width() <= 750)
$('.menu').removeClass('menu').addClass('responsivemenu');
// ou
$('.menu').attr('class','responsivemenu');
There are several ways to do this operation.
If you want to delve into this method I have presented, take a look at: http://goo.gl/F26YSx
2
Buddy, try the following form.
if(window.screen.availWidth<750){
$('.menu').addClass('responsivemenu').removeClass('menu');
}
But I need this to be executed only with a resolution less than 750px for example.
if at the beginning of the code already guarantees that the script will only run below 750px. pq gave me a negative point? the correct code ta
I didn’t give negative point @Rboschini. I gave positive point, I don’t know who gave negative point. I even used your code and it worked.
Wonderful then, good that it worked! needing help is only to call! hugs
Thank you so much for your time and kindness! ;)
the -1
possibly due to the Amigão, usa o jquery.
, to understand see the following topic: When is "use jQuery" not a Valid Answer to a Javascript Question?
Tobymosque thanks, I’ll pay more attention. ;)
I don’t want to open a troubled discussion about this, but we can see in the debate of the link (this META) that in general, even if the AP has put Javascript in the title, present solutions with jquery could still be valid, since the "solution" expected can also be done with the lib in question. Anyway, it is undeniable that Pedro’s answer is chosen as the "best answer", which besides being simple, solves the problem very well and is still in pure JS. But let’s face it, it’s relative to deny the "convenience" of using Jquery. What do you think? [let’s be friendly, Please,rs]
stackoverflow brazil is more mimimi than technology, unfortunately.
Browser other questions tagged javascript html css
You are not signed in. Login or sign up in order to post.
You cannot do this with CSS (media query?)
– Sergio
I believe it’s the best option as well. Make sure you can’t perform this operation directly in CSS. Here is an example for you to study: http://goo.gl/LJFKRp
– thelimarenan