uni-notice-bar.vue 10 KB

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