Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / Element.scroll-more.html
1 <html>\r
2 <head>\r
3   <title>The source code</title>\r
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js">/**\r
9  * @class Ext.Element\r
10  */\r
11 Ext.Element.addMethods({\r
12     <div id="method-Ext.Element-scrollTo"></div>/**\r
13      * Scrolls this element the specified scroll point. It does NOT do bounds checking so if you scroll to a weird value it will try to do it. For auto bounds checking, use scroll().\r
14      * @param {String} side Either "left" for scrollLeft values or "top" for scrollTop values.\r
15      * @param {Number} value The new scroll value\r
16      * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object\r
17      * @return {Element} this\r
18      */\r
19     scrollTo : function(side, value, animate){\r
20         var tester = /top/i,\r
21                 prop = "scroll" + (tester.test(side) ? "Top" : "Left"),\r
22                 me = this,\r
23                 dom = me.dom;\r
24         if (!animate || !me.anim) {\r
25             dom[prop] = value;\r
26         } else {\r
27             me.anim({scroll: {to: tester.test(prop) ? [dom[prop], value] : [value, dom[prop]]}},\r
28                          me.preanim(arguments, 2), 'scroll');\r
29         }\r
30         return me;\r
31     },\r
32     \r
33     <div id="method-Ext.Element-scrollIntoView"></div>/**\r
34      * Scrolls this element into view within the passed container.\r
35      * @param {Mixed} container (optional) The container element to scroll (defaults to document.body).  Should be a\r
36      * string (id), dom node, or Ext.Element.\r
37      * @param {Boolean} hscroll (optional) False to disable horizontal scroll (defaults to true)\r
38      * @return {Ext.Element} this\r
39      */\r
40     scrollIntoView : function(container, hscroll){\r
41         var c = Ext.getDom(container) || Ext.getBody().dom,\r
42                 el = this.dom,\r
43                 o = this.getOffsetsTo(c),\r
44             l = o[0] + c.scrollLeft,\r
45             t = o[1] + c.scrollTop,\r
46             b = t + el.offsetHeight,\r
47             r = l + el.offsetWidth,\r
48                 ch = c.clientHeight,\r
49                 ct = parseInt(c.scrollTop, 10),\r
50                 cl = parseInt(c.scrollLeft, 10),\r
51                 cb = ct + ch,\r
52                 cr = cl + c.clientWidth;\r
53 \r
54         if (el.offsetHeight > ch || t < ct) {\r
55                 c.scrollTop = t;\r
56         } else if (b > cb){\r
57             c.scrollTop = b-ch;\r
58         }\r
59         c.scrollTop = c.scrollTop; // corrects IE, other browsers will ignore\r
60 \r
61         if(hscroll !== false){\r
62                         if(el.offsetWidth > c.clientWidth || l < cl){\r
63                 c.scrollLeft = l;\r
64             }else if(r > cr){\r
65                 c.scrollLeft = r - c.clientWidth;\r
66             }\r
67             c.scrollLeft = c.scrollLeft;\r
68         }\r
69         return this;\r
70     },\r
71 \r
72     // private\r
73     scrollChildIntoView : function(child, hscroll){\r
74         Ext.fly(child, '_scrollChildIntoView').scrollIntoView(this, hscroll);\r
75     },\r
76     \r
77     <div id="method-Ext.Element-scroll"></div>/**\r
78      * Scrolls this element the specified direction. Does bounds checking to make sure the scroll is\r
79      * within this element's scrollable range.\r
80      * @param {String} direction Possible values are: "l" (or "left"), "r" (or "right"), "t" (or "top", or "up"), "b" (or "bottom", or "down").\r
81      * @param {Number} distance How far to scroll the element in pixels\r
82      * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object\r
83      * @return {Boolean} Returns true if a scroll was triggered or false if the element\r
84      * was scrolled as far as it could go.\r
85      */\r
86      scroll : function(direction, distance, animate){\r
87          if(!this.isScrollable()){\r
88              return;\r
89          }\r
90          var el = this.dom,\r
91             l = el.scrollLeft, t = el.scrollTop,\r
92             w = el.scrollWidth, h = el.scrollHeight,\r
93             cw = el.clientWidth, ch = el.clientHeight,\r
94             scrolled = false, v,\r
95             hash = {\r
96                 l: Math.min(l + distance, w-cw),\r
97                 r: v = Math.max(l - distance, 0),\r
98                 t: Math.max(t - distance, 0),\r
99                 b: Math.min(t + distance, h-ch)\r
100             };\r
101             hash.d = hash.b;\r
102             hash.u = hash.t;\r
103             \r
104          direction = direction.substr(0, 1);\r
105          if((v = hash[direction]) > -1){\r
106             scrolled = true;\r
107             this.scrollTo(direction == 'l' || direction == 'r' ? 'left' : 'top', v, this.preanim(arguments, 2));\r
108          }\r
109          return scrolled;\r
110     }\r
111 });</pre>    \r
112 </body>\r
113 </html>