uni-notice-bar.vue 10 KB

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