登录/注册

首页 资讯 微信小程序研发,制作回到顶部按钮

微信小程序研发,制作回到顶部按钮

资讯来源:蜂鸟

微信小程序,小程序开发

2020/05/12 11:23

1883

文章页标题下640*60

我们先看一下效果吧,直接上图。

第一种情况,当页面在顶部的时候,回到顶部按钮是不会出现的。


第二种情况,当页面在离开顶部一定距离的时候,回到顶部按钮出现


接下就是对代码的分析了:
在这里我们如果要使用滚动事件的话,小程序规定 最外层一定要使用scroll-view标签进行包裹,然后在设置scroll-y=”true” 意思是允许页面了纵向滚动,scroll-top是滚动到顶部做处理,一般绑定一个事件,bindscrolltolower同样的原理,滚动到底部做处理,bindscroll表示在滚动的时候出发这个事件。下面WXML内部的话,就是我们回到顶部的按钮设置,我们在点击它时绑定一个事件goTop,让他的滚动高度等于0,这样它就回到顶部了。

WXML代码:

 

    //*********************

     

           

               顶部

           

     

     //*********************

 

JS代码:

//回到顶部按钮

Page({

data: {

   scrollTop: 0

   },

goTop: function(e){

   this.setData({

       scrollTop:0

   })

},

scroll:function(e,res){

// 容器滚动时将此时的滚动距离赋值给 this.data.scrollTop

if(e.detail.scrollTop > 500){

    this.setData({

       floorstatus: true

    });

}else {

    this.setData({

       floorstatus: false

    });

   }

   })

WXSS代码:

bigWrap{

background:#eee;

}

/goTop回到顶部图标start/

.com-widget-goTop {

position: fixed;

bottom: 125px;

right: 5px;

background: rgba(0,0,0,0.48);

border-radius: 50%;

overflow: hidden;

z-index: 500;

}

.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;

-webkit-background-size: 50px auto;

}


文章页底部820*135

发布小程序

文章投稿

投稿邮箱:

info@appbulo.com

官方微信

扫一扫关注 小程序官方微信

新浪微博

扫一扫

( 暂不支持下载 )
PC右侧250*250

关于我们

网站简介 媒体报道

联系我们

官方微博 客服QQ:1763425497

我们的产品

多商户分销商城 本地城市生活圈
扫码浏览手机版
© 版权所有 河源市城市互动科技有限公司 [ 小程序部落 ] 微信小程序导航站 排行榜 小程序商店 资讯 专题 评测 排行服务 站务邮箱:8462482@QQ.com 技术支持:城市互动 粤ICP备14002524号