yxp-txt-scroll.nvue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <view class="container-gg" :style="{minHeight: fontSize + 'rpx'}">
  3. <view class="notice-text-box" ref="yxpTxtBox" id="yxpTxtBox">
  4. <view class="notice-text-con" ref="yxpTxt" id="yxpTxt"
  5. :style="{animationDuration: (durationB || duration) / 1000 + 's',animationDelay: delay / 1000 + 's', paddingLeft: boxWidth + 'px'}">
  6. <text class="notice-text" v-for="(item,index) in text" :key="index" :style="{fontSize:fontSize+'rpx', color: fontColor,
  7. paddingRight: (index === text.length - 1) ? '0' : txtPadding + 'rpx'}"
  8. @click="Select(item)">{{item.noticeTitle}}</text>
  9. </view>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. // #ifdef APP-NVUE
  15. const Binding = uni.requireNativePlugin('bindingx');
  16. const dom = weex.requireModule('dom');
  17. // #endif
  18. export default {
  19. props: {
  20. text: {
  21. type: Array,
  22. default: []
  23. },
  24. fontSize: {
  25. type: String,
  26. default: '24'
  27. },
  28. fontColor: {
  29. type: String,
  30. default: '#fff'
  31. },
  32. duration: {
  33. type: String,
  34. default: '0'
  35. },
  36. delay: {
  37. type: String,
  38. default: '0'
  39. },
  40. txtPadding: {
  41. type: String,
  42. default: '30'
  43. },
  44. speed: {
  45. type: String,
  46. default: '200'
  47. }
  48. },
  49. computed: {},
  50. data() {
  51. return {
  52. x: 0,
  53. gesToken: 0,
  54. textWidth: 0,
  55. boxWidth: 375,
  56. durationB: 0
  57. }
  58. },
  59. mounted() {
  60. this.initScrollWidth()
  61. },
  62. methods: {
  63. Select(item) {
  64. this.$emit("onClick", item)
  65. // this.$parent.open=false;
  66. },
  67. initScrollWidth: function() {
  68. let self = this;
  69. // #ifdef APP-NVUE
  70. setTimeout(() => {
  71. dom.getComponentRect(this.$refs.yxpTxtBox, res => {
  72. this.boxWidth = res.size.width;
  73. dom.getComponentRect(this.$refs.yxpTxt, res => {
  74. this.textWidth = res.size.width - 375 + this.boxWidth;
  75. if (!this.duration || this.duration === 0 || this.duration ===
  76. '0') {
  77. this.durationB = parseInt(this.textWidth / parseFloat(this
  78. .speed) * 1000)
  79. }
  80. this.bindTiming()
  81. });
  82. });
  83. }, 500);
  84. // #endif
  85. // #ifndef APP-NVUE
  86. let viewBox = uni.createSelectorQuery().in(this).select('#yxpTxtBox');
  87. viewBox.fields({
  88. size: true,
  89. rect: true
  90. }, data => {
  91. self.boxWidth = data.width
  92. let view = uni.createSelectorQuery().in(self).select('#yxpTxt');
  93. view.fields({
  94. size: true,
  95. rect: true
  96. }, data => {
  97. self.textWidth = data.width
  98. if (!self.duration || self.duration === 0 || self.duration === '0') {
  99. self.durationB = parseInt(self.textWidth / parseFloat(self.speed) * 1000)
  100. }
  101. }).exec();
  102. }).exec();
  103. // #endif
  104. },
  105. getEl: function(el) {
  106. if (typeof el === 'string' || typeof el === 'number') return el;
  107. // #ifdef APP-PLUS
  108. if (WXEnvironment) {
  109. return el.ref;
  110. } else {
  111. return el instanceof HTMLElement ? el : el.$el;
  112. }
  113. // #endif
  114. // #ifdef H5
  115. return el instanceof HTMLElement ? el : el.$el;
  116. // #endif
  117. },
  118. bindTiming: function() {
  119. this.isInAnimation = true;
  120. var my = this.getEl(this.$refs.yxpTxt);
  121. var self = this;
  122. var textWidth = -this.textWidth;
  123. var delay = parseInt(this.delay);
  124. var duration = this.durationB || this.duration;
  125. var translate_x_origin = 'linear(t,0,' + textWidth + ',' + duration + ')';
  126. setTimeout(() => {
  127. var result = Binding.bind({
  128. eventType: 'timing',
  129. exitExpression: 't>' + duration,
  130. props: [{
  131. element: my,
  132. property: 'transform.translateX',
  133. expression: translate_x_origin
  134. }]
  135. }, function(e) {
  136. if (e.state === 'end' || e.state === 'exit') {
  137. Binding.unbind({
  138. eventType: 'timing',
  139. token: result.gesToken
  140. });
  141. self.bindTiming()
  142. }
  143. });
  144. self.gesToken = result.token;
  145. }, delay)
  146. }
  147. },
  148. }
  149. </script>
  150. <style>
  151. .container-gg {
  152. flex: 1;
  153. display: flex;
  154. flex-direction: column;
  155. overflow: hidden;
  156. /* #ifndef APP-PLUS */
  157. height: 100%;
  158. width: 100%;
  159. /* #endif */
  160. }
  161. .notice-text-box {
  162. display: flex;
  163. flex-direction: column;
  164. position: relative;
  165. /* #ifndef APP-PLUS */
  166. height: 100%;
  167. width: 100%;
  168. /* #endif */
  169. flex: 1;
  170. align-items: center;
  171. justify-content: center;
  172. }
  173. .notice-text-con {
  174. flex: 1;
  175. display: flex;
  176. flex-direction: row;
  177. align-items: center;
  178. flex-wrap: nowrap;
  179. /* #ifndef APP-PLUS */
  180. white-space: pre;
  181. -webkit-animation: scroll-left 3s infinite linear;
  182. animation: scroll-left 3s infinite linear;
  183. left: 0;
  184. padding-left: 100%;
  185. /* #endif */
  186. position: absolute;
  187. /* #ifdef APP-NVUE */
  188. left: 0;
  189. /* #endif */
  190. }
  191. /* #ifndef APP-NVUE */
  192. @-webkit-keyframes scroll-left {
  193. 0% {
  194. -webkit-transform: translateX(0);
  195. transform: translateX(0);
  196. }
  197. 100% {
  198. -webkit-transform: translateX(-100%);
  199. transform: translateX(-100%);
  200. }
  201. }
  202. @keyframes scroll-left {
  203. 0% {
  204. -webkit-transform: translateX(0);
  205. transform: translateX(0);
  206. }
  207. 100% {
  208. -webkit-transform: translateX(-100%);
  209. transform: translateX(-100%);
  210. }
  211. }
  212. /* #endif */
  213. </style>