超簡單實現自定義彈窗


快應用promt.showDialog接口提供的彈框只能顯示文字、按鈕簡單的元素,無法實現復雜的頁面效果。但往往產品經理設計的彈框頁面效果很炫,怎么辦呢?

比如實現如下彈窗效果:展示一個列表,左邊顯示圖片,右邊顯示具體介紹,見圖1;提示用戶查看應用隱私協議彈窗效果,里面內容有超鏈接效果,能點擊,見圖2: 

 

 

圖1  列表彈窗                                 圖2  超鏈接彈窗

此問題一般是開發者對快應用組件、樣式屬性使用不熟悉導致的。

 

解決方法

基於stack組件+position:fixed css樣式可以實現自定義彈窗,彈窗顯示隱藏基於if/show指令來控制,具體代碼如下:

  1.  初始化數據模型:

1
2
3
4
5
6
7
8
data() {
       return  {
         list: [1, 2, 3],
         isShow:  false ,
         ifShow1:  false ,
         ifShow2:  false
       }
     },

2. 頁面布局:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<text onclick= "showToast1" >自定義彈窗樣式1</text>
     <text onclick= "showToast2" >自定義彈窗樣式2</text>
     <!-- 彈窗 -->
     <div  if = "{{isShow}}"  class= "modal center-modal" >
       <div class= "toast-box password-box" >
         <block>
           <stack class= "eyes-input-box" >
             <div style= "flex-direction: column;width: 100%;"  if = "{{ifShow1}}" >
               <div style= "width: 100%;" >
                 <text style= "font-size: 32px;font-weight: 500;" >交通違法</text>
                 <text style= "font-size: 25px;text-align: right;padding-left: 400px"  onclick= "hide" >隱藏</text>
               </div>
               <list style= "height: 550px" >
                 <list-item type= "list"  for = "list"  style= "height: 160px;margin-top: 20px" >
                   <div style= "padding-top: 1px;" >
                     <image src= "/Common/icon.png"  style= "border-radius: 100%;width: 100px;height: 100px;" ></image>
                     <div style= "flex-direction: column; margin-left: 20px" >
                       <text style= "font-size: 28px;font-weight: 600;" >二級標題</text>
                       <text style= "font-size: 26px;margin-top: 10px" >相關內容展示,例如商品屬性、價格、上架日期等信息展示</text>
                       <div style= "height: 2px;bottom: 0;margin-top: 25px"  if = "{{$idx<2}}" >
                     </div>
                     </div>
                     
                   </div>
                 </list-item>
               </list>
             </div>
             <div style= "flex-direction: column;width: 100%;"  if = "{{ifShow2}}" >
               <text style= "font-size: 31px;font-weight: 700;" >城市服務</text>
               <text style= "font-weight: 400;" ><span>點擊“同意”,即表示您同意上述內容及</span><a style= "color: #0000ff;"  onclick= "toast" >應用名稱的用戶協議</a>
                 <span>、</span><a style= "color: #0000ff;"  onclick= "toast" >關於應用名稱與隱私的聲明</a></text>
               <div style= "width: 100%;" >
               <text style= "width: 50%; text-align: center; color: #0000ff;"  onclick= "hide" >確認</text>
               <text style= "width: 50%; text-align: center; color: #0000ff;"  onclick= "hide" >取消</text>
               </div>
             </div>
           </stack>
         </block>
       </div>
     </div>

 

頁面樣式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
.container {
     flex-direction: column;
     justify-content: center;
     align-items: center;
     .body {
         width: 100%;
         height: 100%;
         flex-direction: column;
         align-items: center;
         .img {
             margin-top: 500px;
         }
         .txt {
             margin-top: 570px;
         }
     }
  
     // 彈窗樣式
.modal {
     position: fixed;
     flex-direction: column;
     justify-content: flex-end;
     width: 100%;
     height: 100%;
     padding: 0 34px 34px;
     rgba(0, 0, 0, 0.18);
     animation-name: Modal;
     animation-duration: 130ms;
     animation-timing- function : ease- in ;
     .toast-box {
       width: 100%;
       padding: 25px 50px 20.8px;
       border-radius: 34px;
       white;
       flex-direction: column;
       .toast-title {
         font-size: 41.6px;
         line-height: 56px;
         font-weight: 500;
         font-family: HWtext-65ST;
         color:  #000000;
       }
       .toast-tip {
         font-family: HWtext-55ST;
         font-size: 29px;
         color: rgba(0, 0, 0, 0.6);
         line-height: 40px;
         margin-top: 3.6px;
         margin-bottom: 25px;
       }
       .label-box {
         height: 100px;
         border-bottom: 1px solid rgba(0, 0, 0, 0.1);
         .label {
           color:  #000000;
           width: 100%;
           font-family: HWtext-55ST;
           font-size: 34px;
         }
         input {
           margin-right: -11px;
         }
       }
       .manage {
         height: 100px;
         font-family: HWtext-65ST;
         font-size: 29.12px;
         color:  #007dff;
         font-weight: 500;
       }
       .cancel {
         width: 100%;
         height: 75px;
         margin-top: 37.44px;
         text-align: center;
         font-family: HWtext-65ST;
         font-size: 33.28px;
         color:  #007dff;
         font-weight: 500;
       }
       .btn-box {
         justify-content: space-between;
         align-items: center;
         .bind {
           width: 47%;
           height: 60px;
           border-radius: 10px;
           font-family: HWtext-65ST;
           text-align: center;
           font-size: 33.28px;
           color:  #007dff;
           margin: 16px 0;
           font-weight: 500;
         }
         .line {
           height: 50px;
           width: 1px;
           rgba(0, 0, 0, 0.2);
         }
         .quit {
           width: 47%;
           height: 60px;
           border-radius: 10px;
           text-align: center;
           font-family: HWtext-65ST;
           font-size: 33.28px;
           color:  #007dff;
           margin: 16px 0;
           font-weight: 500;
         }
         .bind:active {
           rgba(0, 0, 0, 0.1);  // 待高保真修改
         }
         .quit:active {
           rgba(0, 0, 0, 0.1);  // 待高保真修改
         }
       }
       .nocar-tip {
         height: 99.84px;
         font-size: 33.28px;
         font-family: HWtext-55ST;
         color:  #000000;
         border-bottom: 1px solid rgba(0, 0, 0, 0.1);
       }
     }
     .password-box {
       margin-top: -110px;
       padding: 30.63px 50px 20.8px;
       .toast-title {
         height: 55.64px;
         margin-bottom: 30.12px;
       }
       .password-tip {
         flex-direction: column;
         margin-top: 3.6px;
         margin-bottom: 25px;
         .control-tip {
           font-family: HWtext-55ST;
           font-size: 24.96px;
           color: rgba(0, 0, 0, 0.6);
         }
       }
       .error-password-tip {
         font-family: HWtext-55ST;
         font-size: 24.96px;
         color:  #fa2a2d;
         height: 32.76px;
         margin-bottom: 37.4px;
         margin-top: 0;
       }
       .btn-box {
         .bind {
           margin-top: 8px;
           margin-bottom: 8px;
         }
         .quit {
           margin-top: 8px;
           margin-bottom: 8px;
         }
       }
   
       .eyes-input-box {
         width: 100%;
         flex-direction: row;
         justify-content: flex-end;
         .input-password {
           width: 100%;
           height: 94.64px;
           border: 1px solid  #ffffff;
           border-bottom-color:  #000000;
           padding: 11.44px 0;
           margin-bottom: 20.8px;
         }
         .password-text {
           width: 100%;
           height: 94.64px;
           padding: 11.44px 0;
           border-bottom: 1px solid  #000000;
           margin-bottom: 20.8px;
           color:  #000000;
           font-size: 36px;
         }
         .eyes-img {
           width: 49.92px;
           height: 49.92px;
           margin-top: 23px;
         }
       }
   
       .password-loading-box {
         flex-direction: row;
         align-items: center;
         justify-content: space-between;
         height: 150px;
         .loading-text {
           font-family: HWtext-55ST;
           font-size: 33.28px;
           color: rgba(0, 0, 0, 0.96);
         }
         .loading-circular {
           width: 102px;
           height: 102px;
           color:  #666666;
         }
       }
     }
   }
}

 

事件綁定:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
showToast1() {
       this .isShow =  true ;
       this .ifShow1 =  true
       this .ifShow2 =  false
     },
     showToast2() {
       this .isShow =  true ;
       this .ifShow1 =  false
       this .ifShow2 =  true
     },
     hide(){
       this .isShow =  false ;
     },
     toast() {
       prompt.showToast({
         message:  '查看隱私協議'
       })
}

 

欲了解更多詳情,請參閱:

快應用開發指導文檔:https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickapp-whitepaper

快應用Style樣式:

https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-style

快應用通用樣式:

https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-common-settings#h1-1575449213432

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM