wxml:

  1. <view>1 显示完后再显示</view>
  2. <view class="example">
  3. <view class="marquee_box">
  4. <view class="marquee_text" style="{{orientation}}:{{marqueeDistance}}px;font-size: {{size}}px;">
  5. {{text}}
  6. </view>
  7. </view>
  8. </view>
  9. <view>2 出现白边后即显示</view>
  10. <view class="example">
  11. <view class="marquee_box">
  12. <view class="marquee_text" style="{{orientation}}:{{marqueeDistance2}}px;font-size: {{size}}px;">
  13. <text>{{text}}</text>
  14. <text wx:if="{{marquee2copy_status}}" style="margin-left:{{marquee2_margin}}px;">{{text}}</text>
  15. </view>
  16. </view>
  17. </view>

  

wxss:

  1. .example {
  2. display: block;
  3. width: 100%;
  4. height: 100rpx;
  5. }
  6.  
  7. .marquee_box {
  8. width: 100%;
  9. position: relative;
  10. }
  11.  
  12. .marquee_text {
  13. white-space: nowrap;
  14. position: absolute;
  15. top: 0;
  16. }

  js:

  1. Page({
  2. data: {
  3. text: '这是一条会滚动的文字滚来滚去的文字跑马灯,哈哈哈哈哈哈哈哈',
  4. marqueePace: 1,//滚动速度
  5. marqueeDistance: 0,//初始滚动距离
  6. marqueeDistance2: 0,
  7. marquee2copy_status: false,
  8. marquee2_margin: 60,
  9. size: 14,
  10. orientation: 'left',//滚动方向
  11. interval: 20 // 时间间隔
  12. },
  13. onShow: function () {
  14. // 页面显示
  15. var vm = this;
  16. var length = vm.data.text.length * vm.data.size;//文字长度
  17. var windowWidth = wx.getSystemInfoSync().windowWidth;// 屏幕宽度
  18. vm.setData({
  19. length: length,
  20. windowWidth: windowWidth,
  21. marquee2_margin: length < windowWidth ? windowWidth - length : vm.data.marquee2_margin//当文字长度小于屏幕长度时,需要增加补白
  22. });
  23. vm.run1();// 水平一行字滚动完了再按照原来的方向滚动
  24. vm.run2();// 第一个字消失后立即从右边出现
  25. },
  26. run1: function () {
  27. var vm = this;
  28. var interval = setInterval(function () {
  29. if (-vm.data.marqueeDistance < vm.data.length) {
  30. vm.setData({
  31. marqueeDistance: vm.data.marqueeDistance - vm.data.marqueePace,
  32. });
  33. } else {
  34. clearInterval(interval);
  35. vm.setData({
  36. marqueeDistance: vm.data.windowWidth
  37. });
  38. vm.run1();
  39. }
  40. }, vm.data.interval);
  41. },
  42. run2: function () {
  43. var vm = this;
  44. var interval = setInterval(function () {
  45. if (-vm.data.marqueeDistance2 < vm.data.length) {
  46. // 如果文字滚动到出现marquee2_margin=30px的白边,就接着显示
  47. vm.setData({
  48. marqueeDistance2: vm.data.marqueeDistance2 - vm.data.marqueePace,
  49. marquee2copy_status: vm.data.length + vm.data.marqueeDistance2 <= vm.data.windowWidth + vm.data.marquee2_margin,
  50. });
  51. } else {
  52. if (-vm.data.marqueeDistance2 >= vm.data.marquee2_margin) { // 当第二条文字滚动到最左边时
  53. vm.setData({
  54. marqueeDistance2: vm.data.marquee2_margin // 直接重新滚动
  55. });
  56. clearInterval(interval);
  57. vm.run2();
  58. } else {
  59. clearInterval(interval);
  60. vm.setData({
  61. marqueeDistance2: -vm.data.windowWidth
  62. });
  63. vm.run2();
  64. }
  65. }
  66. }, vm.data.interval);
  67. }
  68. })

  

微信小程序实现文字跑马灯的更多相关文章

  1. 微信小程序-实现文字跑马灯-wepy

    百度蛮多例子的,但是代码太长懒得看了 前言 要实现跑马灯主要就是获得判断开始定界和结束定界, 1.9.3新增的wxml操作接口 就可以拿到节点长宽等属性,当然你也可以直接用 文字数量 * 文字大小(注 ...

  2. 微信小程序里实现跑马灯效果

    在微信小程序 里实现跑马灯效果,类似滚动字幕或者滚动广告之类的,使用简单的CSS样式控制,没用到JS wxml: <!-- 复制的跑马灯效果 --> <view class=&quo ...

  3. 微信小程序Tabbar文字在真机不显示

    按照官方文档在json中定义好了Tabbar后,在模拟器上显示没问题,而在真机上不显示Tabar文字. 让我很苦笑不得的原因是: 在app.json定义Tabbar文字选中态和非选中态颜色时我用了英文 ...

  4. 微信小程序 canvas 文字居中

    drawCanvas: function(ctx) { //... // 昵称 ctx.setFontSize(16) //字体大小 ctx.setFillStyle('#fff') //字体颜色 c ...

  5. 微信小程序 - 超出文字省略组件

    使用说明 sty:定义样式 text:文字 clamp: 0:代表不限制 1:超过1行省略号(默认) n:超过n行省略     点击下载:ellipsis    

  6. 微信小程序 canvas 文字自动换行

    Page({ drawCanvas: function(ctx) {// 地址 ctx.setFontSize() ctx.setFillStyle('#9E7240') ctx.textAlign= ...

  7. 微信小程序 的文字复制功能如何实现?

    text设置属性  selectable="true" 就可以长按复制了 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing 欢迎关注,有 ...

  8. 微信小程序跑马灯效果--基于CSS3 animation 及 基于JS

    如果本文对你有用,请爱心点个赞,提高排名,帮助更多的人.谢谢大家!❤ 如果解决不了,可以在文末进群交流. 基于CSS3主要代码实现 效果图: 视图模板wxml中: <view class=&qu ...

  9. 微信小程序tips集合:无法输入文字/随时查看页面/元素审查/点击事件/数据绑定

    1:编辑文档无法输入文字 出现这种情况一般是因为之前编辑的文档未保存,所有在其他文档输入的时候会自动输入到未保存的文档中,在文档暂时编辑完毕后要ctrl+s随手保存,不然会出现无法打字情况 2: 随时 ...

随机推荐

  1. C语言 · 上帝造题五分钟

    算法提高 上帝造题五分钟   时间限制:1.0s   内存限制:256.0MB      问题描述 第一分钟,上帝说:要有题.于是就有了L,Y,M,C 第二分钟,LYC说:要有向量.于是就有了长度为n ...

  2. 【Navicat_Premium_11.0.10】破解版

    数据库管理的超级工具 Navicat_Premium_11.0.10破解版: Navicat_Premium_11.0.10 ,功能全开,支持多种数据库,爽~ 下载地址请拖到本文最后: 在没和谐前永久 ...

  3. Mac下面的SecureCRT(附破解方案) 更新到最新的7.2的破解方案

    继续更新到7.2的破解.只是升级了下secureCRT到7.2,方法还是不变 相信很多人升级到了7.2的SecureCRT之后原来的破解方案失效了,一直也有人问新的破解方案,发现了,不敢独享放上crt ...

  4. Hibernate-使用事务

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...

  5. js学习笔记19----getElementsByClassName函数封装

    js里面的getElementsByClassName()方法可通过某个class名获取到元素,在标准浏览器下可使用,在非标准浏览器下不可用.为了能够让这个方法兼容所有的浏览器,可以封装成如下函数: ...

  6. Linux 精确获取指定目录对应的块的剩余空间

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/statfs ...

  7. 关于win7下安卓开发环境的搭建

    一.下载安装JDK(不用配置环境变量) 1.先卸载原来存在的JDK 控制面板-卸载程序-有两个软件(Java SE Development Kit 8 Update 101(64-bit)和Java ...

  8. appium的inspectot使用

    前提已安装好appium环境 1. 打开appium-doctor 2. 启动appium-service,点击  inspector 3. 配置手机参数,参数获取参考上篇博客 4. 点击start ...

  9. e621. Activating a Keystroke When Any Child Component Has Focus

    Normally, a keystroke registered on a component is activated when the component has the focus. This ...

  10. php -- 检查是否存在

    1.检查变量是否存在:isset() 2.检查常量是否存在:defined() 3.检查方法是否存在:function_exists() 4.检查类是否存在:class_exists()