window.matchMedia triggers too often (false positives) - javascript

I’m setting up multiple media queries like:
for (var layoutId in layoutList) {
console.log('Set up MediaQuery [%s]:', layoutId, getCssMediaQueryDefinition(layoutId));
var mql = window.matchMedia(getCssMediaQueryDefinition(layoutId));
mql.removeListener(onMatchMediaChange);
mql.addListener(onMatchMediaChange);
}
var onMatchMediaChange = function (event) {
console.log('onMatchMediaChange [%s]:', _.get(getLayoutForCssMediaQuery(event.media), 'id'), window.innerWidth, event.media);
};
When I start the app, the console looks like:
Set up MediaQuery [phone]: (max-width: 512px)
Set up MediaQuery [tablet]: (max-width: 1002px) and (min-width: 512px)
...which looks correct.
However, when I resize the window, this happens (console statements):
onMatchMediaChange [tablet]: 859 (max-width: 1002px) and (min-width: 512px)
onMatchMediaChange [phone]: 436 (max-width: 512px)
onMatchMediaChange [tablet]: 436 (max-width: 1002px) and (min-width: 512px)
The first two logs are correct, but the third is not: for some reason the media query for "tablet" falsely triggers immediately after "phone".
Any pointers what could be wrong?

Related

Is it possible to resize browser window size using javascript? [duplicate]

This question already has answers here:
Can I resize the browser window?
(4 answers)
Closed 6 years ago.
I want to resize my browser window using Javascript to test my responsive webpage design functionality.
But I didn't find any option for the same.
What I found is, we can modify window size for newly pop up window as shown below:
<body>
<p>Open a new window, and resize the width and height to 500px:</p>
<button onclick="openWin()">Create window</button>
<button onclick="resizeWin()">Resize window</button>
<script>
var myWindow;
function openWin() {
myWindow = window.open("", "", "width=100, height=100");
}
function resizeWin() {
myWindow.resizeTo(250, 250);
myWindow.focus();
}
</script>
</body>
I have defined some responsive behaviour using following css code:
#media screen and (max-width: 500px){
//style changes
}
Can we resize browser window where our page is launched? Or it is not possible?
To resize the window, you can simply do this:
window.resizeTo(width, height);
You likely can use resizeBy/resizeTo but why don't you want to use Chrome built-in tester?
You can open developer console cmd/ctrl + alt + I and click on Device Toolbar or cmd/ctrl + shift + M and adjust screen size using predefined templates or create your own
Update
For automation test there are a several ways to adjust it. Which runtime is running your Jasmine tests? For PhantomJS you can use
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = {
width: 480,
height: 800
};
More info here
There are 2 options to implement responsive layout structure :
by using javascript..
function ResponsiveLayout() {
if (screen.width <= 1440) {
$('#content').addClass("pipeline1");
}
else if (screen.width > 1440 && screen.width <= 1600) {
$('#content').addClass("pipeline2");
}
else if (screen.width > 1600) {
$('#content').addClass("pipeline3");
}
};ResponsiveLayout()
By using css3 media query :
#media (min-width: 1200px){css attrabutes}
#media (max-width: 1199px) and (min-width: 981px){css attrabutes}
#media (max-width: 980px) and (min-width: 641px){css attrabutes}
#media (max-width: 640px) and (min-width: 481px){css attrabutes}
#media (max-width: 480px) and (min-width: 321px){css attrabutes}
#media (max-width: 320px){css attrabutes}
Professionally Media query would be better option but can we use javascript where limited access of css file or inline css -:)

zslider javascript should work only in screen resolution below 768px

i want to use zslider javascript in my website, but can any one tell me how to make it work only in screen resolution below 768px
Here is the zslider script link:
http://www.cssscript.com/demo/mobile-friendly-content-carousel-slider-with-pure-javascript-zslider/
Use media queries to show slider content below 768px:
#media (min-width: 768px) {
.z-slide-wrap {
display: none;
}
}
Than use JavaScript to initialize slider only if width is below 768px:
if (window.innerWidth <= 768) {
var slider1 = new Slider('#demo', '.z-slide-item', {
// OPTIONS HERE
});
}

conditionally load js file with screen width

I have the below java-script which loads the screen as per the resolution and js file.
But I have to refresh the browser to reload the js file as per resolution otherwise it keeps the last resolution js file.
Please help me to understand how to load both at once.
window.requestAnimFrame = (function(){
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function( callback ){ window.setTimeout(callback, 1000 / 60); };
})();
var width = screen.width,
height = screen.height,
checkScreenSize = function () {
if (screen.width !== width || screen.height !== height) {
width = screen.width;
height = screen.height;
$(window).trigger('resolutionchange');
}
};
(function loop(){
requestAnimFrame(loop);
checkScreenSize();
})();
function includeJS(incFile) {
document.write('<script type="text/javascript" src="'+ incFile+ '"></scr' + 'ipt>');
}
if (window.matchMedia("only screen and (min-width: 1240px) and (max-width: 1280px)").matches) {
includeJS('changer/js/changer-1280.js');
} else if (window.matchMedia("only screen and (min-width: 1390px) and (max-width: 1440px)").matches) {
includeJS('changer/js/changer-1440.js');
} else if (window.matchMedia("only screen and (min-width: 1441px) and (max-width: 1441px)").matches) {
includeJS('changer/js/changer-1441.js');
}
Sounds like you want to watch the window resize event maybe? Something like this:
$(window).on('resize', function() {
if (window.matchMedia("only screen and (min-width: 1240px) and (max-width: 1280px)").matches) {
$.getScript('changer/js/changer-1280.js');
} else if (window.matchMedia("only screen and (min-width: 1390px) and (max-width: 1440px)").matches) {
$.getScript('changer/js/changer-1440.js');
} else if (window.matchMedia("only screen and (min-width: 1441px) and (max-width: 1441px)").matches) {
$.getScript('changer/js/changer-1441.js');
}
});
Since you're using jQuery anyway, you can use its $.getScript instead of injecting the script element manually.
I see there's some code there that seems to watch the window height and width to implement a custom window resize event. That's not really necessary though. I think you would especially not want to do that during a RAF loop like that since it's probably triggering a layout during every single frame.
Running those matchMedia checks every time the window's resize event fires will bog down resizing performance too, so you should debounce and only handle the event after there's a pause in resizing. Something like this:
var resizeTimer;
$(window).on('resize', function() {
clearTimeout(resizeTimer);
// Wait half a second before reacting to the resize event,
// in case the user is still actively resizing the window.
resizeTimer = setTimeout(handleWindowResize, 500);
});
function handleWindowResize() {
if (window.matchMedia("only screen and (min-width: 1240px) and (max-width: 1280px)").matches) {
$.getScript('changer/js/changer-1280.js');
} else if (window.matchMedia("only screen and (min-width: 1390px) and (max-width: 1440px)").matches) {
$.getScript('changer/js/changer-1440.js');
} else if (window.matchMedia("only screen and (min-width: 1441px) and (max-width: 1441px)").matches) {
$.getScript('changer/js/changer-1441.js');
}
}

Responsive design + jQuery fine except Firefox

I'm using some pretty basic jQuery to force things into position when they are moved around:
function ipad() {
var width = jQuery(window).width();
if (width < 1200 && width >= 768){ //if tablet
jQuery('.mobbut').css('width', '33.333%');
jQuery('#re2').css('max-width', '');
} else {
jQuery('.mobbut').css('width', '100%');
jQuery('#re2').css('max-width', '400px');
}
if (width < 768){ //if mobile phone
jQuery('._btnselect').hide();
} else {
jQuery('._btnselect').show();
}
}
jQuery(document).ready(function() {
ipad();//run when page first loads
});
jQuery(window).resize(function() {
ipad();//run on every window resize
});
jQuery(window).load(function() {
ipad();//run when page is fully loaded
});
So far my website is perfect on Chrome, Safari, iPad, iPhone, but for some reason it looks a mess when you resize your Firefox window to smaller than 1200px wide. Try for yourself here's the webpage. Is it something to do with the jQuery or more the layout of the page? I inherited this homepage from another designer and it was previously built on tables so this may be giving FF problems.
I tried using media queries and they didn't work, I had to turn to jQuery to get the results.
If they didn't work, it's probably because the syntax was incorrect. Try it like this, which is pretty much exactly what your jQuery is doing, but much simpler:
#re2 {
max-width: 400px;
}
.mobbut {
width: 100%;
}
#media only screen and (max-width: 767px) {
._btnselect {
display: none;
}
}
#media only screen and (min-width: 768px) and (max-width: 1199px) {
#re2 {
max-width: none;
}
.mobbut {
width: 33.333%;
}
}
--EDIT--
Since it looks like you're trying to target the iPad. Try these media queries, from this post:
/* iPads (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}
/* iPads (landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
/* iPads (portrait) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* Styles */
}
These will likely work even more reliably than your jQuery adjustments since they are based on the device-width rather than the page width.
I ran your site with firebug (I do recommend using it) enabled and the error is in the following function
** function futsal(){
var wid = jQuery(window).width();
if (wid <= 1024 && width >= 768){ //if tablet **
just there you declare the var wid and then test against width
I agree with all of the comments above though, use media queries. It's far easier.

get css value on media query change via enquire.js

enquire
.register('screen and (min-width: 1200px)', {
match: function() {
active_li_margin_left = $('li.active').css('margin-left')
active_li_width = $('li.active').width()
console.log(active_li_margin_left, active_li_width)
zwarovsky();
}
})
.register('screen and (min-width: 980px) and (max-width: 1199px)', {
match: function() {
active_li_margin_left = $('li.active').css('margin-left')
active_li_width = $('li.active').width()
console.log(active_li_margin_left, active_li_width)
zwarovsky();
}
})
li.active width change from 770px to 620px, where margin-left from -30px to -20px, this are bootstrap span8 items.
the problem:
seems that enquire fire the functions just before the css media query, so it pickup the previous values when you resize the window.
when i switch from 1200 to 980 resizing the browser window console log show 30px 770 instead 20px 660, and than when i switch back from 980 to 1200 console log show 20px 620.

Categories