Giter Site home page Giter Site logo

jrespond's Introduction

No Maintenance Intended

DEPRECATED

jRespond is no longer supported. It was written to fill the gap before window.matchMedia() was well supported. window.matchMedia() now enjoys support across all modern browsers, rendering jRespond obsolete.


jRespond is a simple way to globally manage JavaScript on responsive websites.

Responsive websites that require JavaScript functionality for some breakpoints and not for others need some type of system for triggering the correct functions at the correct breakpoint and to also be aware of when a browser is resized across breakpoints. Although switching between breakpoints could be seen as an edge case, a few applications for jRespond are:

  • Managing functionality for initial page load: Even if the browser is never resized, jRespond can help manage what JavaScript functions are triggered when the page loads for a given breakpoint.
  • Development testing: jRespond makes it much easier to test in-browser.
  • Borderline device widths: Real user browser resizing and device rotation that crosses breakpoints.

If your project only needs to support modern browsers I highly recommend checking out Rob Tarr's mediaCheck which uses the matchMedia method. But if you're using respond.js as a polyfill to ensure that your site responds on older browsers, jRespond is worth checking out.

How does it work?

jRespond is a script that holds a list of user-defined functions that are fired based on the browser's width compared to a list of customizable breakpoints. Entry and exit functions can be defined so transitions between breakpoints can be managed by removing and unbinding some page elements while creating and binding others. jRespond was built to be independent and browser agnostic. It does NOT sniff for media queries in the stylesheets.

After including jRespond.js, call jRespond and define as many or as few media breakpoints as you need for your project. Labels can be any single-word string:

// call jRespond and add breakpoints
var jRes = jRespond([
	{
		label: 'handheld',
		enter: 0,
		exit: 767
	},{
		label: 'tablet',
		enter: 768,
		exit: 979
	},{
		label: 'laptop',
		enter: 980,
		exit: 1199
	},{
		label: 'desktop',
		enter: 1200,
		exit: 10000
	}
]);

Once running, functions can be registered with jRespond along with a desired breakpoint:

// register enter and exit functions for a single breakpoint
jRes.addFunc({
	breakpoint: 'desktop',
	enter: function() {
		myInitFunc();
	},
	exit: function() {
		myUnInitFunc();
	}
});

Or an array of breakpoints:

// register enter and exit functions for multiple breakpoints
jRes.addFunc({
	breakpoint: ['desktop','laptop'],
	enter: function() {
		myInitFunc();
	},
	exit: function() {
		myUnInitFunc();
	}
});

Or an array of breakpoints and functions:

// register enter and exit functions for multiple breakpoints and functions
jRes.addFunc([
	{
		breakpoint: 'desktop',
		enter: function() {
			myInitFuncDesktop();
		},
		exit: function() {
			myUnInitFuncDesktop();
		}
	},{
		breakpoint: 'laptop',
		enter: function() {
			myInitFuncLaptop();
		},
		exit: function() {
			myUnInitFuncLaptop();
		}
	},{
		breakpoint: 'tablet',
		enter: function() {
			myInitFuncTablet();
		},
		exit: function() {
			myUnInitFuncTablet();
		}
	}
]);

Use '*' to run a function at every breakpoint:

// register enter and exit functions for every breakpoint
jRes.addFunc({
	breakpoint: '*',
	enter: function() {
		myInitFunc();
	},
	exit: function() {
		myUnInitFunc();
	}
});

Ask jRespond what the current breakpoint is at any time:

// get the current breakpoint
jRes.getBreakpoint();

The breakpoint parameter is required but the enter and exit parameters are optional (of course, at least one is required for something to happen).

Use as a module

You can also use jRespond as a browser or Node module.

AMD module

require(['jRespond'], function (jRes) {
	var mediaQueries = jRes([
		{
			label: 'tablet',
			enter: 768,
			exit: 979
		}
	]);

	mediaQueries.addFunc({
		breakpoint: 'tablet',
		enter: function() {
			myInitFunc();
		},
		exit: function() {
			myUnInitFunc();
		}
	});
});

CommonJS syntax

require(['require', 'jRespond'], function (require) {
	var jRes = require('jRespond');

	var mediaQueries = jRes([
		{
			label: 'tablet',
			enter: 768,
			exit: 979
		}
	]);

	mediaQueries.addFunc({
		breakpoint: 'tablet',
		enter: function() {
			myInitFunc();
		},
		exit: function() {
			myUnInitFunc();
		}
	});
});

Performance

jRespond is 1.3kb minified and only polls for the browser width every 500ms. If it detects a change the polling speed is increased to 100ms only until the browser width stops changing.

Browser Support

IE 6+, Safari 5+, Firefox 3+, Chrome 1+

Dependencies

None.

Credits

Thanks to Rob Tarr for inspiring the function registration pattern and Markup Boy for helping me with my JavaScript failings.

jrespond's People

Contributors

giuseppeg avatar herschel666 avatar prayagverma avatar ten1seven avatar thejsj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jrespond's Issues

jRespond and Firefox select menu

Hello, I'm having a problem with jRespond in Firefox. My dropdown menus are redrawing or closing and reopening every half second when they are clicked on and open, causing a jumping effect. I'm using it with jPanelMenu. I'm not seeing it in any other browser. Am I doing something wrong? Here's the code:

<label for="departmentSelect" style="font-size:18px; line-height:22px; padding:0; margin:12px 4px 0 0; font-weight: bold;">Department / Interest:</label>
<select name="departmentSelect" onChange="MM_jumpMenu('parent',this,0)">
<option value="contact_us.asp" selected>Select one...</option>
<option value="contact_us.asp?d=1" >Department 1</option>
<option value="contact_us.asp?d=2" >Department 2</option>
<option value="contact_us.asp?d=3" >Department 3</option>
<option value="contact_us.asp?d=4" >Department 4</option>
<option value="contact_us.asp?d=5" >Department 5</option>
</select>
 //jRespond - responsive javascript management tool
            var jRes = jRespond([
                    {
                            label: 'small',
                            enter: 0,
                            exit: 800
                    },{
                            label: 'large',
                            enter: 801,
                            exit: 10000
                    }
            ]);
            jRes.addFunc({
                    breakpoint: 'small',
                    enter: function() {
                            jPM.on();
                    },
                    exit: function() {
                            jPM.off();
                    }
            });

Add change function for breakpoints.

It would be useful for some cases to have a function that triggers when the window size changes within a breakpoint.

For instance, I had to resort to using jQuery to create equal height columns within a grid system. This of course needs to be updated every time the width changes within the desired range.

Thanks

Bower

It would be nice if jRespond was available as a Bower package 👍

Overlapping breakpoints

I would be REALLY useful to be able to create overlapping breakpoint.

Ex:

  jRes = jRespond([
  {
    label: 'phone',
    enter: 0,
    exit: 480
  },
  {
    label: 'tablet',
    enter: 481,
    exit: 980
  },
  {
    label: 'tablet-and-less',
    enter: 0,
    exit: 980
  }])

Actualy, it break and does'nt work.

What would be the best way to load a jQuery plugin in a enter:function()?

Hi I really love your plugin, works great.

That I want to do is only load a image slider at screen width of 640 and more..
so I do this:

 // call jRespond and add breakpoints
   var jRes = jRespond([
   {
      label: 'desktop',
      enter: 640,
      exit: 10000
   }
 ]);
// register enter and exit functions for a single breakpoint
 jRes.addFunc({
   breakpoint: 'desktop',
   enter: function() {
      // Load the Flexslider
      $('.flexslider').flexslider();
   },
      exit: function() {
   }
});

But Now I still need jQuery and the flexslider plugin loaded, how would I only load these within the enter:function as well?

function is not triggered when page load.

Here is the code

            var outputStr = jQuery('body');

    jRes.addFunc({
        breakpoint: 'tablet',
        enter: function() {
            jQuery(outputStr).addClass('tablet'); // working when page load
            jQuery('.headertopnav').hide(); // is not working when page load

        },
        exit: function() {
            jQuery(outputStr).removeClass('tablet'); // working when page load
            jQuery('.headertopnav').show(); // is not working when page load

        }
    });

Above code is working fine when i am resizing browser but when i reload load page second line of function is not working.

Add Version

Hello, trying to install this library with RailsAssets. It looks like you need to add versioning to the repo for it to work.

https://rails-assets.org/components/new

jRespond has no versions defined. Please create an issue in component's repository.

Function doesn't load at breakpoint only when entering breakpoint

Seems strange. It runs the function only when I enter the breakpoint (make the screen wide then small again to enter a mobile breakpoint). It doesn't work when I refresh the page at a certain breakpoint.

Working locally so I can't provide an example... but I hope you have some help for me...

Addition to remove function

Hey!

In my project I needed a way to dynamically remove functions for certain breakpoints so I added a removeFunc:

var removeFunction = function(opts) {

    var listener;

    if (opts && opts.breakpoint && (opts.enter || opts.exit)) {

        for (var i = mediaListeners.length - 1; i >= 0; i--) { // Loop backwards since we might remove items during the loop.

            listener = mediaListeners[i];

            if (listener.breakpoint == opts.breakpoint) {

                if (listener.enter == opts.enter) {
                    listener.enter = null;
                }

                if (listener.exit == opts.exit) {
                    listener.exit == null;
                }

                if (!listener.enter && !listener.exit) {
                    mediaListeners.splice(i, 1); // Remove breakpoint if no breakpoint listeners.
                }

            }

        }

    }

};

document.documentElement.clientWidth vs window.innerWidth

On Google Chrome 20.0 for OSX window.innerWidth seems to return the width including scrollbar on the right side and document.documentElement.clientWidth excluding the scrollbar.

I think document.documentElement.clientWidth should be the value returned by the winWidth() function. I understand that these values differ from browser to browser.

winWidth method is using IE conditional in Chrome when in an iframe

I'm noticing that when using jRespond inside an iframe in Chrome, the winWidth method is going inside the IE conditional. After looking into the issue, it appears the window.innerWidth is returning 0 which sets the !window.innerWidth conditional to be truthy. I would expect if window.innerWidth returns any number (including 0) then we should not be going inside the IE conditional. Is this expectation correct?

Below is the current winWidth conditional:

    // cross browser window width
    var winWidth = function() {

      var w = 0;

      // IE
      if (!window.innerWidth) {

        if (!(document.documentElement.clientWidth === 0)) {

          // strict mode
          w = document.documentElement.clientWidth;
        } else {

          // quirks mode
          w = document.body.clientWidth;
        }
      } else {

        // w3c
        w = window.innerWidth;
      }

      return w;
    };

Below is what I changed to get this working as I'm expecting:

    // cross browser window width
    var winWidth = function() {

      var w = 0;

      // IE
      if (window.innerWidth == null || window.innerWidth == undefined) {

        if (!(document.documentElement.clientWidth === 0)) {

          // strict mode
          w = document.documentElement.clientWidth;
        } else {

          // quirks mode
          w = document.body.clientWidth;
        }
      } else {

        // w3c
        w = window.innerWidth;
      }

      return w;
    };

I wanted to run this past you first to get your thoughts on if this was the expected behavior before issuing a pull request.

Debugging with firebug almost impossible

Hi,

When jRespond is enabled in a website, it makes it very difficult (almost impossible) to debug the source code with firebug.

When you inspect the HTML source code with firebug, any dom node you unfold is automatically folded again, since the plugin alters attributes/elements in the dom and firebug, as usual, refreshes the view.

How do you tackle debugging when jRespond is enabled?

jRespond and Blendid

Hi there, have been struggling for days to get blendid and jRespond to work together, adding the

provide: {
$: "jquery",
jQuery: "jquery",
"window.jQuery" : "jquery"
}

has sorted the majority of the issues I had with 3rd party libraries in the legacy codebase im using.
The main issue i have now is jRespond remains undefined.
I have tried "import { jRespond } from 'jrespond';
also have tried to import the alternatives from both the 0.1.0 package as well as the 1.0.0 package using yarn to add the packages.

Have i missed something in the task.config ? Have you any advice to get jRespond to work with Blendid ?

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.