Accurate Window Size / Resolution Check

var w = window, d = document, e = d.documentElement, g = d.getElementsByTagName('body')[0], x, resolutionBreakPoint = 1025, // desktop/mobile brakpoint resolutionState = false, // current state resizeTimer; /** * Get an accurate inner-width * jQuery width() varies based on browser (scrollbar) */ function resolutionCheck() { // window, document or body inner width x = w.innerWidth || e.clientWidth || g.clientWidth; if (x < resolutionBreakPoint && resolutionState !== 'mobile') { // perform actions for mobile resolutionState = 'mobile'; } else if (x >= resolutionBreakPoint && resolutionState !== 'desktop') { // perform actions for desktop resolutionState = 'desktop'; } } resolutionCheck(); $(window).resize(function() { clearTimeout(resizeTimer); // delay the check so that you don't get hundreds of requests if you're // resizing the window manually resizeTimer = setTimeout(function() { resolutionCheck(); }, 300); });
Checks the window size based on window, document or body inner-width. Better than jQuery's width function which varies based on browser because of the scrollbar.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.