u-announcement.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <view class="u-announcement">
  3. <view class="u-notice-bar" :style="{backgroundColor: bgColor}" @click="take(true)">
  4. <view class="u-direction-row">
  5. <view class="u-icon-wrap">
  6. <image class="u-icon-left" :src="icon ? icon : '../../../static/image/icon/icon-notice.png'"></image>
  7. <text class="u-text" :style="{color: textColor}">{{name}}</text>
  8. </view>
  9. <view class="u-notice-box" id="u-notice-box">
  10. <view
  11. class="u-notice-content"
  12. id="u-notice-content"
  13. :style="{
  14. animationDuration: animationDuration,
  15. animationPlayState: animationPlayState
  16. }"
  17. >
  18. <text class="u-notice-text u-text" :style="{color: textColor}">{{showText}}</text>
  19. </view>
  20. </view>
  21. <view class="u-icon-wrap">
  22. <image class="u-icon-right" src="../../../static/image/icon/arrow-right-white.png" ></image>
  23. </view>
  24. </view>
  25. </view>
  26. <u-popup :length="600" mode="center" v-model="visibleSync" :zoom="false">
  27. <view class="app-top-image" :style="{backgroundImage: `url(${headerUrl})`}" v-if="headerUrl"></view>
  28. <view class="app-top-image icon" v-else></view>
  29. <view class="u-bottom-content">
  30. <scroll-view scroll-y class="u-content">
  31. {{content}}
  32. </scroll-view>
  33. <view class="u-button" hover-class="u-hover-class" :style="btnStyle" @click="take(false)">
  34. {{btnText}}
  35. </view>
  36. </view>
  37. </u-popup>
  38. </view>
  39. </template>
  40. <script>
  41. import uPopup from '../../../components/basic-component/u-popup/u-popup.vue';
  42. export default {
  43. data() {
  44. return {
  45. animationDuration: '10s',
  46. animationPlayState: 'paused',
  47. showText: '',
  48. visibleSync: false,
  49. showDrawer: false,
  50. timer: null
  51. }
  52. },
  53. components: {
  54. uPopup
  55. },
  56. props: {
  57. content: String,
  58. bgColor: {
  59. type: String,
  60. default: '#f67f79'
  61. },
  62. icon: {
  63. type: String,
  64. default: ''
  65. },
  66. name: {
  67. type: String,
  68. default: '公告'
  69. },
  70. textColor: {
  71. type: String,
  72. default: '#ffffff'
  73. },
  74. headerUrl: {
  75. type: String,
  76. default: ''
  77. },
  78. btnText: {
  79. type: String,
  80. default: '我知道了'
  81. },
  82. btnTextColor: {
  83. type: String,
  84. default: '#ffffff'
  85. },
  86. btnColor: {
  87. type: String,
  88. default: '#ff4544'
  89. },
  90. btnRadius: {
  91. type: String,
  92. default: '40rpx'
  93. },
  94. btnWidth: {
  95. type: Number,
  96. default: 500
  97. }
  98. },
  99. watch: {
  100. content: {
  101. immediate: true,
  102. handler(newVal) {
  103. this.showText = newVal.replace(/[\r\n]/g,"");
  104. this.$nextTick(() => {
  105. this.initSize();
  106. });
  107. }
  108. }
  109. },
  110. methods: {
  111. initSize() {
  112. let query = [];
  113. let textQuery = new Promise(resolve => {
  114. uni.createSelectorQuery().in(this).select(`#u-notice-content`).boundingClientRect().exec(ret => {
  115. this.textWidth = ret[0].width;
  116. resolve();
  117. });
  118. });
  119. query.push(textQuery);
  120. Promise.all(query).then(() => {
  121. this.animationDuration = `${this.textWidth / uni.upx2px(100)}s`;
  122. this.animationPlayState = 'paused';
  123. setTimeout(() => {
  124. this.animationPlayState = 'running';
  125. }, 10);
  126. });
  127. },
  128. take(data) {
  129. this.visibleSync = data;
  130. }
  131. },
  132. computed: {
  133. btnStyle: function() {
  134. return `width: ${this.btnWidth}rpx;color:${this.btnTextColor};background-color:${this.btnColor};border-radius: ${this.btnRadius};`
  135. }
  136. },
  137. mounted() {
  138. this.$nextTick(() => {
  139. this.initSize();
  140. });
  141. }
  142. }
  143. </script>
  144. <style lang="scss" scoped>
  145. .u-notice-bar {
  146. padding: 18upx 24upx;
  147. overflow: hidden;
  148. }
  149. .u-direction-row {
  150. display: flex;
  151. flex-direction: row;
  152. align-items: center;
  153. justify-content: space-between;
  154. }
  155. .u-icon-wrap {
  156. display: flex;
  157. align-items: center;
  158. }
  159. .u-icon-left {
  160. width: 36upx;
  161. height: 36upx;
  162. margin-right: 20upx;
  163. }
  164. .u-notice-box {
  165. flex: 1;
  166. display: flex;
  167. flex-direction: row;
  168. overflow: hidden;
  169. margin-left: 12upx;
  170. }
  171. .u-notice-text {
  172. font-size: 26upx;
  173. word-break: keep-all;
  174. white-space: nowrap
  175. }
  176. .u-notice-content {
  177. animation: u-loop-animation 10s linear infinite both;
  178. text-align: right;
  179. padding-left: 100%;
  180. display: flex;
  181. flex-direction: row;
  182. flex-wrap: nowrap;
  183. }
  184. @keyframes u-loop-animation {
  185. 0% {
  186. transform: translate3d(0, 0, 0);
  187. }
  188. 100% {
  189. transform: translate3d(-100%, 0, 0);
  190. }
  191. }
  192. .u-icon-right {
  193. width: 12upx;
  194. height: 22upx;
  195. margin-left: 10upx;
  196. }
  197. .u-text {
  198. font-size: 26upx;
  199. }
  200. .app-transparent-frame {
  201. width: 600upx;
  202. }
  203. .app-top-image {
  204. height: #{150rpx};
  205. width: #{600rpx};
  206. background-repeat: no-repeat;
  207. background-size: cover;
  208. &.icon {
  209. background-image: url("../../../static/image/icon/announcement.png");
  210. }
  211. }
  212. .u-bottom-content {
  213. background-color: #ffffff;
  214. padding: 50upx;
  215. border-bottom-right-radius: 11upx;
  216. border-bottom-left-radius: 11upx;
  217. }
  218. .u-button {
  219. margin: 0 auto;
  220. font-size: 31upx;
  221. text-align: center;
  222. height: 80upx;
  223. line-height: 80upx;
  224. }
  225. .u-content {
  226. display: block;
  227. min-height: 144upx;
  228. max-height: 285upx;
  229. width: 500upx;
  230. margin-bottom: 66upx;
  231. word-break: break-all;
  232. line-height: 48upx;
  233. }
  234. </style>