Code Monkey home page Code Monkey logo

elemedia's Introduction

UPDATE: if you happened to find this repository and are considering using it in your project, I would suggest using tysonmatanich's much more robust solution, ElementQuery.

Elemedia.js

Style elements based on their own widths, regardless of viewport width!

Huh? What's the point?

A lot of times you'll have a containing element–say a sidebar widget–that looks great until the sidebar gets below n number of pixels or percent wide, which is where you'd traditionally add some media-query based rules in your CSS to fix the layout. But then...

As the viewport continues to get narrower, say your layout then goes into single-column mode, and your sidebar widget now spans 600 pixels instead of its original 300, and all that beautiful narrow-width styling you just added now looks like complete rubbish. Enter media-query based CSS rule set number two. Things are now starting to get ridiculous.

Modularity FTW!

This plugin works by being called on any jQuery selector, and takes two arguments:

  • mediaQuery: a string set to either 'min-width' or 'max-width'
  • threshold: an integer set to the width in pixels* that will trigger the "media query"

When the width of the element the function is called on gets above or below the threshold you supplied (depending on how you set the mediaQuery argument), a class name is dynamically added that follows the form of "mq-[mediaQuery]-[threshold]", where the values in brackets match the values you supply to the plugin.

*em-based width support is coming. Patience, young padawan...

An Example

Calling this...

$('.my-element').elemedia('min-width', 500);

will change your markup from this...

<div class="my-element">

...to this:

<div class="my-element mq-min-width-500">

Chaining is obviously supported, so you can call the function several times in a row on the same selector to add more than one media query to the element, like so:

// the javascript
$('.my-element').elemedia('min-width', 250).elemedia('min-width, 500);

// the markup
<div class="my-element mq-min-width-250 mq-min-width-500">

Stay Classy

If I catch you putting CSS in your JavaScript, I will personally migrate all your projects to SVN while you're not looking. Use the classes, that's what they're there for.

Pre-requisites

  • jQuery >= 1.7

Changelog

  • v 1.0 - Initial release
  • v 1.0.1 - Fixed window resize bug (see issue #2)

elemedia's People

Contributors

mklickman avatar

Stargazers

 avatar

Watchers

 avatar  avatar

elemedia's Issues

Nothing happens on window resize

The class name should update when the window is resized and passes above/below the user-supplied threshold. Instead, it is only updated when the page loads initially.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.