Skip to content

Commit aa1f94a

Browse files
committed
Added support for scrolling via scrollTop/scrollLeft.
Modified sv-test-02.html so that you can dynamically switch the scrolling method used. This will allow us to test the performance of different methods on the different platforms. Modified scrollview.js so that you can specify @data-scroll-method="translate|position|scroll".
1 parent 39e5e75 commit aa1f94a

File tree

3 files changed

+84
-38
lines changed

3 files changed

+84
-38
lines changed

experiments/scrollview/jquery.mobile.scrollview.js

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jQuery.widget( "mobile.scrollview", jQuery.mobile.widget, {
1818
moveThreshold: 10, // User must move this many pixels in any direction to trigger a scroll.
1919
moveIntervalThreshold: 100, // Time between mousemoves must not exceed this threshold.
2020

21-
useCSSTransform: true, // Use CSS "transform" property instead of "top" and "left" for positioning.
21+
scrollMethod: "translate", // "translate", "position", "scroll"
2222

2323
startEventName: "scrollstart",
2424
updateEventName: "scrollupdate",
@@ -46,16 +46,22 @@ jQuery.widget( "mobile.scrollview", jQuery.mobile.widget, {
4646
}
4747
this._$view = $child.addClass("ui-scrollview-view");
4848

49-
this._$clip.css("overflow", "hidden");
49+
this._$clip.css("overflow", this.options.scrollMethod == "scroll" ? "scroll" : "hidden");
5050
this._makePositioned(this._$clip);
5151

5252
this._$view.css("overflow", "hidden");
5353

54-
if (!this.options.useCSSTransform)
55-
{
56-
this._makePositioned(this._$view);
57-
this._$view.css({ left: 0, top: 0 });
58-
}
54+
// Turn off our faux scrollbars if we are using native scrolling
55+
// to position the view.
56+
57+
this.options.showScrollBars = this.options.scrollMethod == "scroll" ? false : this.options.showScrollBars;
58+
59+
// We really don't need this if we are using a translate transformation
60+
// for scrolling. We set it just in case the user wants to switch methods
61+
// on the fly.
62+
63+
this._makePositioned(this._$view);
64+
this._$view.css({ left: 0, top: 0 });
5965

6066
this._sx = 0;
6167
this._sy = 0;
@@ -164,35 +170,42 @@ jQuery.widget( "mobile.scrollview", jQuery.mobile.widget, {
164170

165171
var $v = this._$view;
166172

167-
var uct = this.options.useCSSTransform;
173+
var sm = this.options.scrollMethod;
168174

169-
if (uct)
170-
setElementTransform($v, x + "px", y + "px");
171-
else
172-
$v.css({left: x + "px", top: y + "px"});
175+
switch (sm)
176+
{
177+
case "translate":
178+
setElementTransform($v, x + "px", y + "px");
179+
break;
180+
case "position":
181+
$v.css({left: x + "px", top: y + "px"});
182+
break;
183+
case "scroll":
184+
var c = this._$clip[0];
185+
c.scrollLeft = -x;
186+
c.scrollTop = -y;
187+
break;
188+
}
173189

174190
var $vsb = this._$vScrollBar;
175191
var $hsb = this._$hScrollBar;
176192

177-
if ($vsb || $hsb)
193+
if ($vsb)
178194
{
179-
if ($vsb)
180-
{
181-
var $sbt = $vsb.find(".ui-scrollbar-thumb");
182-
if (uct)
183-
setElementTransform($sbt, "0px", -y/$v.height() * $sbt.parent().height() + "px");
184-
else
185-
$sbt.css("top", -y/$v.height()*100 + "%");
186-
}
187-
188-
if ($hsb)
189-
{
190-
var $sbt = $hsb.find(".ui-scrollbar-thumb");
191-
if (uct)
192-
setElementTransform($sbt, -x/$v.width() * $sbt.parent().width() + "px", "0px");
193-
else
194-
$sbt.css("left", -x/$v.width()*100 + "%");
195-
}
195+
var $sbt = $vsb.find(".ui-scrollbar-thumb");
196+
if (sm == "translate")
197+
setElementTransform($sbt, "0px", -y/$v.height() * $sbt.parent().height() + "px");
198+
else
199+
$sbt.css("top", -y/$v.height()*100 + "%");
200+
}
201+
202+
if ($hsb)
203+
{
204+
var $sbt = $hsb.find(".ui-scrollbar-thumb");
205+
if (sm == "translate")
206+
setElementTransform($sbt, -x/$v.width() * $sbt.parent().width() + "px", "0px");
207+
else
208+
$sbt.css("left", -x/$v.width()*100 + "%");
196209
}
197210
},
198211

@@ -230,9 +243,9 @@ jQuery.widget( "mobile.scrollview", jQuery.mobile.widget, {
230243
this._timerID = setTimeout(tfunc, this._timerInterval);
231244
},
232245

233-
_getScrollPosition: function(x, y)
246+
getScrollPosition: function()
234247
{
235-
return { x: this._sx, y: this._sy };
248+
return { x: -this._sx, y: -this._sy };
236249
},
237250

238251
_getScrollHierarchy: function()

experiments/scrollview/scrollview.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ function ResizePageContentHeight(page)
1212

1313
$("[data-role=page]").live("pageshow", function(event) {
1414
var $page = $(this);
15+
16+
// For the demos that use this script, we want the content area of each
17+
// page to be scrollable in the 'y' direction.
18+
1519
$page.find(".ui-content").attr("data-scroll", "y");
20+
21+
// This code that looks for [data-scroll] will eventually be folded
22+
// into the jqm page processing code when scrollview support is "official"
23+
// instead of "experimental".
24+
1625
$page.find("[data-scroll]:not(.ui-scrollview-clip)").each(function(){
1726
var $this = $(this);
1827
// XXX: Remove this check for ui-scrolllistview once we've
@@ -31,10 +40,18 @@ $("[data-role=page]").live("pageshow", function(event) {
3140
if (paging)
3241
opts.pagingEnabled = true;
3342

43+
var method = $this.data("scroll-method");
44+
if (method)
45+
opts.scrollMethod = method;
46+
3447
$this.scrollview(opts);
3548
}
3649
});
3750

51+
// For the demos, we want to make sure the page being shown has a content
52+
// area that is sized to fit completely within the viewport. This should
53+
// also handle the case where pages are loaded dynamically.
54+
3855
ResizePageContentHeight(event.target);
3956
});
4057

experiments/scrollview/sv-test-02.html

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@
4848
});
4949
});
5050

51+
function changeScrollMethod()
52+
{
53+
var val = $("#s_method").val();
54+
var $sv = $("#evtCatcher").scrollview("scrollTo", 0, 0);
55+
if (val == "scroll") {
56+
$sv.css("overflow", "scroll");
57+
}
58+
else {
59+
$sv.css("overflow", "hidden");
60+
}
61+
$sv.data("scrollview").options.scrollMethod = val;
62+
}
63+
5164
var cb_hd_pd,
5265
cb_hd_sp,
5366
cb_hm_pd,
@@ -61,12 +74,6 @@
6174

6275
function getDummyEvent(o)
6376
{
64-
/*
65-
var obj = new Object;
66-
for (var k in o)
67-
obj[k] = o[k];
68-
return obj;
69-
*/
7077
return { _pd: false, _sp: false, preventDefault: function(){ this._pd = true; }, stopPropagation: function(){ this._sp = true; }};
7178
}
7279

@@ -116,6 +123,15 @@ <h1>Scroll View Events Test</h1>
116123
<div data-role="content">
117124
<h3>Test</h3>
118125
<p>This page wraps the _handleDragStart, _handleDragMove, and _handleDragStop events of the scrollview widget so that the checkboxes below can determine how the native event is treated. You can use this page to figure out what events need to be caught and what special treatment is necessary to prevent native scrolling. You can also test the effect of that treatment on form elements within the sample scrollview.</p>
126+
<div data-role="fieldcontain">
127+
<label for="s_method" class="select">Scrolling Method:</label>
128+
<select name="s_method" id="s_method" onchange="changeScrollMethod();">
129+
<option value="translate">CSS3 Transform/Translate</option>
130+
<option value="position">CSS Top/Left Positioning</option>
131+
<option value="scroll">scrollTop/scrollLeft</option>
132+
</select>
133+
</div>
134+
119135
<div data-role="fieldcontain">
120136
<fieldset data-role="controlgroup">
121137
<input type="checkbox" name="cb_hd_pd" id="cb_hd_pd" />

0 commit comments

Comments
 (0)