Managing Responsive Breakpoints in Javascript - Jonathan Fielding. Responsive design is increasingly popular with separate mobile/desktop sites being replaced by a hybrid approach where websites are able to respond to the users needs. In responsive design, media queries are used to allow sites to change the appearance of the page based on the browser thing like browser width and browser height. To enable support for media queries in older browsers we simply include respond.js in the page. Unfortunately things are not quite as simple in javascript, browsers do not come with a simple way to add javascript to a specific state so we are left to fend for ourselves in this area.
A simple way to handle resizing would be to write a state manager which uses the browser resize event. It would store the name of your current state in a variable and then update as required. A sample of how this might look is shown below (also can be found in this gist). Libraries enquire.js SimpleStateManager Summary. Firing Responsive jQuery Functions based on CSS Media Queries Rather than Window Width | FourFront. Updated: There are some great options for managing Javascript when using CSS media queries in a responsive website. MediaCheck, jRespond, and Breakpoints.js all allow you to fire javascript functions based on customizable breakpoints in browser width.
However, recently I was working on a small site with only a single function to be called at a smaller browser size, in conjunction with a media query, and thought I'd forgo one of these scripts and manage my change using a jQuery window width measurement. The Problem: jQuery $(window).width() and CSS3 Media Queries do not always match. Initially I was using the below code: $(window).resize(function(){ if ($(window).width() <= 800){ // do something here } }); When viewing the site in Firefox, I noticed a small difference in the width at which my media query and javascript were firing.
The solution: use jQuery to test for a changed CSS property, rather than the browser width The jQuery The CSS Was this article helpful? How to detect responsive breakpoints of Twitter Bootstrap 3 using JavaScript? JQuery: How to call RESIZE event only once it's FINISHED resizing? jQuery: Trigger function when window width matches any value in array. How to detect breakpoints in Bootstrap using JavaScript. Html - How to position text over an image in css. Html - how to display text over an image in Bootstrap 3.1. Bind to ready and resize at same time using jQuery .on()
jQuery combine document .ready and .resize functions. jQuery combine .ready and .resize. jQuery combine .ready and .resize. How to trigger certain function for a particular window size - iswwwup.com. I need to trigger zooming function for a particular window size to make responsive site.Am using elevatezoom.js for zooming Am trying to call the zoom by var width = $(screen).width(); var height = $(screen).height(); if (width >= 980) { $('#zoom_01').elevateZoom(); } how can i solve this 1 answer Try this: var width = window.innerWidth;var height = window.innerHeight; if (width >= 980) { $('#zoom_01').elevateZoom();} 2 similar answers β You can bind window resize events by doing: $(window).on('resize', function(event){ // Do stuff here}); You can get the window size by doing: var windowWidth = $(window).width();if(windowWidth > 768){ // Do stuff here} Here is a jsfiddle to see it all in action.
Be careful what you bind in your window resize events. Check this modification of your original jFiddle: What I changed: This may or may not answer your question, but this is what I interpreted your question as asking for. Disabling jQuery click function at certain window size. Importing CSS Breakpoints Into Javascript. Get Pseudo-Element Properties with JavaScript. Journal—Conditionally loading content. Bevan did a great job on the dConstruct website. I tried to help him out along the way, which mostly involved me doing a swoop’n’poop code review every now and then. I was particularly concerned about performance so I suggested base-64 encoding background images wherever possible and squeezing inline images through ImageOptim. I also spotted an opportunity to do a bit of conditional loading.
Apart from the home page, just about every other page on the site features a fairly hefty image in the header …if you view it in a wide enough browser window. If you’re visiting with a narrow viewport, an image like this would just interfere with the linearised layout and be an annoying thing to scroll past. So instead of putting the image in the markup, I put a data-img attribute on the body element: Then in a JavaScript file that’s executed after the DOM has loaded, I check to see if the we’re dealing with a wide-screen viewport or not. I’m doing something similar with videos. Journal—Conditional CSS. I got some great comments on my post about conditionally loading content. Just to recap, I was looking for a way of detecting from JavaScript whether media queries have been executed in CSS without duplicating my breakpoints. That bit is important: I’m not looking for MatchMedia, which involves making media queries in JavaScript.
Instead I’m looking for some otherwise-useless CSS property that I can use to pass information to JavaScript. Tantek initially suggested using good ol’ voice-family, which he has used for hacks in the past. Then Tantek suggested that, whatever property I end up using, I could apply it to an element that’s never rendered: meta or perhaps head. A number of people suggested using font-family, citing Foresight.js as prior art. Then I can read that in JavaScript: window.getComputedStyle(document.head,null).getPropertyValue('font-family') It works!
I guess I could just wait a little while for Opera to copy whatever Webkit browsers do. Back to the drawing board. Nice! Importing CSS Breakpoints Into JavaScript. A Simpler Solution A quick and easy solution to this problem is to have your JS import the breakpoints directly from the CSS values in the DOM. This solution brings the current breakpoint variable into your JS in a way that's Simple & Lightweight DRY Compatible with all browsers that support media queries (IE9+) To see where we're going with this, check out this fully functioning codepen. Declare your breakpoints For simplicity, this code is straight CSS and can easily be abstracted to Sass or Less. Note that I have to hide the ::before pseudo-element so it doesn't show to the user. Importing the Breakpoints into JavaScript This is the magic that queries the property for the current breakpoint. There's a couple things going on here. Firefox and IE return the value with double quotes, while other browsers do not.
Trigger on resize and page load Breakpoints change based on your browser's viewport width, so I need to update the value when the browser is resized. In use Sample Use Case. Html - Text overlay on image.