I am creating a mobile first responsive web site that has a few custom jquery plugins which kick in at certain breakpoints.
First, snippets of the code, and then ill explain the issue...
CSS:
@media (min-width: 20em) {
html:after {
content: "320";
} }
@media (min-width: 25em) {
html:after {
content: "400";
} }
@media (min-width: 30em) {
html:after {
content: "480";
} }
GLOBAL JS
var FOO = {
mq: function() {
return parseInt(window.getComputedStyle(document.documentElement,':after').getPropertyValue('content').replace(/['"]/g,''),10);
}
}
JQUERY PLUGIN 1
this.init = function()
{
$(window).on("load resize orientationchange", function() {
if(FOO.mq() >= 480) {
DO SOMETHING
}
});
};
JQUERY PLUGIN 2
this.init = function()
{
$(window).on("load resize orientationchange", function() {
if(FOO.mq() >= 560) {
DO SOMETHING ELSE
}
});
};
Now the problem with putting the FOO.mq() in the jquery plugin is that it will fire the FOO.mq() function for every DOM object found (e.g. if the plugin does something to 10 images it will fire the FF.mq() 10 times) where really thats not needed as we only need to check once.
I have tried putting the resize/return value in a global object but it nevers seems to work and gets picked up bu the jquery plugins.
Any pointers would be greatly appreciated.
Thanks
windowunless you have event with the same type bubbling somewhereFOO.mq()to calculate its stuff once (and whenever a relevant event occurs) rather than on demand?FOO.mqchanging?