📄 jquery.dimensions.js
字号:
/* Copyright (c) 2007 Paul Bakaus (paul.bakaus@googlemail.com) and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net) * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses. * * $LastChangedDate$ * $Rev$ * * Version: 1.1.2 * * Requires: jQuery 1.1.3+ */(function($){// store a copy of the core height and width methodsvar height = $.fn.height, width = $.fn.width;$.fn.extend({ /** * If used on document, returns the document's height (innerHeight). * If used on window, returns the viewport's (window) height. * See core docs on height() to see what happens when used on an element. * * @example $("#testdiv").height() * @result 200 * * @example $(document).height() * @result 800 * * @example $(window).height() * @result 400 * * @name height * @type Number * @cat Plugins/Dimensions */ height: function() { if ( !this[0] ) error(); if ( this[0] == window ) if ( $.browser.opera || ($.browser.safari && parseInt($.browser.version) > 520) ) return self.innerHeight - (($(document).height() > self.innerHeight) ? getScrollbarWidth() : 0); else if ( $.browser.safari ) return self.innerHeight; else return $.boxModel && document.documentElement.clientHeight || document.body.clientHeight; if ( this[0] == document ) return Math.max( ($.boxModel && document.documentElement.scrollHeight || document.body.scrollHeight), document.body.offsetHeight ); return height.apply(this, arguments); }, /** * If used on document, returns the document's width (innerWidth). * If used on window, returns the viewport's (window) width. * See core docs on width() to see what happens when used on an element. * * @example $("#testdiv").width() * @result 200 * * @example $(document).width() * @result 800 * * @example $(window).width() * @result 400 * * @name width * @type Number * @cat Plugins/Dimensions */ width: function() { if (!this[0]) error(); if ( this[0] == window ) if ( $.browser.opera || ($.browser.safari && parseInt($.browser.version) > 520) ) return self.innerWidth - (($(document).width() > self.innerWidth) ? getScrollbarWidth() : 0); else if ( $.browser.safari ) return self.innerWidth; else return $.boxModel && document.documentElement.clientWidth || document.body.clientWidth; if ( this[0] == document ) if ($.browser.mozilla) { // mozilla reports scrollWidth and offsetWidth as the same var scrollLeft = self.pageXOffset; self.scrollTo(99999999, self.pageYOffset); var scrollWidth = self.pageXOffset; self.scrollTo(scrollLeft, self.pageYOffset); return document.body.offsetWidth + scrollWidth; } else return Math.max( (($.boxModel && !$.browser.safari) && document.documentElement.scrollWidth || document.body.scrollWidth), document.body.offsetWidth ); return width.apply(this, arguments); }, /** * Gets the inner height (excludes the border and includes the padding) for the first matched element. * If used on document, returns the document's height (innerHeight). * If used on window, returns the viewport's (window) height. * * @example $("#testdiv").innerHeight() * @result 210 * * @name innerHeight * @type Number * @cat Plugins/Dimensions */ innerHeight: function() { if (!this[0]) error(); return this[0] == window || this[0] == document ? this.height() : this.is(':visible') ? this[0].offsetHeight - num(this, 'borderTopWidth') - num(this, 'borderBottomWidth') : this.height() + num(this, 'paddingTop') + num(this, 'paddingBottom'); }, /** * Gets the inner width (excludes the border and includes the padding) for the first matched element. * If used on document, returns the document's width (innerWidth). * If used on window, returns the viewport's (window) width. * * @example $("#testdiv").innerWidth() * @result 210 * * @name innerWidth * @type Number * @cat Plugins/Dimensions */ innerWidth: function() { if (!this[0]) error(); return this[0] == window || this[0] == document ? this.width() : this.is(':visible') ? this[0].offsetWidth - num(this, 'borderLeftWidth') - num(this, 'borderRightWidth') : this.width() + num(this, 'paddingLeft') + num(this, 'paddingRight'); }, /** * Gets the outer height (includes the border and padding) for the first matched element. * If used on document, returns the document's height (innerHeight). * If used on window, returns the viewport's (window) height. * * The margin can be included in the calculation by passing an options map with margin * set to true. * * @example $("#testdiv").outerHeight() * @result 220 * * @example $("#testdiv").outerHeight({ margin: true }) * @result 240 * * @name outerHeight * @type Number * @param Map options Optional settings to configure the way the outer height is calculated. * @cat Plugins/Dimensions */ outerHeight: function(options) { if (!this[0]) error(); options = $.extend({ margin: false }, options || {}); return this[0] == window || this[0] == document ? this.height() : this.is(':visible') ? this[0].offsetHeight + (options.margin ? (num(this, 'marginTop') + num(this, 'marginBottom')) : 0) : this.height() + num(this,'borderTopWidth') + num(this, 'borderBottomWidth') + num(this, 'paddingTop') + num(this, 'paddingBottom') + (options.margin ? (num(this, 'marginTop') + num(this, 'marginBottom')) : 0); }, /** * Gets the outer width (including the border and padding) for the first matched element. * If used on document, returns the document's width (innerWidth). * If used on window, returns the viewport's (window) width. * * The margin can be included in the calculation by passing an options map with margin * set to true. * * @example $("#testdiv").outerWidth() * @result 1000 * * @example $("#testdiv").outerWidth({ margin: true }) * @result 1020 * * @name outerHeight * @type Number * @param Map options Optional settings to configure the way the outer width is calculated. * @cat Plugins/Dimensions */ outerWidth: function(options) { if (!this[0]) error(); options = $.extend({ margin: false }, options || {}); return this[0] == window || this[0] == document ? this.width() : this.is(':visible') ? this[0].offsetWidth + (options.margin ? (num(this, 'marginLeft') + num(this, 'marginRight')) : 0) : this.width() + num(this, 'borderLeftWidth') + num(this, 'borderRightWidth') + num(this, 'paddingLeft') + num(this, 'paddingRight') + (options.margin ? (num(this, 'marginLeft') + num(this, 'marginRight')) : 0); }, /** * Gets how many pixels the user has scrolled to the right (scrollLeft). * Works on containers with overflow: auto and window/document. * * @example $(window).scrollLeft() * @result 100 * * @example $(document).scrollLeft() * @result 100 * * @example $("#testdiv").scrollLeft() * @result 100 * * @name scrollLeft * @type Number * @cat Plugins/Dimensions */ /** * Sets the scrollLeft property for each element and continues the chain. * Works on containers with overflow: auto and window/document. * * @example $(window).scrollLeft(100).scrollLeft() * @result 100 * * @example $(document).scrollLeft(100).scrollLeft() * @result 100 * * @example $("#testdiv").scrollLeft(100).scrollLeft() * @result 100 * * @name scrollLeft * @param Number value A positive number representing the desired scrollLeft. * @type jQuery * @cat Plugins/Dimensions */ scrollLeft: function(val) { if (!this[0]) error(); if ( val != undefined ) // set the scroll left return this.each(function() { if (this == window || this == document) window.scrollTo( val, $(window).scrollTop() ); else this.scrollLeft = val; }); // return the scroll left offest in pixels if ( this[0] == window || this[0] == document ) return self.pageXOffset || $.boxModel && document.documentElement.scrollLeft || document.body.scrollLeft; return this[0].scrollLeft; }, /** * Gets how many pixels the user has scrolled to the bottom (scrollTop). * Works on containers with overflow: auto and window/document. * * @example $(window).scrollTop() * @result 100 * * @example $(document).scrollTop() * @result 100 * * @example $("#testdiv").scrollTop() * @result 100 * * @name scrollTop * @type Number * @cat Plugins/Dimensions */ /** * Sets the scrollTop property for each element and continues the chain. * Works on containers with overflow: auto and window/document. * * @example $(window).scrollTop(100).scrollTop() * @result 100 * * @example $(document).scrollTop(100).scrollTop() * @result 100 * * @example $("#testdiv").scrollTop(100).scrollTop() * @result 100 * * @name scrollTop * @param Number value A positive number representing the desired scrollTop. * @type jQuery * @cat Plugins/Dimensions */ scrollTop: function(val) { if (!this[0]) error(); if ( val != undefined ) // set the scroll top return this.each(function() { if (this == window || this == document) window.scrollTo( $(window).scrollLeft(), val ); else this.scrollTop = val; }); // return the scroll top offset in pixels if ( this[0] == window || this[0] == document ) return self.pageYOffset || $.boxModel && document.documentElement.scrollTop || document.body.scrollTop; return this[0].scrollTop; }, /** * Gets the top and left positioned offset in pixels. * The positioned offset is the offset between a positioned * parent and the element itself. * * For accurate calculations make sure to use pixel values for margins, borders and padding. * * @example $("#testdiv").position() * @result { top: 100, left: 100 } * * @example var position = {}; * $("#testdiv").position(position) * @result position = { top: 100, left: 100 } * * @name position * @param Object returnObject Optional An object to store the return value in, so as not to break the chain. If passed in the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -