Code Monkey home page Code Monkey logo

responsivejs's Introduction

ResponsiveJS

Like @media breakpoints from CSS, but for JavaScript.

npm version

Do you have a responsively designed web page with lots of JavaScript that you want to load on the desktop, but not on mobile? This will handle that for you. Designate a minWidth and/or a maxWidth in pixels; your script will only load and execute when the screen is the right width.

To use, call responsiveJS.register() with an object bearing these properties (all of which are optional):

  • name: Name of your script. Used in various console.logs to let you know what's going on.
  • url: You can designate a .js file to be loaded and run when the screen is the right size.
  • activationFunction: A function to be run when the screen is the right size.
  • deactivationFunction: A function to be run when the screen is no longer the right size. You can combine this with the above to have JavaScript features that turn on and off as the window gets resized.
  • minWidth: Run activationFunction and the script downloaded from url when the screen is at least this size. Run deactivationFunction if the screen gets smaller than that.
  • maxWidth: Run activationFunction and the script downloaded from url when the screen is smaller than this. Run deactivationFunction if the screen gets too big.

You can call responsiveJS.register() multiple times with different combinations of settings, like so:

responsiveJS.register({
  name: "Desktop script",
  minWidth: 1200,
  url: "bigFile.js",
  activationScript: function() {
    console.log("Desktop mode");
    // do desktop things
  },
  deactivationScript: function() {
    // stop doing desktop things
  }
});
responsiveJS.register({
  name: "Mobile script",
  maxWidth: 1200,
  activationScript: function() {
    console.log("Mobile mode");
    // do mobile things
  },
  deactivationScript: function() {
    // stop doing mobile things
  }
});

responsivejs's People

Contributors

dawnpaladin avatar

Watchers

James Cloos avatar  avatar

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.