轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

微信小程序小技巧《十七》富文本组件wxParse,制作回到顶部按钮 ... ... ... ...

发布者: 西山居士 | 发布时间: 2018-2-25 14:57| 查看数: 6650| 评论数: 1|帖子模式

一:制作回到顶部按钮
作者:马优晨,来自原文地址

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

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


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


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

WXML代码:

  1. <scroll-view class="bigWrap" scroll-y="true" scroll-top="{{scrollTop}}" bindscroll="scroll" bindscrolltolower= "scrolltolower" style="position: absolute; left: 0; top:0; bottom: 0; right: 0;">
  2. //*********************
  3. <view class="com-widget-goTop" bindtap="goTop" wx:if="{{floorstatus}}">
  4. <view class="icon-gotop">
  5. 顶部
  6. </view>
  7. </view>
  8. //*********************
  9. </view>

JS代码:

  1. //回到顶部按钮
  2. Page({
  3. data: {
  4. scrollTop: 0
  5. },
  6. goTop: function(e){
  7. this.setData({
  8. scrollTop:0
  9. })
  10. },
  11. scroll:function(e,res){
  12. // 容器滚动时将此时的滚动距离赋值给 this.data.scrollTop
  13. if(e.detail.scrollTop > 500){
  14. this.setData({
  15. floorstatus: true
  16. });
  17. }else {
  18. this.setData({
  19. floorstatus: false
  20. });
  21. }
  22. })

WXSS代码:

  1. .bigWrap{
  2. background:#eee;
  3. }
  4. /goTop回到顶部图标start/
  5. .com-widget-goTop {
  6. position: fixed;
  7. bottom: 125px;
  8. right: 5px;
  9. background: rgba(0,0,0,0.48);
  10. border-radius: 50%;
  11. overflow: hidden;
  12. z-index: 500;
  13. }
  14. .com-widget-goTop .icon-gotop{
  15. background-color: rgba(0,0,0,0.8);
  16. display: inline-block;
  17. width: 50px;
  18. height: 50px;
  19. line-height: 68px;
  20. font-size: 12px;
  21. color: #ffffff;
  22. text-align: center;
  23. border-radius: 50%;
  24. background: url(http://m.dev.vd.cn/static/xcx/v1/goo/w_2-3451cc437e.png) no-repeat center -1110px;
  25. -webkit-background-size: 50px auto;
  26. }
二:富文本组件wxParse

项目地址:

项目下载:

wxParse-master.zip

1 打开项目地址,下载项目文件

2 将wxParse文件夹粘贴到项目

3 新建页面 “pages/home/rich_content/rich_content”

4 rich_content.js

  1. var WxParse = require('../../wxParse/wxParse.js');
  2. // pages/home/rich_content/rich_content.js
  3. Page({
  4. data: {},
  5. onLoad: function (options) {
  6. // 页面初始化 options为页面跳转所带来的参数
  7. var article = '<div style="color:red">我是<br>HTML代码</div>';
  8. /**
  9. * WxParse.wxParse(bindName , type, data, target,imagePadding)
  10. * 1.bindName绑定的数据名(必填)
  11. * 2.type可以为html或者md(必填)
  12. * 3.data为传入的具体数据(必填)
  13. * 4.targetPage对象,一般为this(必填)
  14. * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
  15. */
  16. var that = this;
  17. WxParse.wxParse('article', 'html', article, that, 5);
  18. }
  19. })

5 rich_content.wxml

  1. <import src="../../wxParse/wxParse.wxml"/>
  2. <!--这里dataarticlebindName-->
  3. <template is="wxParse" data="{{wxParseData:article.nodes}}"/>

6 rich_content.wxss

  1. @import "../../wxParse/wxParse.wxss";

效果图: 


最新评论

17521585492 发表于 2022-5-25 10:08
Python程序设计电子版

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表