Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 33602a4

Browse files
committed
Implemented view paging for the scrollview. This feature is only enabled for horizontal and vertical scrollviews. To use, pass "xp" or "yp" for the @data-scroll attribute.
Fixed a typo/bug in scrollview.js that was causing the paging bool to be set improperly. Turned on scrollview paging in scrollview-direction.html. Items still left to do: - Code refactoring/cleanup. - Modify the code so that scroll offsets are stored as positive values. This will make it easier for folks to understand.
1 parent 63627e5 commit 33602a4

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

experiments/scrollview/jquery.mobile.scrollview.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ jQuery.widget( "mobile.scrollview", jQuery.mobile.widget, {
2626

2727
eventType: $.support.touch ? "touch" : "mouse",
2828

29-
showScrollBars: true
29+
showScrollBars: true,
30+
31+
pagingEnabled: false
3032
},
3133

3234
_makePositioned: function($ele)
@@ -300,6 +302,18 @@ jQuery.widget( "mobile.scrollview", jQuery.mobile.widget, {
300302
this._$vScrollBar.find(".ui-scrollbar-thumb").css("height", (ch >= vh ? "100%" : Math.floor(ch/vh*100)+ "%"));
301303
}
302304

305+
var svdir = this.options.direction;
306+
307+
this._pageDelta = 0;
308+
this._pageSize = 0;
309+
this._pagePos = 0;
310+
311+
if (this.options.pagingEnabled && (svdir == "x" || svdir == "y"))
312+
{
313+
this._pageSize = svdir == "x" ? cw : ch;
314+
this._pagePos = svdir == "x" ? this._sx : this._sy;
315+
this._pagePos -= this._pagePos % this._pageSize;
316+
}
303317
this._lastMove = 0;
304318
this._enableTracking();
305319

@@ -332,6 +346,7 @@ jQuery.widget( "mobile.scrollview", jQuery.mobile.widget, {
332346

333347
var dx = ex - this._lastX;
334348
var dy = ey - this._lastY;
349+
var svdir = this.options.direction;
335350

336351
if (!this._directionLock)
337352
{
@@ -352,7 +367,6 @@ jQuery.widget( "mobile.scrollview", jQuery.mobile.widget, {
352367
dir = "x";
353368
}
354369

355-
var svdir = this.options.direction;
356370
if (svdir && dir && svdir != dir)
357371
{
358372
// This scrollview can't handle the direction the user
@@ -427,6 +441,20 @@ jQuery.widget( "mobile.scrollview", jQuery.mobile.widget, {
427441

428442
}
429443

444+
if (this.options.pagingEnabled && (svdir == "x" || svdir == "y"))
445+
{
446+
if (this._doSnapBackX || this._doSnapBackY)
447+
this._pageDelta = 0;
448+
else
449+
{
450+
var opos = this._pagePos;
451+
var cpos = svdir == "x" ? newX : newY;
452+
var delta = svdir == "x" ? dx : dy;
453+
454+
this._pageDelta = (opos > cpos && delta < 0) ? this._pageSize : ((opos < cpos && delta > 0) ? -this._pageSize : 0);
455+
}
456+
}
457+
430458
this._didDrag = true;
431459
this._lastX = ex;
432460
this._lastY = ey;
@@ -451,8 +479,20 @@ jQuery.widget( "mobile.scrollview", jQuery.mobile.widget, {
451479

452480
var sx = (this._hTracker && this._speedX && doScroll) ? this._speedX : (this._doSnapBackX ? 1 : 0);
453481
var sy = (this._vTracker && this._speedY && doScroll) ? this._speedY : (this._doSnapBackY ? 1 : 0);
454-
455-
if (sx || sy)
482+
483+
var svdir = this.options.direction;
484+
if (this.options.pagingEnabled && (svdir == "x" || svdir == "y") && !this._doSnapBackX && !this._doSnapBackY)
485+
{
486+
var x = this._sx;
487+
var y = this._sy;
488+
if (svdir == "x")
489+
x = -this._pagePos + this._pageDelta;
490+
else
491+
y = -this._pagePos + this._pageDelta;
492+
493+
this.scrollTo(x, y, this.options.snapbackDuration);
494+
}
495+
else if (sx || sy)
456496
this._startMScroll(sx, sy);
457497
else
458498
this._hideScrollBars();

experiments/scrollview/scrollview-direction.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,8 @@ <h4>Vertical Scrollview</h4>
593593
<div class="square">y</div>
594594
</div>
595595
<h4>Scrollview Paging</h4>
596-
<p>Not implemented yet.</p><p>A scrollview can be set up so that it scrolls by pages. This feature is only enabled for horizontal or vertical scrollviews.</p>
597-
<div id="ex4" data-scroll="true" class="threeByThree">
596+
<p>A scrollview can be set up so that it scrolls by pages. This feature is only enabled for horizontal or vertical scrollviews. Use data-scroll=&quot;xp&quot; or data-scroll=&quot;yp&quot; to turn on paging. The following scrollview pages horizontally.</p>
597+
<div id="ex4" data-scroll="xp" class="threeByThree">
598598
<div class="square">a</div>
599599
<div class="square">b</div>
600600
<div class="square">c</div>

experiments/scrollview/scrollview.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ function ResizePageContentHeight(page)
1212

1313
$("[data-role=page]").live("pageshow", function(event) {
1414
var $page = $(this);
15-
$page.find(".ui-content, [data-scroll]").not(".ui-scrollview-clip").each(function(){
15+
$page.find(".ui-content").attr("data-scroll", "y");
16+
$page.find("[data-scroll]:not(.ui-scrollview-clip)").each(function(){
1617
var $this = $(this);
1718
// XXX: Remove this check for ui-scrolllistview once we've
1819
// integrated list divider support into the main scrollview class.
@@ -21,14 +22,14 @@ $("[data-role=page]").live("pageshow", function(event) {
2122
else
2223
{
2324
var st = $this.data("scroll") + "";
24-
var snap = st && st.search(/^[xy]s$/ != -1);
25+
var paging = st && st.search(/^[xy]p$/) != -1;
2526
var dir = st && st.search(/^[xy]/) != -1 ? st.charAt(0) : null;
2627

2728
var opts = {};
2829
if (dir)
2930
opts.direction = dir;
30-
if (snap)
31-
opts.snap = "viewport";
31+
if (paging)
32+
opts.pagingEnabled = true;
3233

3334
$this.scrollview(opts);
3435
}

0 commit comments

Comments
 (0)