提交 9c37507a 编写于 作者: D DCloud_LXH

feat: scroll-view

上级 b9214406
import { plusReady } from '@dcloudio/uni-shared'
let webview
let pullToRefreshStyle
export function initScrollBounce() {
plusReady(() => {
if (!webview) {
webview = plus.webview.currentWebview()
}
if (!pullToRefreshStyle) {
pullToRefreshStyle = (webview.getStyle() || {}).pullToRefresh || {}
}
})
}
export function disableScrollBounce({ disable }) {
if (pullToRefreshStyle && pullToRefreshStyle.support) {
webview.setPullToRefresh(
Object.assign({}, pullToRefreshStyle, {
support: !disable,
})
)
}
}
......@@ -6,15 +6,15 @@ export function Friction(e) {
this._startTime = 0
}
Friction.prototype.set = function(e, t) {
Friction.prototype.set = function (e, t) {
this._x = e
this._v = t
this._startTime = new Date().getTime()
}
Friction.prototype.setVelocityByEnd = function(e) {
Friction.prototype.setVelocityByEnd = function (e) {
this._v = ((e - this._x) * this._dragLog) / (Math.pow(this._drag, 100) - 1)
}
Friction.prototype.x = function(e) {
Friction.prototype.x = function (e) {
if (e === undefined) {
e = (new Date().getTime() - this._startTime) / 1e3
}
......@@ -26,7 +26,7 @@ Friction.prototype.x = function(e) {
this._dt = e
return this._x + (this._v * t) / this._dragLog - this._v / this._dragLog
}
Friction.prototype.dx = function(e) {
Friction.prototype.dx = function (e) {
if (e === undefined) {
e = (new Date().getTime() - this._startTime) / 1e3
}
......@@ -38,30 +38,30 @@ Friction.prototype.dx = function(e) {
this._dt = e
return this._v * t
}
Friction.prototype.done = function() {
Friction.prototype.done = function () {
return Math.abs(this.dx()) < 3
}
Friction.prototype.reconfigure = function(e) {
Friction.prototype.reconfigure = function (e) {
var t = this.x()
var n = this.dx()
this._drag = e
this._dragLog = Math.log(e)
this.set(t, n)
}
Friction.prototype.configuration = function() {
Friction.prototype.configuration = function () {
var e = this
return [
{
label: 'Friction',
read: function() {
read: function () {
return e._drag
},
write: function(t) {
write: function (t) {
e.reconfigure(t)
},
min: 0.001,
max: 0.1,
step: 0.001
}
step: 0.001,
},
]
}
......@@ -10,13 +10,13 @@ export function Scroll(extent, friction, spring) {
this._springOffset = 0
}
Scroll.prototype.snap = function(e, t) {
Scroll.prototype.snap = function (e, t) {
this._springOffset = 0
this._springing = true
this._spring.snap(e)
this._spring.setEnd(t)
}
Scroll.prototype.set = function(e, t) {
Scroll.prototype.set = function (e, t) {
this._friction.set(e, t)
if (e > 0 && t >= 0) {
this._springOffset = 0
......@@ -35,7 +35,7 @@ Scroll.prototype.set = function(e, t) {
}
this._startTime = new Date().getTime()
}
Scroll.prototype.x = function(e) {
Scroll.prototype.x = function (e) {
if (!this._startTime) {
return 0
}
......@@ -59,25 +59,25 @@ Scroll.prototype.x = function(e) {
}
return t
}
Scroll.prototype.dx = function(e) {
Scroll.prototype.dx = function (e) {
var t = 0
t =
this._lastTime === e
? this._lastDx
: this._springing
? this._spring.dx(e)
: this._friction.dx(e)
? this._spring.dx(e)
: this._friction.dx(e)
this._lastTime = e
this._lastDx = t
return t
}
Scroll.prototype.done = function() {
Scroll.prototype.done = function () {
return this._springing ? this._spring.done() : this._friction.done()
}
Scroll.prototype.setVelocityByEnd = function(e) {
Scroll.prototype.setVelocityByEnd = function (e) {
this._friction.setVelocityByEnd(e)
}
Scroll.prototype.configuration = function() {
Scroll.prototype.configuration = function () {
var e = this._friction.configuration()
e.push.apply(e, this._spring.configuration())
return e
......
......@@ -26,12 +26,12 @@ function i(scroll, t, n) {
}
var o = {
id: 0,
cancelled: false
cancelled: false,
}
i(o, scroll, t, n)
return {
cancel: r.bind(null, o),
model: scroll
model: scroll,
}
}
......@@ -62,7 +62,7 @@ export function Scroller(element, options) {
this.updatePosition()
}
Scroller.prototype.onTouchStart = function() {
Scroller.prototype.onTouchStart = function () {
this._startPosition = this._position
this._lastChangePos = this._startPosition
if (this._startPosition > 0) {
......@@ -79,7 +79,7 @@ Scroller.prototype.onTouchStart = function() {
}
this.updatePosition()
}
Scroller.prototype.onTouchMove = function(x, y) {
Scroller.prototype.onTouchMove = function (x, y) {
var startPosition = this._startPosition
if (this._enableX) {
startPosition += x
......@@ -96,7 +96,7 @@ Scroller.prototype.onTouchMove = function(x, y) {
this.updatePosition()
this.dispatchScroll()
}
Scroller.prototype.onTouchEnd = function(e, r, o) {
Scroller.prototype.onTouchEnd = function (e, r, o) {
if (
this._enableSnap &&
this._position > -this._extent &&
......@@ -178,7 +178,7 @@ Scroller.prototype.onTouchEnd = function(e, r, o) {
}
)
}
Scroller.prototype.onTransitionEnd = function() {
Scroller.prototype.onTransitionEnd = function () {
this._element.style.transition = ''
this._element.style.webkitTransition = ''
this._element.removeEventListener('transitionend', this._onTransitionEnd)
......@@ -191,7 +191,7 @@ Scroller.prototype.onTransitionEnd = function() {
}
this.dispatchScroll()
}
Scroller.prototype.snap = function() {
Scroller.prototype.snap = function () {
var e = this._itemSize
var t = this._position % e
var i =
......@@ -208,7 +208,7 @@ Scroller.prototype.snap = function() {
}
}
}
Scroller.prototype.scrollTo = function(e, t) {
Scroller.prototype.scrollTo = function (e, t) {
if (this._animation) {
this._animation.cancel()
this._scrolling = false
......@@ -230,7 +230,7 @@ Scroller.prototype.scrollTo = function(e, t) {
this._element.addEventListener('transitionend', this._onTransitionEnd)
this._element.addEventListener('webkitTransitionEnd', this._onTransitionEnd)
}
Scroller.prototype.dispatchScroll = function() {
Scroller.prototype.dispatchScroll = function () {
if (
typeof this._options.onScroll === 'function' &&
Math.round(this._lastPos) !== Math.round(this._position)
......@@ -243,13 +243,13 @@ Scroller.prototype.dispatchScroll = function() {
scrollHeight: this._scrollHeight || this._element.offsetHeight,
scrollWidth: this._scrollWidth || this._element.offsetWidth,
offsetHeight: this._element.parentElement.offsetHeight,
offsetWidth: this._element.parentElement.offsetWidth
}
offsetWidth: this._element.parentElement.offsetWidth,
},
}
this._options.onScroll(e)
}
}
Scroller.prototype.update = function(e, t, n) {
Scroller.prototype.update = function (e, t, n) {
var i = 0
var r = this._position
if (this._enableX) {
......@@ -288,7 +288,7 @@ Scroller.prototype.update = function(e, t, n) {
this._extent = i
this._scroll._extent = i
}
Scroller.prototype.updatePosition = function() {
Scroller.prototype.updatePosition = function () {
var transform = ''
if (this._enableX) {
transform = 'translateX(' + this._position + 'px) translateZ(0)'
......@@ -300,6 +300,6 @@ Scroller.prototype.updatePosition = function() {
this._element.style.webkitTransform = transform
this._element.style.transform = transform
}
Scroller.prototype.isScrolling = function() {
Scroller.prototype.isScrolling = function () {
return this._scrolling || this._snapping
}
......@@ -15,7 +15,7 @@ export function Spring(e, t, n) {
this._startTime = 0
}
Spring.prototype._solve = function(e, t) {
Spring.prototype._solve = function (e, t) {
var n = this._c
var i = this._m
var r = this._k
......@@ -25,13 +25,13 @@ Spring.prototype._solve = function(e, t) {
const s = e
const l = t / (a * e)
return {
x: function(e) {
x: function (e) {
return (s + l * e) * Math.pow(Math.E, a * e)
},
dx: function(e) {
dx: function (e) {
var t = Math.pow(Math.E, a * e)
return a * (s + l * e) * t + l * t
}
},
}
}
if (o > 0) {
......@@ -40,7 +40,7 @@ Spring.prototype._solve = function(e, t) {
const l = (t - c * e) / (u - c)
const s = e - l
return {
x: function(e) {
x: function (e) {
let t
let n
if (e === this._t) {
......@@ -56,7 +56,7 @@ Spring.prototype._solve = function(e, t) {
}
return s * t + l * n
},
dx: function(e) {
dx: function (e) {
let t
let n
if (e === this._t) {
......@@ -71,7 +71,7 @@ Spring.prototype._solve = function(e, t) {
n = this._powER2T = Math.pow(Math.E, u * e)
}
return s * c * t + l * u * n
}
},
}
}
var d = Math.sqrt(4 * i * r - n * n) / (2 * i)
......@@ -82,34 +82,34 @@ Spring.prototype._solve = function(e, t) {
var l = (t - a * e) / d
return {
x: function(e) {
x: function (e) {
return (
Math.pow(Math.E, a * e) * (s * Math.cos(d * e) + l * Math.sin(d * e))
)
},
dx: function(e) {
dx: function (e) {
var t = Math.pow(Math.E, a * e)
var n = Math.cos(d * e)
var i = Math.sin(d * e)
return t * (l * d * n - s * d * i) + a * t * (l * i + s * n)
}
},
}
}
Spring.prototype.x = function(e) {
Spring.prototype.x = function (e) {
if (e === undefined) {
e = (new Date().getTime() - this._startTime) / 1e3
}
return this._solution ? this._endPosition + this._solution.x(e) : 0
}
Spring.prototype.dx = function(e) {
Spring.prototype.dx = function (e) {
if (e === undefined) {
e = (new Date().getTime() - this._startTime) / 1e3
}
return this._solution ? this._solution.dx(e) : 0
}
Spring.prototype.setEnd = function(e, t, n) {
Spring.prototype.setEnd = function (e, t, n) {
if (!n) {
n = new Date().getTime()
}
......@@ -136,25 +136,25 @@ Spring.prototype.setEnd = function(e, t, n) {
}
}
}
Spring.prototype.snap = function(e) {
Spring.prototype.snap = function (e) {
this._startTime = new Date().getTime()
this._endPosition = e
this._solution = {
x: function() {
x: function () {
return 0
},
dx: function() {
dx: function () {
return 0
}
},
}
}
Spring.prototype.done = function(e) {
Spring.prototype.done = function (e) {
if (!e) {
e = new Date().getTime()
}
return o(this.x(), this._endPosition, 0.4) && a(this.dx(), 0.4)
}
Spring.prototype.reconfigure = function(e, t, n) {
Spring.prototype.reconfigure = function (e, t, n) {
this._m = e
this._k = t
this._c = n
......@@ -163,13 +163,13 @@ Spring.prototype.reconfigure = function(e, t, n) {
this._startTime = new Date().getTime()
}
}
Spring.prototype.springConstant = function() {
Spring.prototype.springConstant = function () {
return this._k
}
Spring.prototype.damping = function() {
Spring.prototype.damping = function () {
return this._c
}
Spring.prototype.configuration = function() {
Spring.prototype.configuration = function () {
function e(e, t) {
e.reconfigure(1, t, e.damping())
}
......@@ -183,14 +183,14 @@ Spring.prototype.configuration = function() {
read: this.springConstant.bind(this),
write: e.bind(this, this),
min: 100,
max: 1e3
max: 1e3,
},
{
label: 'Damping',
read: this.damping.bind(this),
write: t.bind(this, this),
min: 1,
max: 500
}
max: 500,
},
]
}
......@@ -2,11 +2,11 @@ import { Scroller } from './Scroller'
export default {
methods: {
initScroller: function(element, options) {
initScroller: function (element, options) {
this._touchInfo = {
trackingID: -1,
maxDy: 0,
maxDx: 0
maxDx: 0,
}
this._scroller = new Scroller(element, options)
this.__handleTouchStart = this._handleTouchStart.bind(this)
......@@ -14,19 +14,19 @@ export default {
this.__handleTouchEnd = this._handleTouchEnd.bind(this)
this._initedScroller = true
},
_findDelta: function(event) {
_findDelta: function (event) {
var touchInfo = this._touchInfo
return event.detail.state === 'move' || event.detail.state === 'end'
? {
x: event.detail.dx,
y: event.detail.dy
y: event.detail.dy,
}
: {
x: event.screenX - touchInfo.x,
y: event.screenY - touchInfo.y
y: event.screenY - touchInfo.y,
}
},
_handleTouchStart: function(e) {
_handleTouchStart: function (e) {
var t = this._touchInfo
var n = this._scroller
if (n) {
......@@ -48,10 +48,10 @@ export default {
if (n.onTouchStart) {
n.onTouchStart()
}
event.preventDefault()
e.preventDefault()
}
},
_handleTouchMove: function(event) {
_handleTouchMove: function (event) {
var touchInfo = this._touchInfo
if (touchInfo.trackingID !== -1) {
event.preventDefault()
......@@ -80,7 +80,7 @@ export default {
}
}
},
_handleTouchEnd: function(event) {
_handleTouchEnd: function (event) {
var touchInfo = this._touchInfo
if (touchInfo.trackingID !== -1) {
event.preventDefault()
......@@ -92,7 +92,7 @@ export default {
var r = touchInfo.historyTime.length
var o = {
x: 0,
y: 0
y: 0,
}
if (r > 2) {
for (
......@@ -121,6 +121,6 @@ export default {
}
}
}
}
}
},
},
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册