We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
quickApp GuidesQuick AppSpecial TopicsFrameworkCustom Elementsnoticebar

noticebar

Overview

The noticebar element is used to display bulletins, system notifications, ads and so on. This element has the following features:

  • The content displayed can adapt to the notice bar size.
  • The bulletin text color and background color can be modified.
  • Icons such as the speaker icon can be added.
  • The notice bar can be closed.
  • Scrolling text is supported.

The following figure shows the effect.

The basic layout code is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. <template>
  2. <div class="container">
  3. <text class="section">Basic layout</text>
  4. <noticebar class="notice" show-icon="true" show-close="true" scrollable="true"
  5. background-color="#B0C4DE" color="#DC143C"
  6. text="Quick apps are a new form of installation-free apps developed based on industry standards formulated by Quick App Alliance, which consists of mainstream mobile phone vendors in China." >
  7. </noticebar>
  8. </div>
  9. </template>

Development Procedure

Customizing the Subelements

  1. Design the layout.

    The layout of the notice bar element consists the bulletins, close button, speaker icon, scrolling text and text element for viewing more details.

    1. The layout is displayed horizontally.
    2. The block element is used with the if and else commands to control different combined layouts, reducing layout nesting.
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <template>
    2. <!-- Only one root node is allowed in template. -->
    3. <div class="container" style="background-color: {{backgroundColor}};display:{{displaynotice}}">
    4. <image class="img" if="{{showClose}}" src="./Images/close.png" onclick="close"></image>
    5. <image class="img" if="{{showIcon}}" src="./Images/sound.png"></image>
    6. <block if="{{scrollable}}">
    7. <div id="box" class="box">
    8. <text id="txt1" class="scrolltxt" style="width: {{textWidth}};color: {{color}}">{{ text }}</text>
    9. </div>
    10. </block>
    11. <block elif="{{showGetMore}}">
    12. <text class="elliptxt" style="color: {{color}}">{{ text }}</text>
    13. <div class="moreBox" onclick="getMore">
    14. <text class="moretxt" style="color: {{moreColor}}">{{ moreText }}</text>
    15. <image class="img" src="./Images/more.png"></image>
    16. </div>
    17. </block>
    18. <block else>
    19. <text class="plaintxt" style="color: {{color}}">{{ text }}</text>
    20. </block>
    21. </div>
    22. </template>
  2. Customize the attributes and supported events.

    Supported attributes

    Expand

    Attribute

    Type

    Default Value

    Description

    text

    String

    -

    Text displayed on the notice bar.

    moreText

    String

    -

    Text of the text element. This attribute is used when the content of the notice bar is not completely displayed.

    backgroundColor

    Boolean

    true

    Background color of the notice bar.

    color

    Color

    #de8c17

    Color of the text displayed on the notice bar.

    speed

    Number

    160

    Scrolling speed of the text. The default speed is 160 px per second.

    moreColor

    Color

    #999999

    Text color of the text element.

    scrollable

    Boolean

    false

    Indicates whether text is scrolled.

    NOTE

    If the value is true, the text can be displayed only in a single line on the notice bar.

    showIcon

    Boolean

    false

    Indicates whether to display the icon on the left side.

    showClose

    Boolean

    false

    Indicates whether to display the close icon. Tap this icon to close the notice bar.

    showGetMore

    Boolean

    false

    Indicates whether to display the text element for viewing more details.

    Supported event

    Expand

    Event

    Parameter

    Description

    getmore

    {}

    Event of viewing more details.

Setting Up the Notice Bar

  1. Render the element and then scroll the text.

    The text displays on the notification bar and then starts to scroll left. When the text disappears, it scrolls from the right edge.

  2. When the text is scrolling, the moreText attribute, together with showGetMore and moreColor, does not take effect. The scrolling text can be displayed only in a single line.
    NOTICE

    You need to calculate the actual width of the scrolling text in a single line in advance. If the width is longer than the device width, some text will not be displayed.

    If the text width is smaller than that of the device screen, set the text width to the width of the device screen. Currently, the quick app does not provide the API for calculating the width of the text. However, this width is required for scrolling the text, without which the text is truncated by default. The width in this section is calculated based on the following formula: Width of the text = Width of a single text * Number of characters.

  3. Implement the scrolling by using the translateX animation. Two animations are involved: an animation that scrolls from the initial position to the left and a cyclical animation that scrolls from the right edge to the left edge.

    The animation duration depends on the text width. The wider the text is, the longer the animation duration is.

Collapse
Word wrap
Dark theme
Copy code
  1. onInit() {
  2. console.log("oninit showGetMore=" + this.showGetMore);
  3. console.log("oninit scrollable=" + this.scrollable);
  4. if (this.scrollable) {
  5. this.initScrollNoticeSize();
  6. }
  7. },
  8. initScrollNoticeSize() {
  9. this.deviceWidth = device.getInfoSync().screenWidth;
  10. console.info("initScrollNotice deviceWidth=" + this.deviceWidth);
  11. let len = this.text.length;
  12. console.log("onInit txt len=" + len);
  13. let w = this.singleTxtWidth * len;
  14. if (w < this.deviceWidth) {
  15. this.textWidth = this.deviceWidth;
  16. } else {
  17. this.textWidth = w;
  18. }
  19. console.log("textWidth= " + this.textWidth);
  20. setTimeout(() => {
  21. //this.calTextWidth();
  22. this.calBoxWidth();
  23. }, 1000);
  24. },

Adding the Close Icon

  1. Add the close icon, which will make the notice bar dismiss once tapped. The root node cannot use the if and show commands in IDE. Therefore, the display attribute of CSS is set to none to make the layout invisible.
  2. If the parent element needs to listen to the close event, call $emit('close') in the close() method to notify the parent element.
Collapse
Word wrap
Dark theme
Copy code
  1. close: function () {
  2. console.log("close=");
  3. this.stopAnimation = true;
  4. this.displaynotice = "none";
  5. this.$emit('close');
  6. },

Adding the Text Element for Viewing More Details

You need to add a text element so that users can tap it to view more details when the content is not completely displayed. Generally, tapping it can redirect users to a notice page.
  1. Process the redirection logic in the parent element.
    NOTICE

    Do not process the redirection logic in the subelement because each notice bar corresponds to a different notice page.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. getMoreDetail:function (evt){
    2. console.info("getMoreDetail evt:"+JSON.stringify(evt));
    3. prompt.showToast({
    4. message: JSON.stringify(evt.detail),
    5. duration: 2000,
    6. gravity: 'center'
    7. })
    8. }
  2. Listen to the getmore event in the parent element.
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <noticebar class="notice" show-get-more="true" more-text="View more details" @getmore="getMoreDetail" text="[The text cannot be displayed in a single line] Quick apps are a new form of installation-free apps developed based on industry standards." ></noticebar>
  3. Trigger the notification in the subelement.
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. getMore: function () {
    2. console.info("getMore"+this.text);
    3. this.$emit('getmore', this.text);
    4. },

Cases

Icons and the Text Element for Viewing More Details Are Partially Displayed

The following figure shows the effect.

Cause analysis: The default value of flex-shrink is 1 in CSS. When the space calculated by the engine is insufficient, the icon is compressed.

Solution: Set flex-shrink to 0 to not resize the element.

Text Element for Viewing More Details Is Not Displayed on the Right Side of the Screen

The following figure shows the effect.

Solution: Set the width of elliptxt to 100% so that the text element is always displayed on the right side of the screen.

Sample Code

noticebar.ux file:

Collapse
Word wrap
Dark theme
Copy code
  1. <template>
  2. <!-- Only one root node is allowed in template. -->
  3. <div class="container" style="background-color: {{backgroundColor}};display:{{displaynotice}}">
  4. <image class="img" if="{{showClose}}" src="./Images/close.png" onclick="close"></image>
  5. <image class="img" if="{{showIcon}}" src="./Images/sound.png"></image>
  6. <block if="{{scrollable}}">
  7. <div id="box" class="box">
  8. <text id="txt1" class="scrolltxt" style="width: {{textWidth}};color: {{color}}">{{ text }}</text>
  9. </div>
  10. </block>
  11. <block elif="{{showGetMore}}">
  12. <text class="elliptxt" style="color: {{color}}">{{ text }}</text>
  13. <div class="moreBox" onclick="getMore">
  14. <text class="moretxt" style="color: {{moreColor}}">{{ moreText }}</text>
  15. <image class="img" src="./Images/more.png"></image>
  16. </div>
  17. </block>
  18. <block else>
  19. <text class="plaintxt" style="color: {{color}}">{{ text }}</text>
  20. </block>
  21. </div>
  22. </template>
  23. <style>
  24. .container {
  25. width: 100%;
  26. flex-direction: row;
  27. align-items: center;
  28. padding-left: 16px;
  29. padding-right: 16px;
  30. }
  31. .img {
  32. width: 28px;
  33. height: 28px;
  34. align-self: center;
  35. margin-right: 8px;
  36. resize-mode: contain;
  37. flex-shrink: 0;
  38. }
  39. .box {
  40. flex-direction: column;
  41. align-items: flex-start;
  42. }
  43. .moreBox {
  44. flex-direction: row;
  45. align-items: center;
  46. margin-left: 20px;
  47. flex-shrink: 0;
  48. }
  49. .moretxt {
  50. lines: 1;
  51. flex-shrink: 0;
  52. }
  53. .title {
  54. lines: 1;
  55. }
  56. .scrolltxt {
  57. lines: 1;
  58. margin-left: 20px;
  59. }
  60. .elliptxt {
  61. text-overflow: ellipsis;
  62. lines: 1;
  63. width: 100%;
  64. }
  65. .plaintxt {
  66. align-self: flex-start;
  67. }
  68. </style>
  69. <script>
  70. import device from '@system.device';
  71. module.exports = {
  72. props: {
  73. text: {
  74. type: String,
  75. default: ''
  76. },
  77. moreText: {
  78. type: String,
  79. default: ''
  80. },
  81. backgroundColor: {
  82. type: String,
  83. default: '#fffbe8'
  84. },
  85. speed: {
  86. // The default scrolling speed is 100 px per second.
  87. type: Number,
  88. default: 160
  89. },
  90. color: {
  91. type: String,
  92. default: '#de8c17'
  93. },
  94. moreColor: {
  95. type: String,
  96. default: '#999999'
  97. },
  98. scrollable: {
  99. // Scroll the text or not. The text does not scroll by default.
  100. type: Boolean,
  101. default: false
  102. },
  103. showIcon: {
  104. // Display the icon on the left side or not.
  105. type: [Boolean, String],
  106. default: false
  107. },
  108. showGetMore: {
  109. // Display the view more details icon on the right side or not.
  110. type: [Boolean, String],
  111. default: false
  112. },
  113. showClose: {
  114. // Display the close button on the left side or not.
  115. type: Boolean,
  116. default: false
  117. }
  118. },
  119. data() {
  120. return {
  121. singleTxtWidth: 80,
  122. textWidth: 750,
  123. deviceWidth: 0,
  124. boxWidth: 0,
  125. speed: 100,
  126. stopAnimation: false,
  127. displaynotice: "flex",
  128. animationDuration: 'none',
  129. animationPlayState: 'paused',
  130. animationDelay: '0s'
  131. }
  132. },
  133. onInit() {
  134. console.log("oninit showGetMore=" + this.showGetMore);
  135. console.log("oninit scrollable=" + this.scrollable);
  136. if (this.scrollable) {
  137. this.initScrollNoticeSize();
  138. }
  139. },
  140. initScrollNoticeSize() {
  141. this.deviceWidth = device.getInfoSync().screenWidth;
  142. console.info("initScrollNotice deviceWidth=" + this.deviceWidth);
  143. let len = this.text.length;
  144. console.log("onInit txt len=" + len);
  145. let w = this.singleTxtWidth * len;
  146. if (w < this.deviceWidth) {
  147. this.textWidth = this.deviceWidth;
  148. } else {
  149. this.textWidth = w;
  150. }
  151. console.log("textWidth= " + this.textWidth);
  152. setTimeout(() => {
  153. //this.calTextWidth();
  154. this.calBoxWidth();
  155. }, 1000);
  156. },
  157. calTextWidth() {
  158. var that = this;
  159. this.$element('txt1').getBoundingClientRect({
  160. success(res) {
  161. let msg = JSON.stringify(res);
  162. console.log ('calTextWidth current coordinate:' + msg);
  163. that.textleft = res.left;
  164. console.log("calTextWidth this.textleft=" + that.textleft);
  165. },
  166. fail() {
  167. console.log ('calTextWidth failed.');
  168. },
  169. complete() {
  170. console.log('calTextWidth complete')
  171. }
  172. })
  173. },
  174. calBoxWidth() {
  175. var that = this;
  176. this.$element('box').getBoundingClientRect({
  177. success(res) {
  178. let msg = JSON.stringify(res);
  179. console.log ('calBoxWidth current coordinate:' + msg);
  180. that.boxWidth = res.width;
  181. that.startAnimateScroll();
  182. },
  183. fail() {
  184. console.log ('calBoxWidth failed.');
  185. },
  186. complete() {
  187. console.log('calBoxWidth complete')
  188. }
  189. })
  190. },
  191. startAnimateScroll() {
  192. var that = this;
  193. this.duration = this.textWidth / this.speed * 1000;
  194. console.log("startAnimateScroll this.duration=" + this.duration)
  195. this.animationDelay = this.boxWidth / this.speed;
  196. let dis = -1 * this.textWidth;
  197. var options = {
  198. duration: this.duration,
  199. easing: 'linear',
  200. delay: this.animationDelay,
  201. fill: 'forwards'
  202. }
  203. var frames = [
  204. {
  205. transform: {
  206. translateX: 0,
  207. }
  208. },
  209. {
  210. transform: {
  211. translateX: dis,
  212. }
  213. }];
  214. var animation = this.$element('txt1').animate(frames, options);
  215. animation.play();
  216. animation.onfinish = function () {
  217. console.log("animation onfinish");
  218. that.animationLoop();
  219. }
  220. animation.oncancel = function () {
  221. console.log("animation oncancel");
  222. }
  223. },
  224. animationLoop() {
  225. if (this.stopAnimation) {
  226. return;
  227. }
  228. var that = this;
  229. this.duration = this.textWidth / this.speed * 1000;
  230. var options = {
  231. duration: this.duration, easing: 'linear',
  232. delay: 0,
  233. fill: 'forwards'
  234. }
  235. let value1 = this.boxWidth + 200;
  236. console.log("animationLoop value1=" + value1);
  237. let value2 = -1 * this.textWidth;
  238. var frames = [
  239. {
  240. transform: {
  241. translateX: value1,
  242. }
  243. },
  244. {
  245. transform: {
  246. translateX: value2,
  247. }
  248. }];
  249. var animation = this.$element('txt1').animate(frames, options);
  250. animation.play();
  251. animation.onfinish = function () {
  252. console.log("animation onfinish");
  253. that.animationLoop();
  254. }
  255. animation.oncancel = function () {
  256. console.log("animation oncancel");
  257. }
  258. },
  259. onDestroy: function () {
  260. console.log("ondestroy");
  261. this.stopAnimation = true;
  262. },
  263. click: function () {
  264. },
  265. close: function () {
  266. console.log("close=");
  267. this.stopAnimation = true;
  268. this.displaynotice = "none";
  269. this.$emit('close');
  270. },
  271. getMore: function () {
  272. console.info("getMore"+this.text);
  273. this.$emit('getmore', this.text);
  274. },
  275. }
  276. </script>

hello.ux file:

Collapse
Word wrap
Dark theme
Copy code
  1. <import name="noticebar" src="../Component/noticebar.ux"></import>
  2. <template>
  3. <!-- Only one root node is allowed in template. -->
  4. <div class="container">
  5. <text class="example-info">The noticebar element is used to display bulletins, system notifications, ads and so on. Icons, colors and the animations can be customized.</text>
  6. <text class="section">Content in full display or not</text>
  7. <noticebar class="notice" text="Quick App Overview" show-get-more="true" more-text="View more details" @getmore="getMoreDetail"></noticebar>
  8. <noticebar class="notice" text="[Display the content in multiple lines.] Quick apps are a new form of installation-free apps developed based on industry standards formulated by Quick App Alliance, which consists of mainstream mobile phone vendors in China." ></noticebar>
  9. <noticebar class="notice" show-get-more="true" more-text="View more details" @getmore="getMoreDetail" text="[The text cannot be displayed in a single line] Quick apps are a new form of installation-free apps developed based on industry standards." ></noticebar>
  10. <text class="section">Set the text color and background color</text>
  11. <noticebar class="notice" background-color="#B0C4DE" color="#DC143C" text="Quick apps can be accessed from the home screen, HUAWEI Assistant∙TODAY, Huawei Quick App Center, HUAWEI AppGallery, and more." ></noticebar>
  12. <text class="section">Set the speaker icon and the text element for viewing more details.</text>
  13. <noticebar class="notice" show-icon="true" text="JavaScript and CSS are used for development, and quick apps have only 1/5 code of their Android native apps." ></noticebar>
  14. <noticebar class="notice" show-icon="true" show-get-more="true" @getmore="getMoreDetail" text="Quick apps can be accessed from the home screen, HUAWEI Assistant∙TODAY, Huawei Quick App Center, HUAWEI AppGallery, and more." ></noticebar>
  15. <noticebar class="notice" show-icon="true" show-get-more="true" more-text="View more details" @getmore="getMoreDetail" text="Quick apps can be accessed from the home screen, HUAWEI Assistant∙TODAY, Huawei Quick App Center, HUAWEI AppGallery, and more." ></noticebar>
  16. <text class="section">Set the close icon</text>
  17. <noticebar class="notice" show-close="true" text=" Redirection across platforms becomes much easier in diverse scenarios, without the need to depend on a specific app." ></noticebar>
  18. <text class="section">Scroll the text</text>
  19. <noticebar class="notice" scrollable="true" text="Quick apps are a new form of installation-free apps developed based on industry standards formulated by Quick App Alliance, which consists of mainstream mobile phone vendors in China." ></noticebar>
  20. </div>
  21. </template>
  22. <style>
  23. .container {
  24. flex-direction: column;
  25. padding-left: 20px;
  26. padding-right: 20px;
  27. }
  28. .example-info{
  29. margin-top: 30px;
  30. }
  31. .notice{
  32. margin-top: 20px;
  33. }
  34. .section{
  35. background-color: #87cefa;
  36. width: 100%;
  37. height: 50px;
  38. margin-top: 25px;
  39. padding-left: 16px;
  40. }
  41. </style>
  42. <script>
  43. import prompt from '@system.prompt';
  44. module.exports = {
  45. data: {
  46. },
  47. onInit() {
  48. this.$page.setTitleBar({
  49. text: 'menu',
  50. textColor: '#ffffff',
  51. backgroundColor: '#007DFF',
  52. backgroundOpacity: 0.5,
  53. menu: true
  54. });
  55. },
  56. getMoreDetail:function (evt){
  57. console.info("getMoreDetail evt:"+JSON.stringify(evt));
  58. prompt.showToast({
  59. message: JSON.stringify(evt.detail),
  60. duration: 2000,
  61. gravity: 'center'
  62. })
  63. }
  64. }
  65. </script>
Search in Guides
Enter a keyword.