uni-notice-bar.vue 10.0 KB

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