uni-app 完美支持安卓和ios 的 js 图片跟着滚动条滚动

<template>
    <div class="content" ref="con">
        <img ref="dd" class="directory_bg" src="../../static/image/directory/directory_bg.png" />
		<scroll-view class="directory_list" scroll-y @scroll='soll'>
			<view class="" v-for="(item,index) in 82" :key="index" style="height: 100px;">
				{{item}}--{{index}}
			</view>
		</scroll-view>
    </div>
</template>

<script>
export default {
    data(){
        return {
			imgH:0,
			listH: 0,
			conH: 0
        }  
    },
	onReady() {
		this.getInfo()
	},
	mounted(){
		// console.log(this.$refs.dd.offsetHeight ,this.$refs.con.offsetHeight)
		// this.imgH = this.$refs.dd.offsetHeight - this.$refs.con.offsetHeight; //计算溢出高度
		this.conH = this.$refs.con.offsetHeight;
	},
	methods:{
		soll(event){
			console.log(event)
			if(!this.imgH && this.$refs.dd.offsetHeight) {
				this.imgH = this.$refs.dd.offsetHeight - this.conH;
			}
			 event.preventDefault()
			const {scrollHeight, deltaY, scrollTop} = event.detail;
			// 
			/**
			 * 
			 * (图片溢出的高) =  图片总高度- 视图高度
			 * (总的滚动条距离顶部距离) = (滑到最底部的时滚动条距离顶部的距离) = (总的列表高度 - 可视区域的高度)
			 * 当前滑动的位置滚动条距离顶部的占比 = (图片溢出的高 / (总的滚动条距离顶部距离))
			 * 当前scrollTop(当前滚动条距离顶部的高度) * (图片溢出的高 / (总的滚动条距离顶部距离))
			 */
			 //计算公式
			this.$refs.dd.style.marginTop = -(scrollTop * (this.imgH / (scrollHeight - this.$refs.con.offsetHeight))) +'px'
		},
		getInfo(){
		}
	}
}
</script>

<style lang='less' scoped>
.content{
	height: 100vh;
	overflow: hidden;
	.directory_bg{
		margin-top: 0;
		width: 100%;
		transition: .1s;
	}
	.directory_list{
		position: absolute;
		z-index: 1;
		top: 0;
		height: 100%;
	}
}
</style>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值