{{item}}_uniapp scrollview滚动到最右边">
当前位置:   article > 正文

uniApp页面滚动到指定位置_uniapp scrollview滚动到最右边

uniapp scrollview滚动到最右边
Html
<template>
	<view>
		<view class="topSel">
			<view class="nav">
				<view v-for="(item,navIndex) in nav" :key="navIndex" class="navList" :class="currentNav==navIndex? 'ac':''" @tap="selNav(navIndex)">{{item}}</view>
			</view>
		</view>
		<view class="Html">Html</view>
		<view class="Css">Css</view>
		<view class="Javascript">Javascript</view>
	</view>
</template>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
Javascript
<script>
	export default {
		data() {
			return {
				nav: ["Html", "Css", "Javascript"],
				currentNav: 0,
				HtmlTop:0,
				JavascriptTop:0,
				CssTop:0
			};
		},
		onShow(){
			this.$Tool.getRect(".Html").then(res => {
				//此处减50是因为page设置了padding,依个人情况而定没设置则不需要减
				this.HtmlTop = res.top-50
			})
			this.$Tool.getRect(".Css").then(res => {
				this.CssTop = res.top-50
			})
			this.$Tool.getRect(".Javascript").then(res => {
				this.JavascriptTop = res.top-50
			})
		},
		methods: {
			selNav(index) {
				this.currentNav = index
				switch (index) {
					case 0:
						uni.pageScrollTo({
							scrollTop: this.HtmlTop
						})
						break;
					case 1:
						uni.pageScrollTo({
							scrollTop: this.CssTop
						})
						break;
					case 2:
						uni.pageScrollTo({
							scrollTop: this.JavascriptTop
						})
						break;
				}
			}
		}
	};
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
Css
<style lang="scss">
	page {
		padding-top: 100rpx;
	}

	.topSel {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100rpx;
		background-color: rgba(255,255,255,.2);
		.nav {
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 60%;
			margin: 0 auto;
			font-size: 31rpx;
			color: #666;
			line-height: 70rpx;
			.ac {
				border-bottom: 2px transparent solid;
				border-image: linear-gradient(to right, #ffb759, #ff3251) 1 10;
				color: #ff3251;
				line-height: 70rpx;
			}
		}
	}
	.Html{
		width: 750rpx;
		height: 2000rpx;
		background-color: #1CBBB4;
		color: #fff;
	}
	.Javascript {
		width: 750rpx;
		height: 2000rpx;
		background-color: pink;
		color: #fff;
	}
	.Css{
		width: 750rpx;
		height: 2000rpx;
		background-color: #00BFFF;
		color: #fff;
	}
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
Tips

1.this.$Tool.getRect()方法参考:uniApp获取页面元素大小和位置
2.uni.createSelectorQuery().select()方法获取到的单位默认是px

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/312483
推荐阅读
相关标签