uni-notice-bar.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <template>
  2. <view v-if="show" class="uni-noticebar" :style="{ backgroundColor: backgroundColor }" @click="onClick">
  3. <!-- #ifdef MP-ALIPAY -->
  4. <view v-if="showClose === true || showClose === 'true'" class="uni-noticebar-close" @click="close">
  5. <uni-icons type="closefill" :color="color" size="12" />
  6. </view>
  7. <view v-if="showIcon === true || showIcon === 'true'" class="uni-noticebar-icon">
  8. <uni-icons type="sound" :color="color" size="14" />
  9. </view>
  10. <!-- #endif -->
  11. <!-- #ifndef MP-ALIPAY -->
  12. <uni-icons v-if="showClose === true || showClose === 'true'" class="uni-noticebar-close" type="closefill" :color="color"
  13. size="12" @click="close" />
  14. <uni-icons v-if="showIcon === true || showIcon === 'true'" class="uni-noticebar-icon" type="sound" :color="color"
  15. size="14" />
  16. <!-- #endif -->
  17. <view ref="textBox" class="uni-noticebar__content-wrapper" :class="{'uni-noticebar__content-wrapper--scrollable':scrollable, 'uni-noticebar__content-wrapper--single':!scrollable && (single || moreText)}">
  18. <view :id="elIdBox" class="uni-noticebar__content" :class="{'uni-noticebar__content--scrollable':scrollable, 'uni-noticebar__content--single':!scrollable && (single || moreText)}">
  19. <text :id="elId" ref="animationEle" class="uni-noticebar__content-text" :class="{'uni-noticebar__content-text--scrollable':scrollable,'uni-noticebar__content-text--single':!scrollable && (single || moreText)}"
  20. :style="{color:color, width:wrapWidth+'px', 'animationDuration': animationDuration, '-webkit-animationDuration': animationDuration ,animationPlayState: webviewHide?'paused':animationPlayState,'-webkit-animationPlayState':webviewHide?'paused':animationPlayState, animationDelay: animationDelay, '-webkit-animationDelay':animationDelay}">{{text}}</text>
  21. </view>
  22. </view>
  23. <view v-if="showGetMore === true || showGetMore === 'true'" class="uni-noticebar__more" @click="clickMore">
  24. <text v-if="moreText" :style="{ color: moreColor }" class="uni-noticebar__more-text">{{ moreText }}</text>
  25. <uni-icons type="arrowright" :color="moreColor" size="14" />
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import uniIcons from '../uni-icons/uni-icons.vue'
  31. // #ifdef APP-NVUE
  32. const dom = weex.requireModule('dom');
  33. const animation = weex.requireModule('animation');
  34. // #endif
  35. export default {
  36. name: 'UniNoticeBar',
  37. components: {
  38. uniIcons
  39. },
  40. props: {
  41. text: {
  42. type: String,
  43. default: ''
  44. },
  45. moreText: {
  46. type: String,
  47. default: ''
  48. },
  49. backgroundColor: {
  50. type: String,
  51. default: '#fffbe8'
  52. },
  53. speed: {
  54. // 默认1s滚动100px
  55. type: [String, Number],
  56. default: 100
  57. },
  58. color: {
  59. type: String,
  60. default: '#de8c17'
  61. },
  62. moreColor: {
  63. type: String,
  64. default: '#999999'
  65. },
  66. single: {
  67. // 是否单行
  68. type: [String, Boolean],
  69. default: false
  70. },
  71. scrollable: {
  72. // 是否滚动,添加后控制单行效果取消
  73. type: [String, Boolean],
  74. default: false
  75. },
  76. showIcon: {
  77. // 是否显示左侧icon
  78. type: [String, Boolean],
  79. default: false
  80. },
  81. showGetMore: {
  82. // 是否显示右侧查看更多
  83. type: [String, Boolean],
  84. default: false
  85. },
  86. showClose: {
  87. // 是否显示左侧关闭按钮
  88. type: [String, Boolean],
  89. default: false
  90. }
  91. },
  92. data() {
  93. const elId = `Uni_${Math.ceil(Math.random() * 10e5).toString(36)}`
  94. const elIdBox = `Uni_${Math.ceil(Math.random() * 10e5).toString(36)}`
  95. return {
  96. textWidth: 0,
  97. boxWidth: 0,
  98. wrapWidth: '',
  99. webviewHide: false,
  100. // #ifdef APP-NVUE
  101. stopAnimation: false,
  102. // #endif
  103. elId: elId,
  104. elIdBox: elIdBox,
  105. show: true,
  106. animationDuration: 'none',
  107. animationPlayState: 'paused',
  108. animationDelay: '0s'
  109. }
  110. },
  111. mounted() {
  112. // #ifdef APP-PLUS
  113. var pages = getCurrentPages();
  114. var page = pages[pages.length - 1];
  115. var currentWebview = page.$getAppWebview();
  116. currentWebview.addEventListener('hide',()=>{
  117. this.webviewHide = true
  118. })
  119. currentWebview.addEventListener('show',()=>{
  120. this.webviewHide = false
  121. })
  122. // #endif
  123. this.$nextTick(() => {
  124. this.initSize()
  125. })
  126. },
  127. // #ifdef APP-NVUE
  128. beforeDestroy() {
  129. this.stopAnimation = true
  130. },
  131. // #endif
  132. methods: {
  133. initSize() {
  134. if (this.scrollable) {
  135. // #ifndef APP-NVUE
  136. let query = [],
  137. boxWidth = 0,
  138. textWidth = 0;
  139. let textQuery = new Promise((resolve, reject) => {
  140. uni.createSelectorQuery()
  141. .in(this)
  142. .select(`#${this.elId}`)
  143. .boundingClientRect()
  144. .exec(ret => {
  145. this.textWidth = ret[0].width
  146. resolve()
  147. })
  148. })
  149. let boxQuery = new Promise((resolve, reject) => {
  150. uni.createSelectorQuery()
  151. .in(this)
  152. .select(`#${this.elIdBox}`)
  153. .boundingClientRect()
  154. .exec(ret => {
  155. this.boxWidth = ret[0].width
  156. resolve()
  157. })
  158. })
  159. query.push(textQuery)
  160. query.push(boxQuery)
  161. Promise.all(query).then(() => {
  162. this.animationDuration = `${this.textWidth / this.speed}s`
  163. this.animationDelay = `-${this.boxWidth / this.speed}s`
  164. setTimeout(() => {
  165. this.animationPlayState = 'running'
  166. }, 1000)
  167. })
  168. // #endif
  169. // #ifdef APP-NVUE
  170. dom.getComponentRect(this.$refs['animationEle'], (res) => {
  171. let winWidth = uni.getSystemInfoSync().windowWidth
  172. this.textWidth = res.size.width
  173. animation.transition(this.$refs['animationEle'], {
  174. styles: {
  175. transform: `translateX(-${winWidth}px)`
  176. },
  177. duration: 0,
  178. timingFunction: 'linear',
  179. delay: 0
  180. }, () => {
  181. if (!this.stopAnimation) {
  182. animation.transition(this.$refs['animationEle'], {
  183. styles: {
  184. transform: `translateX(-${this.textWidth}px)`
  185. },
  186. timingFunction: 'linear',
  187. duration: (this.textWidth - winWidth) / this.speed * 1000,
  188. delay: 1000
  189. }, () => {
  190. if (!this.stopAnimation) {
  191. this.loopAnimation()
  192. }
  193. });
  194. }
  195. });
  196. })
  197. // #endif
  198. }
  199. // #ifdef APP-NVUE
  200. if (!this.scrollable && (this.single || this.moreText)) {
  201. dom.getComponentRect(this.$refs['textBox'], (res) => {
  202. this.wrapWidth = res.size.width
  203. })
  204. }
  205. // #endif
  206. },
  207. loopAnimation() {
  208. // #ifdef APP-NVUE
  209. animation.transition(this.$refs['animationEle'], {
  210. styles: {
  211. transform: `translateX(0px)`
  212. },
  213. duration: 0
  214. }, () => {
  215. if (!this.stopAnimation) {
  216. animation.transition(this.$refs['animationEle'], {
  217. styles: {
  218. transform: `translateX(-${this.textWidth}px)`
  219. },
  220. duration: this.textWidth / this.speed * 1000,
  221. timingFunction: 'linear',
  222. delay: 0
  223. }, () => {
  224. if (!this.stopAnimation) {
  225. this.loopAnimation()
  226. }
  227. });
  228. }
  229. });
  230. // #endif
  231. },
  232. clickMore() {
  233. this.$emit('getmore')
  234. },
  235. close() {
  236. this.show = false;
  237. this.$emit('close')
  238. },
  239. onClick() {
  240. this.$emit('click')
  241. }
  242. }
  243. }
  244. </script>
  245. <style lang="scss" scoped>
  246. @import '@/uni.scss';
  247. .uni-noticebar {
  248. /* #ifndef APP-NVUE */
  249. display: flex;
  250. width: 100%;
  251. box-sizing: border-box;
  252. /* #endif */
  253. flex-direction: row;
  254. align-items: center;
  255. padding: 6px 12px;
  256. margin-bottom: 10px;
  257. }
  258. .uni-noticebar-close {
  259. margin-right: 5px;
  260. }
  261. .uni-noticebar-icon {
  262. margin-right: 5px;
  263. }
  264. .uni-noticebar__content-wrapper {
  265. flex: 1;
  266. flex-direction: column;
  267. overflow: hidden;
  268. }
  269. .uni-noticebar__content-wrapper--single {
  270. /* #ifndef APP-NVUE */
  271. line-height: 18px;
  272. /* #endif */
  273. }
  274. .uni-noticebar__content-wrapper--single,
  275. .uni-noticebar__content-wrapper--scrollable {
  276. flex-direction: row;
  277. }
  278. .uni-noticebar__content--scrollable {
  279. /* #ifdef APP-NVUE */
  280. flex: 0;
  281. /* #endif */
  282. /* #ifndef APP-NVUE */
  283. flex: 1;
  284. display: block;
  285. overflow: hidden;
  286. /* #endif */
  287. }
  288. .uni-noticebar__content--single {
  289. /* #ifndef APP-NVUE */
  290. display: flex;
  291. flex: none;
  292. width: 100%;
  293. justify-content: center;
  294. /* #endif */
  295. }
  296. .uni-noticebar__content-text {
  297. font-size: 14px;
  298. line-height: 18px;
  299. /* #ifndef APP-NVUE */
  300. word-break: break-all;
  301. /* #endif */
  302. }
  303. .uni-noticebar__content-text--single {
  304. /* #ifdef APP-NVUE */
  305. lines: 1;
  306. /* #endif */
  307. /* #ifndef APP-NVUE */
  308. display: inline-block;
  309. width: 100%;
  310. white-space: nowrap;
  311. /* #endif */
  312. overflow: hidden;
  313. text-overflow: ellipsis;
  314. }
  315. .uni-noticebar__content-text--scrollable {
  316. /* #ifdef APP-NVUE */
  317. lines: 1;
  318. padding-left: 750rpx;
  319. /* #endif */
  320. /* #ifndef APP-NVUE */
  321. display: inline-block;
  322. white-space: nowrap;
  323. padding-left: 100%;
  324. animation: notice 10s 0s linear infinite both;
  325. animation-play-state: paused;
  326. /* #endif */
  327. }
  328. .uni-noticebar__more {
  329. /* #ifndef APP-NVUE */
  330. display: inline-flex;
  331. /* #endif */
  332. flex-direction: row;
  333. flex-wrap: nowrap;
  334. align-items: center;
  335. padding-left: 5px;
  336. }
  337. .uni-noticebar__more-text {
  338. font-size: 14px;
  339. }
  340. @keyframes notice {
  341. 100% {
  342. transform: translate3d(-100%, 0, 0);
  343. }
  344. }
  345. </style>