uni-notice-bar.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. watch:{
  151. text:function(newValue,oldValue){
  152. this.initSize();
  153. }
  154. },
  155. computed: {
  156. isShowGetMore() {
  157. return this.showGetMore === true || this.showGetMore === 'true'
  158. },
  159. isShowClose() {
  160. return (this.showClose === true || this.showClose === 'true')
  161. && (this.showGetMore === false || this.showGetMore === 'false')
  162. }
  163. },
  164. mounted() {
  165. // #ifdef APP-PLUS
  166. var pages = getCurrentPages();
  167. var page = pages[pages.length - 1];
  168. var currentWebview = page.$getAppWebview();
  169. currentWebview.addEventListener('hide', () => {
  170. this.webviewHide = true
  171. })
  172. currentWebview.addEventListener('show', () => {
  173. this.webviewHide = false
  174. })
  175. // #endif
  176. this.$nextTick(() => {
  177. this.initSize()
  178. })
  179. },
  180. // #ifdef APP-NVUE
  181. beforeDestroy() {
  182. this.stopAnimation = true
  183. },
  184. // #endif
  185. methods: {
  186. initSize() {
  187. if (this.scrollable) {
  188. // #ifndef APP-NVUE
  189. let query = [],
  190. boxWidth = 0,
  191. textWidth = 0;
  192. let textQuery = new Promise((resolve, reject) => {
  193. uni.createSelectorQuery()
  194. // #ifndef MP-ALIPAY
  195. .in(this)
  196. // #endif
  197. .select(`#${this.elId}`)
  198. .boundingClientRect()
  199. .exec(ret => {
  200. this.textWidth = ret[0].width
  201. resolve()
  202. })
  203. })
  204. let boxQuery = new Promise((resolve, reject) => {
  205. uni.createSelectorQuery()
  206. // #ifndef MP-ALIPAY
  207. .in(this)
  208. // #endif
  209. .select(`#${this.elIdBox}`)
  210. .boundingClientRect()
  211. .exec(ret => {
  212. this.boxWidth = ret[0].width
  213. resolve()
  214. })
  215. })
  216. query.push(textQuery)
  217. query.push(boxQuery)
  218. Promise.all(query).then(() => {
  219. this.animationDuration = `${this.textWidth / this.speed}s`
  220. this.animationDelay = `-${this.boxWidth / this.speed}s`
  221. setTimeout(() => {
  222. this.animationPlayState = 'running'
  223. }, 1000)
  224. })
  225. // #endif
  226. // #ifdef APP-NVUE
  227. dom.getComponentRect(this.$refs['animationEle'], (res) => {
  228. let winWidth = uni.getSystemInfoSync().windowWidth
  229. this.textWidth = res.size.width
  230. animation.transition(this.$refs['animationEle'], {
  231. styles: {
  232. transform: `translateX(-${winWidth}px)`
  233. },
  234. duration: 0,
  235. timingFunction: 'linear',
  236. delay: 0
  237. }, () => {
  238. if (!this.stopAnimation) {
  239. animation.transition(this.$refs['animationEle'], {
  240. styles: {
  241. transform: `translateX(-${this.textWidth}px)`
  242. },
  243. timingFunction: 'linear',
  244. duration: (this.textWidth - winWidth) / this.speed * 1000,
  245. delay: 1000
  246. }, () => {
  247. if (!this.stopAnimation) {
  248. this.loopAnimation()
  249. }
  250. });
  251. }
  252. });
  253. })
  254. // #endif
  255. }
  256. // #ifdef APP-NVUE
  257. if (!this.scrollable && (this.single || this.moreText)) {
  258. dom.getComponentRect(this.$refs['textBox'], (res) => {
  259. this.wrapWidth = res.size.width
  260. })
  261. }
  262. // #endif
  263. },
  264. loopAnimation() {
  265. // #ifdef APP-NVUE
  266. animation.transition(this.$refs['animationEle'], {
  267. styles: {
  268. transform: `translateX(0px)`
  269. },
  270. duration: 0
  271. }, () => {
  272. if (!this.stopAnimation) {
  273. animation.transition(this.$refs['animationEle'], {
  274. styles: {
  275. transform: `translateX(-${this.textWidth}px)`
  276. },
  277. duration: this.textWidth / this.speed * 1000,
  278. timingFunction: 'linear',
  279. delay: 0
  280. }, () => {
  281. if (!this.stopAnimation) {
  282. this.loopAnimation()
  283. }
  284. });
  285. }
  286. });
  287. // #endif
  288. },
  289. clickMore() {
  290. this.$emit('getmore')
  291. },
  292. close() {
  293. this.show = false;
  294. this.$emit('close')
  295. },
  296. onClick() {
  297. this.$emit('click')
  298. }
  299. }
  300. }
  301. </script>
  302. <style lang="scss" scoped>
  303. .uni-noticebar {
  304. /* #ifndef APP-NVUE */
  305. display: flex;
  306. width: 100%;
  307. box-sizing: border-box;
  308. /* #endif */
  309. flex-direction: row;
  310. align-items: center;
  311. padding: 10px 12px;
  312. margin-bottom: 10px;
  313. }
  314. .uni-cursor-point {
  315. /* #ifdef H5 */
  316. cursor: pointer;
  317. /* #endif */
  318. }
  319. .uni-noticebar-close {
  320. margin-left: 8px;
  321. margin-right: 5px;
  322. }
  323. .uni-noticebar-icon {
  324. margin-right: 5px;
  325. }
  326. .uni-noticebar__content-wrapper {
  327. flex: 1;
  328. flex-direction: column;
  329. overflow: hidden;
  330. }
  331. .uni-noticebar__content-wrapper--single {
  332. /* #ifndef APP-NVUE */
  333. line-height: 18px;
  334. /* #endif */
  335. }
  336. .uni-noticebar__content-wrapper--single,
  337. .uni-noticebar__content-wrapper--scrollable {
  338. flex-direction: row;
  339. }
  340. /* #ifndef APP-NVUE */
  341. .uni-noticebar__content-wrapper--scrollable {
  342. position: relative;
  343. }
  344. /* #endif */
  345. .uni-noticebar__content--scrollable {
  346. /* #ifdef APP-NVUE */
  347. flex: 0;
  348. /* #endif */
  349. /* #ifndef APP-NVUE */
  350. flex: 1;
  351. display: block;
  352. overflow: hidden;
  353. /* #endif */
  354. }
  355. .uni-noticebar__content--single {
  356. /* #ifndef APP-NVUE */
  357. display: flex;
  358. flex: none;
  359. width: 100%;
  360. justify-content: center;
  361. /* #endif */
  362. }
  363. .uni-noticebar__content-text {
  364. font-size: 14px;
  365. line-height: 18px;
  366. /* #ifndef APP-NVUE */
  367. word-break: break-all;
  368. /* #endif */
  369. }
  370. .uni-noticebar__content-text--single {
  371. /* #ifdef APP-NVUE */
  372. lines: 1;
  373. /* #endif */
  374. /* #ifndef APP-NVUE */
  375. display: block;
  376. width: 100%;
  377. white-space: nowrap;
  378. /* #endif */
  379. overflow: hidden;
  380. text-overflow: ellipsis;
  381. }
  382. .uni-noticebar__content-text--scrollable {
  383. /* #ifdef APP-NVUE */
  384. lines: 1;
  385. padding-left: 750rpx;
  386. /* #endif */
  387. /* #ifndef APP-NVUE */
  388. position: absolute;
  389. display: block;
  390. height: 18px;
  391. line-height: 18px;
  392. white-space: nowrap;
  393. padding-left: 100%;
  394. animation: notice 10s 0s linear infinite both;
  395. animation-play-state: paused;
  396. /* #endif */
  397. }
  398. .uni-noticebar__more {
  399. /* #ifndef APP-NVUE */
  400. display: inline-flex;
  401. /* #endif */
  402. flex-direction: row;
  403. flex-wrap: nowrap;
  404. align-items: center;
  405. padding-left: 5px;
  406. }
  407. @keyframes notice {
  408. 100% {
  409. transform: translate3d(-100%, 0, 0);
  410. }
  411. }
  412. </style>