uni-notice-bar.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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="closeempty" :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="closeempty" :color="color" size="12" @click="close" />
  13. <uni-icons v-if="showIcon === true || showIcon === 'true'" class="uni-noticebar-icon" type="sound" :color="color" size="14" />
  14. <!-- #endif -->
  15. <view ref="textBox" class="uni-noticebar__content-wrapper" :class="{'uni-noticebar__content-wrapper--scrollable':scrollable, 'uni-noticebar__content-wrapper--single':!scrollable && (single || moreText)}">
  16. <view :id="elIdBox" class="uni-noticebar__content" :class="{'uni-noticebar__content--scrollable':scrollable, 'uni-noticebar__content--single':!scrollable && (single || moreText)}">
  17. <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)}" :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>
  18. </view>
  19. </view>
  20. <view v-if="showGetMore === true || showGetMore === 'true'" class="uni-noticebar__more" @click="clickMore">
  21. <text v-if="moreText" :style="{ color: moreColor }" class="uni-noticebar__more-text">{{ moreText }}</text>
  22. <uni-icons type="arrowright" :color="moreColor" size="14" />
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import uniIcons from '../uni-icons/uni-icons.vue'
  28. // #ifdef APP-NVUE
  29. const dom = weex.requireModule('dom');
  30. const animation = weex.requireModule('animation');
  31. // #endif
  32. /**
  33. * NoticeBar 自定义导航栏
  34. * @description 通告栏组件
  35. * @tutorial https://ext.dcloud.net.cn/plugin?id=30
  36. * @property {Number} speed 文字滚动的速度,默认100px/秒
  37. * @property {String} text 显示文字
  38. * @property {String} backgroundColor 背景颜色
  39. * @property {String} color 文字颜色
  40. * @property {String} moreColor 查看更多文字的颜色
  41. * @property {String} moreText 设置“查看更多”的文本
  42. * @property {Boolean} single = [true|false] 是否单行
  43. * @property {Boolean} scrollable = [true|false] 是否滚动,为true时,NoticeBar为单行
  44. * @property {Boolean} showIcon = [true|false] 是否显示左侧喇叭图标
  45. * @property {Boolean} showClose = [true|false] 是否显示左侧关闭按钮
  46. * @property {Boolean} showGetMore = [true|false] 是否显示右侧查看更多图标,为true时,NoticeBar为单行
  47. * @event {Function} click 点击 NoticeBar 触发事件
  48. * @event {Function} close 关闭 NoticeBar 触发事件
  49. * @event {Function} getmore 点击”查看更多“时触发事件
  50. */
  51. export default {
  52. name: 'UniNoticeBar',
  53. components: {
  54. uniIcons
  55. },
  56. props: {
  57. text: {
  58. type: String,
  59. default: ''
  60. },
  61. moreText: {
  62. type: String,
  63. default: ''
  64. },
  65. backgroundColor: {
  66. type: String,
  67. default: '#fffbe8'
  68. },
  69. speed: {
  70. // 默认1s滚动100px
  71. type: Number,
  72. default: 100
  73. },
  74. color: {
  75. type: String,
  76. default: '#de8c17'
  77. },
  78. moreColor: {
  79. type: String,
  80. default: '#999999'
  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 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. padding: 6px 12px;
  275. margin-bottom: 10px;
  276. }
  277. .uni-noticebar-close {
  278. margin-right: 5px;
  279. }
  280. .uni-noticebar-icon {
  281. margin-right: 5px;
  282. }
  283. .uni-noticebar__content-wrapper {
  284. flex: 1;
  285. flex-direction: column;
  286. overflow: hidden;
  287. }
  288. .uni-noticebar__content-wrapper--single {
  289. /* #ifndef APP-NVUE */
  290. line-height: 18px;
  291. /* #endif */
  292. }
  293. .uni-noticebar__content-wrapper--single,
  294. .uni-noticebar__content-wrapper--scrollable {
  295. flex-direction: row;
  296. }
  297. /* #ifndef APP-NVUE */
  298. .uni-noticebar__content-wrapper--scrollable {
  299. position: relative;
  300. height: 18px;
  301. }
  302. /* #endif */
  303. .uni-noticebar__content--scrollable {
  304. /* #ifdef APP-NVUE */
  305. flex: 0;
  306. /* #endif */
  307. /* #ifndef APP-NVUE */
  308. flex: 1;
  309. display: block;
  310. overflow: hidden;
  311. /* #endif */
  312. }
  313. .uni-noticebar__content--single {
  314. /* #ifndef APP-NVUE */
  315. display: flex;
  316. flex: none;
  317. width: 100%;
  318. justify-content: center;
  319. /* #endif */
  320. }
  321. .uni-noticebar__content-text {
  322. font-size: 14px;
  323. line-height: 18px;
  324. /* #ifndef APP-NVUE */
  325. word-break: break-all;
  326. /* #endif */
  327. }
  328. .uni-noticebar__content-text--single {
  329. /* #ifdef APP-NVUE */
  330. lines: 1;
  331. /* #endif */
  332. /* #ifndef APP-NVUE */
  333. display: block;
  334. width: 100%;
  335. white-space: nowrap;
  336. /* #endif */
  337. overflow: hidden;
  338. text-overflow: ellipsis;
  339. }
  340. .uni-noticebar__content-text--scrollable {
  341. /* #ifdef APP-NVUE */
  342. lines: 1;
  343. padding-left: 750rpx;
  344. /* #endif */
  345. /* #ifndef APP-NVUE */
  346. position: absolute;
  347. display: block;
  348. height: 18px;
  349. line-height: 18px;
  350. white-space: nowrap;
  351. padding-left: 100%;
  352. animation: notice 10s 0s linear infinite both;
  353. animation-play-state: paused;
  354. /* #endif */
  355. }
  356. .uni-noticebar__more {
  357. /* #ifndef APP-NVUE */
  358. display: inline-flex;
  359. /* #endif */
  360. flex-direction: row;
  361. flex-wrap: nowrap;
  362. align-items: center;
  363. padding-left: 5px;
  364. }
  365. .uni-noticebar__more-text {
  366. font-size: 14px;
  367. }
  368. @keyframes notice {
  369. 100% {
  370. transform: translate3d(-100%, 0, 0);
  371. }
  372. }
  373. </style>