0
Running Js code when screen size is in a certain size
I'm trying to run some javascript code when the screen size is in a certain size... Well i tried with jquery: $(window).width(), well it does work but i have to refresh with page for it .... Please help('Javascript')
6 Réponses
+ 1
Calviղ such an intereting thing to try ... Thansk for answering bro
0
Have you try to run the code in window.onload ?
0
Use matchMedia function to detect:
var mql = window.matchMedia('(max-width: 600px)');
mql.addListener(function(e) {
if(e.matches) console.log('screen smaller or equal to 600px') ;
else console.log('Screen size larger than 600px');
} );
https://code.sololearn.com/WBKZ4jpDuwzB/?ref=app
0
Taste. No, I haven't but i'll try ... Thanks for helping me bro
0
You could also wrap it inside resize method
$(window).resize(function() {
if($(this).width() > 600) return;
alert(”smaller than 600”);
});
0
Toni Isotalo, I tried that too
But it didn't work for me.
thanks for answering
$(document).ready(function(){
$(window).resize(function(){
// code here
});
});