【uniapp】封装返回顶部的公共组件

在这里插入图片描述
1.根目录components文件里,创建totop文件、totop.vue页面

<template>
	<view class="page">
		<view class="btn" @tap="toTop" :style="{'display':(isTop===false? 'none':'block')}">
			<tui-icon name="arrowup" :size="32" color="#fff" ></tui-icon>
		</view>
	</view>
</template>

<script>
	export default{
		name:"totop",
		props:{
			isTop:{
				type:Boolean,
				default:false
			}
		},
		methods:{
			toTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 200,
				});
			},
		}
	}
</script>
<style scoped>
	.btn {
		position: fixed;
		z-index: 999;
		right: 16px;
		bottom: 100px;
		background-color: #007AFF;
		border-radius: 50%;
		width: 40px;
		height: 40px;
		line-height: 40px;
		text-align: center;
		display: none;
	}
</style>

2.应用到demo.vue

<template>
	<view class="page">
		...
		<totop :isTop="isTop"></totop>
	</view>
</template>
<script>
	export default{
		data(){
			return{
				isTop: false,
			}
		},
		methods:{
			onPageScroll(e) {
				this.isTop= e.scrollTop > 10?true:false;
			},
		},
	}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我来给你介绍一下如何封装一个listcell组件。 1. 创建组件文件 首先,在uniapp的项目中,选择需要封装listcell组件的页面。在该页面所在的目录下,创建一个名为list-cell的组件文件夹。 2. 编写组件结构 在list-cell文件夹下,创建一个名为list-cell.vue的Vue单文件组件。在该组件中,可以编写自定义的listcell组件结构。 例如: ```html <template> <view class="list-cell"> <view class="list-cell__left">{{ left }}</view> <view class="list-cell__right">{{ right }}</view> </view> </template> <script> export default { name: 'ListCell', props: { left: String, right: String } } </script> <style> .list-cell { display: flex; justify-content: space-between; align-items: center; padding: 20rpx; background-color: #fff; border-bottom: 1px solid #ddd; } .list-cell__left { font-size: 30rpx; color: #333; } .list-cell__right { font-size: 28rpx; color: #666; } </style> ``` 3. 在页面中引入组件 在需要使用listcell组件的页面中,引入该组件。例如,在index.vue页面中,可以引入listcell组件。 ```html <template> <view> <list-cell left="姓名" right="张三"></list-cell> <list-cell left="年龄" right="18"></list-cell> <list-cell left="性别" right="男"></list-cell> </view> </template> <script> import ListCell from '@/components/list-cell/list-cell.vue' export default { components: { ListCell }, } </script> ``` 这样,当在index.vue页面中使用list-cell组件时,就可以自动引入并使用该组件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值