Signin  Pricing  Examples  Support

Libraries > viewport.js

viewport.js - Search Live Website Code
It's like doing CTRL+F on millions of websites' source code



NerdyData is a search engine for source code. Search the source code of 200 million websites.


JAVASCRIPT FILE http://telia.no/netcom-theme/js/viewport.js


Libraries on telia.no


tsr-core.js

carousel.js

main.js

newsticker.js

slideshow.js

swfobject.js

topmenu.js

viewport.js



Explore More


main.js

plugins.js

jquery-1.10.2.min.js

modernizr-2.6.2.min.js

viewport.js
				
// Live cross-browser viewport detection
// Think media queries, but done in javascript!
// A better solution than separate @media css files or isolated @media brackets
// Special thanks to Paul Irish for the debounce function

(function (win, documentElement) {
        // Event function
    var addEvent = (win.addEventListener) ? function (type, node, fn) {
            node.addEventListener(type, fn, false);
        } : function (type, node, fn) {
            node.attachEvent(
                'on'+type,
                function (e) {
                    fn.apply(node, [e]);
                }
            );
        },

        // Debounce function
        debounce = function (func, threshold) {
            var timeout;
            return function () {
                var obj = this,
                    args = arguments,
                    delayed = function () {
                        func.apply(obj, args);
                        timeout = null;
                    };
                if (timeout) clearTimeout(timeout);
                timeout = setTimeout(delayed, 50); 
            };
        },

        // Sizes array
        viewportColumns = {
            960: 960,
            810: 810,
            720: 720,
            480: 480
        //    320: 320
        },

        // Minimum viewport width
        viewportMinColumns = 400,
        
        // Viewport Change event
        viewportChange = function() {
        	var oldClassNames,
                classNames = oldClassNames = documentElement.className.replace(/(\s|\b)+vp(lt|gt)*\d+(\b|\s)+/g, ''),
                viewportWidth = documentElement.clientWidth,
                //viewportWidth = screen.width,
                bucket = [],
                viewportMaxColumns = viewportMinColumns,
                col;

        	for (col in viewportColumns) viewportMaxColumns = Math.max(viewportMaxColumns, viewportWidth >= viewportColumns[col] ? col : viewportMinColumns);
            
            bucket.push('vp'+viewportMaxColumns);

            for (col in viewportColumns) {
                bucket.push('vp'+(viewportWidth >= viewportColumns[col] ? 'gt' : 'lt')+col);
            }
            
            if (navigator.userAgent.indexOf("Android")>-1) {
	            // Listen for orientation changes
				window.addEventListener("orientationchange", function() {
				  if(window.orientation === 0) {
				  	jQuery('#meta-viewport-id').attr('content','width=device-width, initial-scale=1, target-densityDpi=high-dpi');
				  } else if(window.orientation === 90) { 
				  	jQuery('#meta-viewport-id').attr('content','width=device-width, initial-scale=1, target-densityDpi=medium-dpi');
				  }
				}, false);
	            
	            /* HTC HD HACk */
	            if (viewportWidth >= 720) {
	            	bucket.push('vplt720');
	            }
            }
            
            classNames += ' '+bucket.join(' ');

            if (oldClassNames != classNames) documentElement.className = classNames;   
        };

    // Attach function to events
    addEvent('resize', win, debounce(viewportChange));
    addEvent('orientationchange', win, viewportChange);

    viewportChange();
})(this, document.documentElement);


A B C D E F G H I J K L M N O P Q R S T U V W X Y Z