diff --git a/bonzo.js b/bonzo.js index 7642e91..214bd67 100644 --- a/bonzo.js +++ b/bonzo.js @@ -298,10 +298,11 @@ var el = this[0] if (!el) return this if (x == null && y == null) { - return (isBody(el) ? getWindowScroll() : { x: el.scrollLeft, y: el.scrollTop })[type] + return (isBody(el) ? getWindowScroll(el) : { x: el.scrollLeft, y: el.scrollTop })[type] } if (isBody(el)) { - win.scrollTo(x, y) + var elWin = getElementWindow(el) + elWin.scrollTo(x, y) } else { x != null && (el.scrollLeft = x) y != null && (el.scrollTop = y) @@ -790,7 +791,7 @@ var el = this[0] , de = el.ownerDocument.documentElement , bcr = el.getBoundingClientRect() - , scroll = getWindowScroll() + , scroll = getWindowScroll(el) , width = el.offsetWidth , height = el.offsetHeight , top = bcr.top + scroll.y - Math.max(0, de && de.clientTop, doc.body.clientTop) @@ -994,11 +995,19 @@ } function isBody(element) { - return element === win || (/^(?:body|html)$/i).test(element.tagName) + return element === win || (/^(?:body|html|iframe)$/i).test(element.tagName) } - function getWindowScroll() { - return { x: win.pageXOffset || html.scrollLeft, y: win.pageYOffset || html.scrollTop } + function getWindowScroll(el) { + var elWin = getElementWindow(el) + , elHtml = elWin.document.documentElement || html + return { x: elWin.pageXOffset || elHtml.scrollLeft, y: elWin.pageYOffset || elHtml.scrollTop } + } + + function getElementWindow(el) { + if (el.contentWindow) return el.contentWindow + if (el.ownerDocument) return el.ownerDocument.defaultView || el.ownerDocument.parentWindow + return win } function createScriptFromHtml(html) { diff --git a/bonzo.min.js b/bonzo.min.js index 7ebe25d..a7f0dc9 100644 --- a/bonzo.min.js +++ b/bonzo.min.js @@ -3,4 +3,4 @@ * https://github.com/ded/bonzo * License MIT */ -(function(e,t,n){typeof module!="undefined"&&module.exports?module.exports=n():typeof define=="function"&&define.amd?define(n):t[e]=n()})("bonzo",this,function(){function L(e,n){var r=null,i=t.defaultView.getComputedStyle(e,"");return i&&(r=i[n]),e.style[n]||r}function A(e){return e&&e.nodeName&&(e.nodeType==1||e.nodeType==11)}function O(e,t,n){var r,i,s;if(typeof e=="string")return Q.create(e);A(e)&&(e=[e]);if(n){s=[];for(r=0,i=e.length;r0?V(s,r):r)},null,r)},this,r),s.length=i,_(o,function(e){s[--i]=e},null,!r),s}function U(e,t,n){var r=Q(e),i=r.css("position"),s=r.offset(),o="relative",u=i==o,a=[parseInt(r.css("left"),10),parseInt(r.css("top"),10)];i=="static"&&(r.css("position",o),i=o),isNaN(a[0])&&(a[0]=u?0:e.offsetLeft),isNaN(a[1])&&(a[1]=u?0:e.offsetTop),t!=null&&(e.style.left=t-s.left+a[0]+w),n!=null&&(e.style.top=n-s.top+a[1]+w)}function z(e,t){return typeof t=="function"?t.call(e,e):t}function W(t,n,r){var i=this[0];return i?t==null&&n==null?($(i)?J():{x:i.scrollLeft,y:i.scrollTop})[r]:($(i)?e.scrollTo(t,n):(t!=null&&(i.scrollLeft=t),n!=null&&(i.scrollTop=n)),this):this}function X(e){this.length=0;if(e){e=typeof e!="string"&&!e.nodeType&&typeof e.length!="undefined"?e:[e],this.length=e.length;for(var t=0;t/,u=["","
",1],a=["","
",3],f=["",1],l=["_","",0,1],c={thead:u,tbody:u,tfoot:u,colgroup:u,caption:u,tr:["","
",2],th:a,td:a,col:["","
",2],fieldset:["
","
",1],legend:["
","
",2],option:f,optgroup:f,script:l,style:l,link:l,param:l,base:l},h=/^(checked|selected|disabled)$/,p,d,v,m={},g=0,y=/^-?[\d\.]+$/,b=/^data-(.+)$/,w="px",E="setAttribute",S="getAttribute",x=function(){var e=t.createElement("p");return{transform:function(){var t=["transform","webkitTransform","MozTransform","OTransform","msTransform"],n;for(n=0;n]+)/),i=t.createElement("div"),s=[],u=n?c[n[1].toLowerCase()]:null,a=u?u[2]+1:1,f=u&&u[3],l=r;i.innerHTML=u?u[0]+e+u[1]:e;while(a--)i=i.firstChild;f&&i&&i.nodeType!==1&&(i=i.nextSibling);do(!n||i.nodeType==1)&&s.push(i);while(i=i.nextSibling);return _(s,function(e){e[l]&&e[l].removeChild(e)}),s}():A(e)?[e.cloneNode(!0)]:[]},Q.doc=function(){var e=Q.viewport();return{width:Math.max(t.body.scrollWidth,n.scrollWidth,e.width),height:Math.max(t.body.scrollHeight,n.scrollHeight,e.height)}},Q.firstChild=function(e){for(var t=e.childNodes,n=0,r=t&&t.length||0,i;n0?V(s,r):r)},null,r)},this,r),s.length=i,_(o,function(e){s[--i]=e},null,!r),s}function U(e,t,n){var r=G(e),i=r.css("position"),s=r.offset(),o="relative",u=i==o,a=[parseInt(r.css("left"),10),parseInt(r.css("top"),10)];i=="static"&&(r.css("position",o),i=o),isNaN(a[0])&&(a[0]=u?0:e.offsetLeft),isNaN(a[1])&&(a[1]=u?0:e.offsetTop),t!=null&&(e.style.left=t-s.left+a[0]+w),n!=null&&(e.style.top=n-s.top+a[1]+w)}function z(e,t){return typeof t=="function"?t.call(e,e):t}function W(e,t,n){var r=this[0];if(!r)return this;if(e==null&&t==null)return($(r)?J(r):{x:r.scrollLeft,y:r.scrollTop})[n];if($(r)){var i=K(r);i.scrollTo(e,t)}else e!=null&&(r.scrollLeft=e),t!=null&&(r.scrollTop=t);return this}function X(e){this.length=0;if(e){e=typeof e!="string"&&!e.nodeType&&typeof e.length!="undefined"?e:[e],this.length=e.length;for(var t=0;t/,u=["","
",1],a=["","
",3],f=["",1],l=["_","",0,1],c={thead:u,tbody:u,tfoot:u,colgroup:u,caption:u,tr:["","
",2],th:a,td:a,col:["","
",2],fieldset:["
","
",1],legend:["
","
",2],option:f,optgroup:f,script:l,style:l,link:l,param:l,base:l},h=/^(checked|selected|disabled)$/,p,d,v,m={},g=0,y=/^-?[\d\.]+$/,b=/^data-(.+)$/,w="px",E="setAttribute",S="getAttribute",x=function(){var e=t.createElement("p");return{transform:function(){var t=["transform","webkitTransform","MozTransform","OTransform","msTransform"],n;for(n=0;n]+)/),i=t.createElement("div"),s=[],u=n?c[n[1].toLowerCase()]:null,a=u?u[2]+1:1,f=u&&u[3],l=r;i.innerHTML=u?u[0]+e+u[1]:e;while(a--)i=i.firstChild;f&&i&&i.nodeType!==1&&(i=i.nextSibling);do(!n||i.nodeType==1)&&s.push(i);while(i=i.nextSibling);return _(s,function(e){e[l]&&e[l].removeChild(e)}),s}():A(e)?[e.cloneNode(!0)]:[]},G.doc=function(){var e=G.viewport();return{width:Math.max(t.body.scrollWidth,n.scrollWidth,e.width),height:Math.max(t.body.scrollHeight,n.scrollHeight,e.height)}},G.firstChild=function(e){for(var t=e.childNodes,n=0,r=t&&t.length||0,i;n')).attr({ + src: 'about:blank' + }).css({ + width: '200px' + , height: '200px' + }) + , $body; + $iframe[0].onload = function() { + $body = $(this.contentWindow.document.body); + $body.append($(dom.create('
')).css({ + width: '400px' + , height: '300px' + })) + + $iframe.scrollTop(5) + ok($iframe.scrollTop() == 5, 'should set iframe window scroll top') + $iframe.scrollLeft(25) + ok($iframe.scrollLeft() == 25, 'should set iframe window scroll left') + + $body.scrollTop(10) + ok($body.scrollTop() == 10, 'should set iframe document body scroll top') + $body.scrollLeft(50) + ok($body.scrollLeft() == 50, 'should set iframe document body scroll left') + } + $iframe.appendTo(document.body) + }) + test('width & height can be accessed on window and document', 2, function () { ok($(window).css('width') == $(document).css('width'), 'win and doc have same width') ok($(window).css('height') < $(document).css('height'), 'document height is much larger than win height')