当前位置:   article > 正文

微信小程序之公告自右往左滑动_小程序 从右往左公告

小程序 从右往左公告

在微信小程序中,除了轮播图和广告之外,公告的使用频率也很多,一般都是从右往左移动,今天我就此做了以下的代码记录

这是样式,由于不知道怎么弄动态图片,只能这样展示:

.wxml

  1. <!-- 公告 -->
  2. <view class="inform">
  3. <view class="inform-img">
  4. <image src="/images/horn.png" class="inform-image"></image>
  5. </view>
  6. <view class='marquee' style='width:{{marqueeWidth}}rpx;'>
  7. <view class='marqueeTitle' style="width:{{length}}rpx;transform:translateX( {{marqueeDistance}}rpx);font-size: {{size}}rpx;">{{text}}</view>
  8. </view>
  9. </view>

.wxss

  1. .inform{
  2. width:96%;
  3. height:70rpx;
  4. background-color: #F6F6F6;
  5. margin:10rpx 0;
  6. margin-left: 2%;
  7. border-radius: 10rpx;
  8. display: flex;
  9. float: left;
  10. }
  11. .inform-img{
  12. width:10%;
  13. margin-left: 5%;
  14. text-align: center;
  15. }
  16. .inform-image{
  17. width:60rpx;
  18. height:60rpx;
  19. margin-top:5rpx;
  20. }
  21. .marquee{
  22. width:80%;
  23. height: 70rpx;
  24. overflow: hidden;
  25. line-height: 70rpx;
  26. }
  27. .marqueeTitle{
  28. height: 70rpx;
  29. color:#ff0000;
  30. }

.js

  1. // pages/home/home.js
  2. var app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. //公告滚动
  9. text: "通知:即日起在小程序商城消费满一百元可享受免费包邮服务!",
  10. marqueePace: 1, //滚动速度
  11. marqueeDistance: 0, //初始滚动距离
  12. size: 28,
  13. marqueeWidth: 660,
  14. marqueeMargin: 40,
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: function(options) {
  20. },
  21. /**
  22. * 生命周期函数--监听页面初次渲染完成
  23. */
  24. onReady: function() {
  25. },
  26. /**
  27. * 生命周期函数--监听页面显示
  28. */
  29. onShow: function() {
  30. var that = this;
  31. var length = that.data.text.length * that.data.size; //计算文字的长度
  32. console.log(length)
  33. that.setData({
  34. length: length,
  35. // 当文字长度小于屏幕长度时,需要增加补白
  36. marqueeMargin: length < that.data.marqueeWidth ? (that.data.marqueeWidth - length) / 4 : that.data.marqueeMargin
  37. })
  38. if (that.data.length > that.data.marqueeWidth) {
  39. that.run1();
  40. }
  41. },
  42. // 标题超过限制宽度时做滚动播放
  43. run1: function () {
  44. var that = this;
  45. var mytime = setInterval(function () {
  46. if (-that.data.marqueeDistance < that.data.length) {
  47. that.setData({
  48. marqueeDistance: that.data.marqueeDistance - that.data.marqueePace,
  49. })
  50. } else {
  51. clearInterval(mytime);
  52. that.setData({
  53. marqueeDistance: that.data.marqueeWidth
  54. });
  55. that.run1();
  56. }
  57. }, 5)
  58. },
  59. /**
  60. * 生命周期函数--监听页面隐藏
  61. */
  62. onHide: function() {
  63. },
  64. /**
  65. * 生命周期函数--监听页面卸载
  66. */
  67. onUnload: function() {
  68. },
  69. /**
  70. * 页面相关事件处理函数--监听用户下拉动作
  71. */
  72. onPullDownRefresh: function() {
  73. },
  74. /**
  75. * 页面上拉触底事件的处理函数
  76. */
  77. onReachBottom: function() {
  78. },
  79. /**
  80. * 用户点击右上角分享
  81. */
  82. onShareAppMessage: function() {
  83. }
  84. })

这只是我写的一个简单的公告样式,如果有哪些地方需要优化,还请各位告知。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/839401
推荐阅读
相关标签