forked from borbit/jquery.viewport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.viewport.min.js
16 lines (13 loc) · 2.79 KB
/
jquery.viewport.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
jQuery.Viewport 0.2.3
Makes an element as a handy viewport for displaying content
with absolute position. For all details and documentation:
http://borbit.github.com/jquery.viewport/
Copyright (c) 2011-2013 Serge Borbit <serge.borbit@gmail.com>
Licensed under the MIT license
*/
(function(m){function s(a,b){var j,k,g,h;function p(){!1!==b.width&&!1!==b.height?(c.height(b.height),c.width(b.width)):!1!==i&&(c.height(i.height()),c.width(i.width()));e.height=c.height();e.width=c.width()}k=j=h=g=0;var e={height:0,width:0},n=!0,o=!0,q=0,r=0,f=m("<div/>").addClass(b.binderClass),c=m("<div/>").addClass(b.contentClass);a.css(t);c.css(u);f.css(v);var i=!1;!b.content&&a.children().length?i=a.children():b.content&&(i=b.content);p();(function(){var d=b.position.split(" ");if(~d.indexOf("bottom")){o=
false;g=j-e.height}else if(~d.indexOf("top")){o=false;g=0}if(~d.indexOf("right")){n=false;h=k-e.width}else if(~d.indexOf("left")){n=false;h=0}})();i&&(i.detach(),c.append(i));f.append(c).appendTo(a);a.bind("dragstop",function(d,a){g!=a.position.top&&(n=false);h!=a.position.left&&(o=false);h=a.position.left;g=a.position.top});var l=Math.floor;return{adjust:function(){j=a.height();k=a.width();var d,b;if(j>e.height){c.css("top",0);f.css("height",e.height);f.css("top",l(j/2)-l(e.height/2))}else{d=e.height-
j;f.css("height",j+d*2);f.css("top",-d);if(o){g=l(d/2);c.css("top",g)}else{b=g+(d-q);b>=0||(b=0);g=b;c.css("top",b)}q=d}if(k>e.width){c.css("left",0);f.css("width",e.width);f.css("left",l(k/2)-l(e.width/2))}else{d=e.width-k;f.css("width",k+d*2);f.css("left",-d);if(n){h=l(d/2);c.css("left",h)}else{b=h+(d-r);b>=0||(b=0);h=b;c.css("left",b)}r=d}},updateContentSize:p,setContentHeight:function(a){e.height=a;c.height(a)},setContentWidth:function(a){e.width=a;c.width(a)},contentSize:e,content:c,binder:f}}
m.widget("ui.viewport",{options:{binderClass:"viewportBinder",contentClass:"viewportContent",position:"center",content:!1,height:!1,width:!1},_create:function(){var a=this.options.content;if(null!=a.tagName||m.isArray(a))this.options.content=m(a);this.viewport=s(this.element,this.options);this.viewport.adjust()},update:function(){this.viewport.updateContentSize();this.viewport.adjust()},adjust:function(){this.viewport.adjust()},content:function(){return this.viewport.content},binder:function(){return this.viewport.binder},
size:function(a,b){if(null==a||null==b)return this.viewport.contentSize;this.viewport.setContentHeight(a);this.viewport.setContentWidth(b)},height:function(a){if(null==a)return this.viewport.contentSize.height;this.viewport.setContentHeight(a)},width:function(a){if(null==a)return this.viewport.contentSize.width;this.viewport.setContentWidth(a)}});var v={position:"absolute",overflow:"hidden"},t={position:"relative",overflow:"hidden"},u={position:"absolute"}})(jQuery);