prize-news.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <!-- 横向滚动播放 -->
  2. <template>
  3. <view class='text-scroll-wrap'>
  4. <view class="text-content" :style="{ left:leftMove+'px' }">
  5. <view class="text-item" v-for="(item,index) in myList" :key="index">
  6. <view class="text">
  7. <image :src="item.avatar" class="image"></image>
  8. <view class="title">
  9. <view class="name">恭喜</view>
  10. <view class="prize">{{ item.nickName }}</view>
  11. <view class="name">{{ item.type == 1 ? '刮出了' : '兑换了' }}</view>
  12. <view class="prize">{{ item.prizeInfo }}</view>
  13. </view>
  14. </view>
  15. </view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. name: 'prize-news',
  22. components: {},
  23. props: {
  24. list: { // 滚动列表
  25. type: Array,
  26. default: () => {
  27. return [{
  28. nickName: '测试'
  29. },{
  30. nickName: '测试'
  31. }];
  32. }
  33. },
  34. type: { // 类型
  35. type: String,
  36. default: 'text',
  37. validator: (value) => {
  38. return ['text', 'image'].indexOf(value) !== -1;
  39. }
  40. },
  41. textKey: { // 文字key值
  42. type: String,
  43. default: ''
  44. },
  45. imageKey: { // 图片key值
  46. type: String,
  47. default: ''
  48. },
  49. actives: { // 如果需要,此为已选列表
  50. type: Array,
  51. default: () => {
  52. return [];
  53. }
  54. },
  55. duration: { // 间隔时间
  56. type: [Number, String],
  57. default: 20
  58. },
  59. delay: { // 延迟时间
  60. type: [Number, String],
  61. default: 0
  62. },
  63. initPosition: { // 初始位置
  64. type: String,
  65. default: 'left',
  66. validator: (value) => {
  67. return ['left', 'right'].indexOf(value) !== -1;
  68. }
  69. }
  70. },
  71. data() {
  72. return {
  73. myList: [],
  74. leftMove: 0,
  75. firstItemWidth: 0,
  76. wrapWidth: 0,
  77. };
  78. },
  79. // 组件实例化之前
  80. beforeCreate() {},
  81. // 组件创建完成
  82. created() {
  83. this.myList = this.list;
  84. },
  85. // 组件挂载之前
  86. beforeMount() {},
  87. // 组件挂载之后
  88. mounted() {
  89. let query = uni.createSelectorQuery().in(this);
  90. query.select('.text-item').boundingClientRect(data => {
  91. this.firstItemWidth = data.width;
  92. }).exec();
  93. query.select('.text-scroll-wrap').boundingClientRect(data => {
  94. this.wrapWidth = data.width;
  95. this.initPosition === 'left' ? this.leftMove = 0 : this.leftMove = this.wrapWidth;
  96. }).exec();
  97. if (this.delay <= 0) {
  98. this.scrollContent();
  99. } else {
  100. let t = setTimeout(() => {
  101. clearTimeout(t);
  102. this.scrollContent();
  103. }, Number(this.delay));
  104. }
  105. },
  106. // 组件数据更新时
  107. beforeUpdate() {},
  108. // 组价更新
  109. updated() {},
  110. // 组件销毁前
  111. beforeDestroy() {},
  112. // 组件销毁后
  113. destroyed() {},
  114. // 页面方法
  115. methods: {
  116. // 点击此项
  117. clickThis(e) {
  118. // console.log(e.currentTarget.dataset.text);
  119. this.$emit('change', e.currentTarget.dataset.text);
  120. },
  121. // 滚动
  122. scrollContent() {
  123. let num = this.initPosition === 'left' ? 0 : this.wrapWidth;
  124. setInterval(() => {
  125. num--;
  126. if (num < 0 && Math.abs(num) >= this.firstItemWidth) {
  127. this.myList.push(this.myList[0]);
  128. this.myList.splice(0, 1);
  129. num += this.firstItemWidth;
  130. let query = uni.createSelectorQuery().in(this);
  131. query.selectAll('.text-item').boundingClientRect(data => {
  132. this.firstItemWidth = data[1].width;
  133. }).exec();
  134. }
  135. this.leftMove = num;
  136. }, Number(this.duration));
  137. }
  138. }
  139. };
  140. </script>
  141. <style lang='scss'>
  142. .text-scroll-wrap {
  143. overflow: hidden;
  144. width: 100%;
  145. height: 50rpx;
  146. line-height: 50rpx;
  147. margin-bottom: 12rpx;
  148. position: relative;
  149. .text-content {
  150. display: inline-block;
  151. white-space: nowrap;
  152. height: 100%;
  153. position: absolute;
  154. top: 0;
  155. .text-item {
  156. height: 100%;
  157. display: inline-block;
  158. overflow: hidden;
  159. font-size: 28rpx;
  160. color: #666;
  161. .text {
  162. display: flex;
  163. align-items: center;
  164. height: 50rpx;
  165. padding: 0 40rpx 0 4rpx;
  166. margin: 0 60rpx;
  167. background: rgba(255, 255, 255, .5);
  168. border-radius: 25rpx;
  169. font-size: 24rpx;
  170. .image {
  171. display: inline-block;
  172. width: 46rpx;
  173. height: 46rpx;
  174. vertical-align: middle;
  175. border-radius: 50%;
  176. }
  177. .title {
  178. display: flex;
  179. align-items: center;
  180. .name{
  181. margin: 0 16rpx;
  182. }
  183. .prize{
  184. color: #F27120;
  185. }
  186. }
  187. }
  188. &.active .text {
  189. background: #1E90FF;
  190. color: #fff;
  191. }
  192. }
  193. }
  194. }
  195. </style>