微信小程序微信小程序实现跑马灯(多条文字横向衔接滚动)

た 入场券 2022-12-05 13:52 619阅读 0赞

js代码

  1. Page({
  2. data: {
  3. data: [
  4. {
  5. img: "惠便利自提店铺优惠多多惠便利自提店铺优惠多多惠便利自提店铺优惠多多惠便利自提店铺优惠多多惠",
  6. linkurl: "",
  7. linkname: "",
  8. starspos: 0,
  9. back_color: "gold"
  10. }, {
  11. img: "江龙:每日惠自提店铺特价啦~店铺特价啦~店铺特价啦~",
  12. linkurl: "",
  13. linkname: "",
  14. starspos: 0, //间距
  15. back_color: "#000000"
  16. }, {
  17. img: "啦啦啦啦啦啦啦啦啦啦~啦啦啦啦啦~啦啦啦啦啦~",
  18. linkurl: "",
  19. linkname: "",
  20. starspos: 0, //间距
  21. back_colors: "red"
  22. }
  23. , {
  24. img: "哈哈哈哈~",
  25. linkurl: "",
  26. linkname: "",
  27. starspos: 0, //间距
  28. back_colors: "red"
  29. }
  30. ],
  31. broadcast_arr: {
  32. speed: 2.8, //滚动速度,每秒2.8个字
  33. font_size: "16", //字体大小(px)
  34. text_color: "#ffffff", //字体颜色
  35. back_color: "#269e9e", //背景色
  36. }
  37. },
  38. onLoad: function(options) {
  39. let ititdata = this.data.data,
  40. assist = this.data.broadcast_arr,
  41. this_width = 0,
  42. spacing = 0,
  43. speed = (this.data.broadcast_arr.speed * this.data.broadcast_arr.font_size); //m每秒行走的距离
  44. for (let i in ititdata) {
  45. ititdata[i].starspos = wx.getSystemInfoSync().windowWidth; //以取屏幕宽度为间距
  46. this_width += ititdata[i].img.length * this.data.broadcast_arr.font_size;
  47. if (i != ititdata.length - 1) {
  48. spacing += ititdata[i].starspos
  49. }
  50. }
  51. let total_length = this_width + spacing;//总长
  52. assist.time = total_length / speed; /**滚动时间*/
  53. assist.width_mal = total_length;
  54. this.setData({
  55. broadcast_arr: assist,
  56. data: ititdata
  57. })
  58. }

wxml

  1. <view class="marquee_container " style="background:{
  2. {broadcast_arr.back_color}};font-size:32rpx;">
  3. <view class='marquee_text' style='--marqueeWidth--:{
  4. {-broadcast_arr.width_mal}}px;--speed--:{
  5. {broadcast_arr.time}}s;width:{
  6. {broadcast_arr.width_mal}}px;'>
  7. <block wx:for="{
  8. {data}}">
  9. <view style='color:{
  10. {broadcast_arr.text_color}};margin-left:{
  11. {index!=0?item.starspos*2:0}}rpx;' class="">
  12. {
  13. {
  14. item.img}}
  15. </view>
  16. </block>
  17. </view>
  18. </view>

wxss

  1. @keyframes around {
  2. from {
  3. margin-left: 100%;
  4. }
  5. to {
  6. margin-left: var(--marqueeWidth--);
  7. }
  8. }
  9. .marquee_container {
  10. /* background-color: #0099FF; */
  11. padding: 12rpx 0;
  12. position: relative;
  13. width: 100%;
  14. height: 50rpx;
  15. }
  16. .marquee_text {
  17. display: flex;
  18. white-space: nowrap;
  19. animation-name: around;
  20. animation-duration: var(--speed--);
  21. animation-iteration-count: infinite;
  22. animation-timing-function: linear;
  23. line-height: 50rpx;
  24. }
  25. .marquee_tit {
  26. height: 50rpx;
  27. line-height: 50rpx;
  28. position: absolute;
  29. padding-left: 22rpx;
  30. }

效果
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,619人围观)

还没有评论,来说两句吧...

相关阅读