prize-news.vue 4.1 KB

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