background preloader

Css/javascript media queried

Facebook Twitter

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.

Managing Responsive Breakpoints in Javascript - Jonathan Fielding

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.

When working with multiple states though, it is important to remember that there are some gotchas when changing state in javascript: Libraries enquire.js SimpleStateManager Summary. Firing Responsive jQuery Functions based on CSS Media Queries Rather than Window Width. Updated: There are some great options for managing Javascript when using CSS media queries in a responsive website.

Firing Responsive jQuery Functions based on CSS Media Queries Rather than Window Width

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: 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.

How to trigger certain function for a particular window size - iswwwup.com

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. Importing CSS Breakpoints Into Javascript. Get Pseudo-Element Properties with JavaScript. Journal—Conditionally loading content. Bevan did a great job on the dConstruct website.

Journal—Conditionally loading content

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.

Journal—Conditional CSS

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. 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.

Importing CSS Breakpoints Into JavaScript

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. Now, let's get started! 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.

Html - Text overlay on image.