【微信小程序】实现类似WEB端【返回顶部】功能
2024-08-29 11:46:05
1、原理:利用小程序自带的<scroll-view>组件,该组件的bindScroll和scroll-top方法、属性进行联合操作
2、效果图:
3、wxml:
<scroll-view scroll-y="true" style="height:{{scrollHeight}}px;" bindscroll="scroll" scroll-top="{{scrollTop}}" >
<text class="title">{{list.art_title}}</text>
<p class="oneline">{{list.seo_desc}}</p> <!--回到顶部 -->
<view class="com-widget-goTop" bindtap="goTop" wx:if="{{floorstatus}}">
<view class="icon-gotop">
顶部
</view>
</view> </scroll-view>
4、js:
scroll: function (e, res) {
// 容器滚动时实时获取滚动高度:e.detail.scrollTop
//当超过限定值如500时,展示【返回顶部图片】,通过标识"floorstatus"的真假判断展示、隐藏
if (e.detail.scrollTop > 500) {
this.setData({
floorstatus: true
});
}
else {
this.setData({
floorstatus: false
});
}
},
// 点击图片,使scrollTop属性==0,回到顶部
goTop: function (e) {
this.setData({
scrollTop: 0
})
},
5、wxss:
/*回到顶部 */
.com-widget-goTop {
position: fixed;
bottom: 125px;
right: 5px;
background: rgba(0,0,0,0.48);
border-radius: 50%;
overflow: hidden;
z-index:;
}
.com-widget-goTop .icon-gotop{
background-color: rgba(0,0,0,0.8);
display: inline-block;
width: 50px;
height: 50px;
line-height: 68px;
font-size: 12px;
color: #ffffff;
text-align: center;
border-radius: 50%;
background:url(http://m.dev.vd.cn/static/xcx/v1/goo/w_2-3451cc437e.png) no-repeat center -1110px;
background-size:50px auto;
}
/*end*/
最新文章
- Bootstrap-Select 动态加载数据的小记
- 在php中定义常量时,const与define的区别?
- 理解C# 4 dynamic(2) – ExpandoObject的使用
- 浅谈c语言的指针
- jvm基础笔记
- 二叉树的遍历(递归,迭代,Morris遍历)
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(1)
- spoj 274
- 英文论文写作之讨论与结论Discussion and Conclusion
- IEnumerable中的 Any方法
- android在桌面弹出一个窗口
- lucene 总结收集(url)
- Chrome浏览器扩展开发系列之十二:Content Scripts
- DispatcherTimer和Timer(计时器)
- Android 程序结构
- Python filter() 函数
- Integer 函数传参实现值交换
- 模块化jQuery的方法
- Could not install the app on the device, read the error above for details. Make sure you have an Android emulator running or a device connected and have set up your Android development environment:
- 人人中的 shiro权限管理 简单说明