Skip to content

Instantly share code, notes, and snippets.

@aharris
Created January 21, 2015 16:21
Show Gist options
  • Save aharris/3502ae1f45c767062381 to your computer and use it in GitHub Desktop.
Save aharris/3502ae1f45c767062381 to your computer and use it in GitHub Desktop.
Responsive JS method from: http://www.ashtonharris.me/#!/responsive_js
var App = window.App || {
beacon: $('.beacon').css('width'),
// Store the beacon Value
lastBeacon: this.beacon,
init: function () {
// Fire the initial functions for first screen size
this.getBeacon(this.beacon);
},
resize: function () {
var newBeacon = $('.beacon').css('width');
// If the screen size has changed fire the functions for the new screen size
if (this.lastBeacon !== newBeacon) {
this.lastBeacon = newBeacon;
this.getBeacon(newBeacon);
}
},
getBeacon: function(beacon) {
switch(beacon) {
case "0px":
this.smallStuff();
break;
case "768px":
this.mediumStuff();
break;
case "1024px":
this.largeStuff();
break;
default:
console.log('Error: No beacon match found');
}
},
smallStuff: function() {
// Small viewport functions
},
mediumStuff: function() {
// Medium viewport functions
},
largeStuff: function() {
// Large Viewport Functions
}
};
$().ready(function () {
App.init();
});
$(window).resize(function () {
App.resize();
});
<!DOCTYPE html>
<html>
<head>
<title>Responsive JavaScript</title>
</head>
<body>
<div class="beacon"></div>
<!-- Include jQuery yourself -->
<script type="text/javascript" src="jquery-2.1.1.min.js" ></script>
</body>
</html>
.beacon {
display: none;
width: 0px;
}
/* Medium screen styles */
@media only screen and (min-width: 48em) {
.beacon {
width: 768px;
}
}
/* Large screen styles */
@media only screen and (min-width: 64em) {
.beacon {
width: 1024px;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment