提交 48e993db 编写于 作者: DCloud_JSON's avatar DCloud_JSON

更新 示例项目 pull-zoom-image 向上滚动时,头部图片不缩放,跟随内容移动

上级 365c3afc
<template> <template>
<view @click="back" class="nav-back"> <view @click="back" class="nav-back">
<image class="back-img" src="/static/template/scroll-fold-nav/back.png" mode="widthFix"></image> <image class="back-img" src="/static/template/scroll-fold-nav/back.png" mode="widthFix"></image>
</view> </view>
<view class="root" @touchstart="touchstart($event as TouchEvent)" @touchmove="touchmove($event as TouchEvent)" <view style="flex:1" @touchstart="touchstart($event as TouchEvent)" @touchmove="touchmove($event as TouchEvent)" @touchend="touchend" @touchcancel="touchend">
@touchend="touchend" @touchcancel="touchend"> <image class="head-img" ref="head-img" src="../../../static/template/pull-zoom-image/head-img.jpg" mode="scaleToFill"></image>
<image class="head-img" ref="head-img" src="../../../static/template/pull-zoom-image/head-img.jpg"
mode="scaleToFill"></image>
<view class="body" ref="body"> <view class="body" ref="body">
<view class="user-info"> <view class="user-info">
<image class="user-avatar" src="../../../static/test-image/logo.png" mode="widthFix"></image> <image class="user-avatar" src="../../../static/test-image/logo.png" mode="widthFix"></image>
...@@ -40,52 +38,54 @@ ...@@ -40,52 +38,54 @@
5. 请向上\向下拖动页面观察效果。 5. 请向上\向下拖动页面观察效果。
</text> </text>
</view> </view>
<view class="content-item" v-for="(item,index) in 10" :key="index">
<!-- <view class="content-item" v-for="(item,index) in 6" :key="index">
<text class="text">content-{{item}}</text> <text class="text">content-{{item}}</text>
</view> -->
</view> </view>
</view> </view>
</view> </view>
</view>
</template> </template>
<script> <script>
let sY : number = 0, let sY : number = 0,
touchstartAfter : boolean = false; dY : number = 0,
maxScrollTop:number = 0;
export default { export default {
data() { data() {
return { return {
$nodeMap: new Map<string, INode>(), $nodeMap: new Map<string, INode>(),
y: 0 as number y: 0 as number,
scrollTop:0 as number
}
},
onReady() {
uni.getSystemInfo({
success: (e) => {
maxScrollTop = 1425 - e.screenHeight
} }
})
}, },
methods: { methods: {
doMove() { doMove() {
// console.log('doMove',this.y);
if(this.y > 0){
this.setINodeStyle("head-img", 'transform', `scale(${(this.y > 0 ? this.y : 0) / 200 + 1})`) this.setINodeStyle("head-img", 'transform', `scale(${(this.y > 0 ? this.y : 0) / 200 + 1})`)
}else{
this.setINodeStyle("head-img", 'transform', `translateY(${this.y})`)
}
this.setINodeStyle("body", 'transform', `translateY(${this.y})`) this.setINodeStyle("body", 'transform', `translateY(${this.y})`)
}, },
touchstart(e : TouchEvent) { touchstart(e : TouchEvent) {
// console.log('touchstart')
if (this.y !== 0) {
return
}
sY = e.touches[0].screenY; sY = e.touches[0].screenY;
touchstartAfter = true
}, },
touchmove(e : TouchEvent) { touchmove(e : TouchEvent) {
// console.log('touchmove')
if (!touchstartAfter) {
return
}
this.y += e.touches[0].screenY - sY; this.y += e.touches[0].screenY - sY;
dY = e.touches[0].screenY - sY
sY = e.touches[0].screenY; sY = e.touches[0].screenY;
this.doMove() this.doMove()
}, },
touchend() { touchend() {
// console.log('touchend') // console.log('touchend')
touchstartAfter = false
function moveTo(y : number, callback : () => void, speed : number = 10) { function moveTo(y : number, callback : () => void, speed : number = 10) {
let interval : number = 0 let interval : number = 0
let acceleration : number = 1 let acceleration : number = 1
...@@ -99,20 +99,39 @@ ...@@ -99,20 +99,39 @@
} else { } else {
this.y += dy / speed * acceleration this.y += dy / speed * acceleration
} }
// 滚动边界限制
if(this.y < maxScrollTop*-1){
this.y = maxScrollTop*-1
}
if(this.y > 0){
this.y = 0
}
// console.log('this.y',this.y);
this.doMove() this.doMove()
if (this.y == y) { if (this.y == y || this.y == maxScrollTop*-1 || this.y == 0) {
clearInterval(interval) clearInterval(interval)
callback() callback()
} }
}, 16) }, 16)
} }
// console.log('dY',dY);
if(this.y < 0){
// console.log(dY);
moveTo(this.y + dY * 20, () => {
// console.log('done');
},5)
}else{
const bounceY = (this.y * -0.03).toInt() const bounceY = (this.y * -0.03).toInt()
console.log('bounceY', bounceY); // console.log('bounceY', bounceY);
moveTo(bounceY, () => { moveTo(bounceY, () => {
moveTo(0, () => { moveTo(0, () => {
console.log('done'); // console.log('done');
}, 30) }, 30)
}) })
}
}, },
// 工具方法,用于快速设置 INode 的 style // 工具方法,用于快速设置 INode 的 style
setINodeStyle(refName : string, propertyName : string, propertyStyle : any) : void { setINodeStyle(refName : string, propertyName : string, propertyStyle : any) : void {
...@@ -143,21 +162,17 @@ ...@@ -143,21 +162,17 @@
</script> </script>
<style> <style>
.root {
flex: 1;
}
.head-img { .head-img {
height: 350px; height: 350px;
width: 1000rpx; /* width: 1000rpx; */
margin-left: -125rpx; margin-left: -125rpx;
margin-top: -50px; margin-top: -50px;
background-color: #aab8d9; background-color: #aab8d9;
} }
.body { .body {
flex: 1;
margin-top: -200px; margin-top: -200px;
width: 750rpx;
} }
.user-info { .user-info {
...@@ -191,28 +206,27 @@ ...@@ -191,28 +206,27 @@
.content { .content {
background-color: #FFF; background-color: #FFF;
border-radius: 10px; border-radius: 10px;
flex: 1;
padding: 15px; padding: 15px;
} }
.content-item { .content-item {
padding: 10px; padding: 15px;
margin-bottom: 2px; margin-bottom: 16px;
background-color: #fff; background-color: #fff;
border-radius: 5px; border-radius: 5px;
border: 1px solid rgba(220, 220, 220, 0.1); border: 1px solid rgba(220, 220, 220, 0.5);
} }
.text { .text {
font-size: 12px; font-size: 14px;
color: #666; color: #666;
line-height: 20px; line-height: 20px;
} }
.nav-back { .nav-back {
position: absolute; position: absolute;
top: 20px; top: 30px;
left: 8px; left: 10px;
background-color: rgba(220, 220, 220, 0.3); background-color: rgba(220, 220, 220, 0.3);
border-radius: 100px; border-radius: 100px;
width: 28px; width: 28px;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册