Skip to content

Instantly share code, notes, and snippets.

@eigan
Last active December 23, 2015 06:19
Show Gist options
  • Save eigan/6593476 to your computer and use it in GitHub Desktop.
Save eigan/6593476 to your computer and use it in GitHub Desktop.

Method for checking viewport in javascript

Really simple trick to get the current viewport in javascript.

HTML (somewhere in your grid)

<div class="viewport-width-id"></div>

CSS:

.viewport-width-id { width: 1px; }
@media (max-width: 1030px) { .viewport-width-id { width: 5px } }
@media (max-width: 768px) { .viewport-width-id { width: 10px; } }

Javascript

site = {
  getPlatform : function() {
		var winWidth = $(".viewport-width-id").width();
		if(winWidth == 1) return 'desktop';
		if(winWidth == 5)  return 'tablet';
		return 'mobile';
	}
}

site.getPlatform() // desktop | tablet | mobile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment