Comments (9)
For those arriving here through google I want to point out that both listen
and fire
have been removed from the API in v2 of Enquire
from enquire.js.
Yo! Not a noob question at all, don't worry, I'm happy to help.
The simple answer is just call fire
or listen
- this will cause all media queries registered thus far to be evaluated, and if you've called listen
, all media queries will be reevaluated whenever a browser resize/orientation change event is raised. Usually you'll want to do this is a document ready callback e.g.
$(function() {
enquire.register("screen and (min-width:500px)", function() {
alert("wassup?");
}).listen();
});
Now onto the little note! The little note can be explained with the following scenario: Imagine we'd written the code above and had it run in a browser starting at a width of 600px.
On load the query is evaluated, it matches so the alert is shown, and the media query will be considered to be in a matched state. If you then resize the browser up to a width of 700px, the media query will be re-evaluated, it matches again but because the media query is already in a matched state, the callback is not triggered again.
If you then resize the browser down to 400px, the query is re-evaluated, it no longer matches, so is now in an unmatched state (if we'd supplied an unmatch callback this would have been triggered here as the query transitioned from an matched state to a unmatched state). You then resize your browser up to 500px, the media query is re-evaluated, it now matches and because the query transitioned from an unmatched to a matched state the alert is finally shown again.
Hopefully that clears everything up :)
from enquire.js.
Appreciate your quick response!
However, I think I'm missing something about the document ready callback. So here's my function, which triggers fine on change. Am I not calling it correctly? I'm sure it's something really basic I'm missing.
function media_queries() {
enquire.register("screen and (max-width: 959px)", [{
match : function() {
jQuery('#main').append(jQuery('.basics-side'));
},
unmatch : function() {
jQuery('aside').append(jQuery('.basics-side'));
}
}]).listen();
}
jQuery(document).ready(function() {
media_queries();
});
from enquire.js.
Everything looks OK with your code on the surface of it, so maybe there is a bug somewhere. Could you create a jsfiddle with the markup markup and stuff in place, I can dive into it a bit further then.
from enquire.js.
D'oh, in the process of creating the fiddle I figured it out. It actually had to do with multiple media queries. Instead of chaining, I was calling enquire.register two separate times, which resulted in the weird behavior (I had foolishly stripped it out in the code example I gave earlier).
It's in the documentation, but here's the correct way for anyone else who might stumble across this:
enquire.register("screen and (max-width: 768px)", {
// code
}).register("screen and (max-width: 959px)", {
// code
}).listen();
Thanks for your help, and for your awesome work!
from enquire.js.
Haha its often the way, you don't imagine the problem being completely trivial so you don't even consider the simpler parts of your code as the source of error. Glad to hear all is solved though :-)
You're not required to chain methods mind you, so if you encountered issues with unchained methods there may be something for me to investigate yet. If the unchained approach looks like it should work, but strangely doesn't let me know and I'll delve into it.
Thanks for the kind words. For now I'll close this issue.
from enquire.js.
@damassi THANK YOU. I couldn't figure out why this wasn't working...
For those looking for this answer (I didn't see it right away in the documentation, but it's there!) --
you can use setup.
setup : function() {},
from enquire.js.
As of v2, listening is the default, so just registering a query is enough
from enquire.js.
Perhaps I am doing something wrong because it will not load the unmatched case if I open this in a browser wider than 1024px. I should be getting a console log that says "large", but that only happens when I change the browser size. I even tried switching the cases and changing the media query to min-width 1024px.
enquire.register('screen and (max-width:1024px)', {
match: function() {
$('.menu').click(function(){
$('header').toggleClass('open');
$('.nav_items').slideToggle(300)
});
console.log('small')
},
unmatch: function() {
menu.initSlider();
$(window).resize(function() {
menu.onResize();
});
$('header').removeClass('open');
$('.nav_items').removeAttr('style')
console.log('large')
}
});
from enquire.js.
Related Issues (20)
- Unit tests for registered media query callbacks. HOT 1
- Playing nice with Foundation 6 Framework HOT 2
- Enquire.js and Fullpage.js not playing nice HOT 2
- Only matched is fired, why? HOT 1
- Menu HOT 2
- Newest version on npm HOT 3
- matchMedia polyfill error HOT 2
- Can you please publish it with the distribution version on npm
- typescript definitions HOT 1
- Compilation warning (version 2.1.4) HOT 11
- matchMedia polyfill stopped working with latest release HOT 10
- Looking for maintainers HOT 4
- bower.json version mismatch HOT 2
- trigger when page loaded first time
- Error: matchMedia not present, legacy browsers require a polyfill HOT 1
- enquire is undefined HOT 6
- Enquire.js (on Shopify Debut Theme) Breaks Bootstrap Modal function HOT 1
- Not working in latest version of Edge on Windows 10
- How to enable enquire.js for a drupal project
- Inspired by Enquire.js
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from enquire.js.