Created
May 23, 2014 16:53
-
-
Save JordanForeman/4f3c6b0f8d8f40059cdf to your computer and use it in GitHub Desktop.
JavaScript Breakpoint Responder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Responsive JavaScript Helpers | |
| /* CONSTANTS HOLDER */ | |
| var ScaleDirection = { | |
| SCALE_UP : "UP", | |
| SCALE_DOWN : "DOWN" | |
| }; | |
| /* RESPONDER CLASS */ | |
| var Responder = new function(){ | |
| this.breakpoints = new Array(); | |
| }; | |
| /* | |
| * Adds a breakpoint responder | |
| * - breakpoint - (int) represents the visual breakpoint being hit | |
| * - sizeDownCallback - (function) code to be run when the UI is scaling down | |
| * - sizeUpCallback - (function) code to be run when the UI is scaling up | |
| */ | |
| Responder.prototype.AddBreakpoint = new function(breakpoint, sizeDownCallback, sizeUpCallback){ | |
| var breakpoint = { | |
| px : breakpoint, | |
| down : sizeDownCallback, | |
| up : sizeUpCallback | |
| }; | |
| this.breakpoints.push(breakpoint); | |
| return; | |
| }; | |
| /* | |
| * Calls a breakpoint responder for a given breakpoint value and scale direction | |
| * - breakpoint - (int) | |
| * - scaleDirection - (string) | |
| */ | |
| Responder.prototype.CallBreakpoint = new function(breakpoint, scaleDirection){ | |
| if (!this.HasBreakpoint(breakpoint)) | |
| return; | |
| var func = null; | |
| for (var i = 0; i < this.breakpoints.length; i++) { | |
| var bp = this.breakpoints[i]; | |
| if (bp.px == breakpoint) | |
| func = (scaleDirection == ScaleDirection.SCALE_UP) ? bp.up : bp.down; | |
| } | |
| if(!func) | |
| return; | |
| return func(); | |
| }; | |
| /* | |
| * Returns true if a breakpoint for the given pixel value is registered. False if not | |
| * - breakpoint - (int) | |
| */ | |
| Responder.prototype.HasBreakpoint = new function(breakpoint) { | |
| for (var i = 0; i < this.breakpoints.length; i++) { | |
| if (this.breakpoints[i].px == breakpoint) | |
| return true; | |
| } | |
| return false; | |
| }; | |
| var responder = new Responder(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very basic JavaScript responder. Still need to implement the logic to trigger it on page resize (and determine which direction the page is scaling)