popup.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <view class="container">
  3. <uni-card is-full :is-shadow="false">
  4. <text class="uni-h6">弹出层组件用于弹出一个覆盖到页面上的内容,使用场景如:底部弹出分享弹窗、页面插屏广告等。</text>
  5. </uni-card>
  6. <uni-section title="基本示例" type="line">
  7. <view class="example-body box">
  8. <button class="button" type="primary" @click="toggle('top')"><text class="button-text">顶部</text></button>
  9. <button class="button" type="primary" @click="toggle('bottom')"><text class="button-text">底部</text></button>
  10. <button class="button" type="primary" @click="toggle('center')"><text class="button-text">居中</text></button>
  11. <button class="button" type="primary" @click="toggle('left')"><text class="button-text">左侧</text></button>
  12. <button class="button" type="primary" @click="toggle('right')"><text class="button-text">右侧</text></button>
  13. </view>
  14. </uni-section>
  15. <uni-section title="提示消息" type="line">
  16. <view class="example-body box">
  17. <button class="button popup-success" @click="messageToggle('success')"><text
  18. class="button-text success-text">成功</text></button>
  19. <button class="button popup-error" @click="messageToggle('error')"><text
  20. class="button-text error-text">失败</text></button>
  21. <button class="button popup-warn" @click="messageToggle('warn')"><text
  22. class="button-text warn-text">警告</text></button>
  23. <button class="button popup-info" @click="messageToggle('info')"><text
  24. class="button-text info-text">信息</text></button>
  25. </view>
  26. </uni-section>
  27. <uni-section title="对话框示例" type="line" class="hideOnPc">
  28. <view class="example-body box">
  29. <button class="button popup-success" @click="dialogToggle('success')"><text
  30. class="button-text success-text">成功</text></button>
  31. <button class="button popup-error" @click="dialogToggle('error')"><text
  32. class="button-text error-text">失败</text></button>
  33. <button class="button popup-warn" @click="dialogToggle('warn')"><text
  34. class="button-text warn-text">警告</text></button>
  35. <button class="button popup-info" @click="dialogToggle('info')"><text
  36. class="button-text info-text">信息</text></button>
  37. </view>
  38. </uni-section>
  39. <uni-section title="输入框示例" type="line" padding>
  40. <view class="dialog-box">
  41. <text class="dialog-text">输入内容:{{ value }}</text>
  42. </view>
  43. <button class="button" type="primary" @click="inputDialogToggle"><text
  44. class="button-text">输入对话框</text></button>
  45. </uni-section>
  46. <uni-section title="底部分享示例" type="line" padding>
  47. <button class="button" type="primary" @click="shareToggle"><text class="button-text">分享模版示例</text></button>
  48. </uni-section>
  49. <view>
  50. <!-- 普通弹窗 -->
  51. <uni-popup ref="popup" background-color="#fff" @change="change">
  52. <view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }"><text
  53. class="text">popup 内容</text></view>
  54. </uni-popup>
  55. </view>
  56. <view>
  57. <!-- 提示信息弹窗 -->
  58. <uni-popup ref="message" type="message">
  59. <uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
  60. </uni-popup>
  61. </view>
  62. <view>
  63. <!-- 提示窗示例 -->
  64. <uni-popup ref="alertDialog" type="dialog">
  65. <uni-popup-dialog :type="msgType" title="通知" content="欢迎使用 uni-popup!" @confirm="dialogConfirm"
  66. @close="dialogClose"></uni-popup-dialog>
  67. </uni-popup>
  68. </view>
  69. <view>
  70. <!-- 输入框示例 -->
  71. <uni-popup ref="inputDialog" type="dialog">
  72. <uni-popup-dialog ref="inputClose" mode="input" title="输入内容" value="对话框预置提示内容!"
  73. placeholder="请输入内容" @confirm="dialogInputConfirm"></uni-popup-dialog>
  74. </uni-popup>
  75. </view>
  76. <view>
  77. <!-- 分享示例 -->
  78. <uni-popup ref="share" type="share">
  79. <uni-popup-share></uni-popup-share>
  80. </uni-popup>
  81. </view>
  82. </view>
  83. </template>
  84. <script>
  85. export default {
  86. data() {
  87. return {
  88. type: 'center',
  89. msgType: 'success',
  90. messageText: '这是一条成功提示',
  91. value: ''
  92. }
  93. },
  94. onReady() {},
  95. methods: {
  96. change(e) {
  97. console.log('当前模式:' + e.type + ',状态:' + e.show);
  98. },
  99. toggle(type) {
  100. this.type = type
  101. // open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
  102. this.$refs.popup.open(type)
  103. },
  104. messageToggle(type) {
  105. this.msgType = type
  106. this.messageText = `这是一条${type}消息提示`
  107. this.$refs.message.open()
  108. },
  109. dialogToggle(type) {
  110. this.msgType = type
  111. this.$refs.alertDialog.open()
  112. },
  113. dialogConfirm() {
  114. console.log('点击确认')
  115. this.messageText = `点击确认了 ${this.msgType} 窗口`
  116. this.$refs.message.open()
  117. },
  118. inputDialogToggle() {
  119. this.$refs.inputDialog.open()
  120. },
  121. dialogClose() {
  122. console.log('点击关闭')
  123. },
  124. dialogInputConfirm(val) {
  125. uni.showLoading({
  126. title: '3秒后会关闭'
  127. })
  128. setTimeout(() => {
  129. uni.hideLoading()
  130. console.log(val)
  131. this.value = val
  132. // 关闭窗口后,恢复默认内容
  133. this.$refs.inputDialog.close()
  134. }, 3000)
  135. },
  136. shareToggle() {
  137. this.$refs.share.open()
  138. }
  139. }
  140. }
  141. </script>
  142. <style lang="scss">
  143. @mixin flex {
  144. /* #ifndef APP-NVUE */
  145. display: flex;
  146. /* #endif */
  147. flex-direction: row;
  148. }
  149. @mixin height {
  150. /* #ifndef APP-NVUE */
  151. height: 100%;
  152. /* #endif */
  153. /* #ifdef APP-NVUE */
  154. flex: 1;
  155. /* #endif */
  156. }
  157. .box {
  158. @include flex;
  159. }
  160. .button {
  161. @include flex;
  162. align-items: center;
  163. justify-content: center;
  164. flex: 1;
  165. height: 35px;
  166. margin: 0 5px;
  167. border-radius: 5px;
  168. }
  169. .example-body {
  170. background-color: #fff;
  171. padding: 10px 0;
  172. }
  173. .button-text {
  174. color: #fff;
  175. font-size: 12px;
  176. }
  177. .popup-content {
  178. @include flex;
  179. align-items: center;
  180. justify-content: center;
  181. padding: 15px;
  182. height: 50px;
  183. background-color: #fff;
  184. }
  185. .popup-height {
  186. @include height;
  187. width: 200px;
  188. }
  189. .text {
  190. font-size: 12px;
  191. color: #333;
  192. }
  193. .popup-success {
  194. color: #fff;
  195. background-color: #e1f3d8;
  196. }
  197. .popup-warn {
  198. color: #fff;
  199. background-color: #faecd8;
  200. }
  201. .popup-error {
  202. color: #fff;
  203. background-color: #fde2e2;
  204. }
  205. .popup-info {
  206. color: #fff;
  207. background-color: #f2f6fc;
  208. }
  209. .success-text {
  210. color: #09bb07;
  211. }
  212. .warn-text {
  213. color: #e6a23c;
  214. }
  215. .error-text {
  216. color: #f56c6c;
  217. }
  218. .info-text {
  219. color: #909399;
  220. }
  221. .dialog,
  222. .share {
  223. /* #ifndef APP-NVUE */
  224. display: flex;
  225. /* #endif */
  226. flex-direction: column;
  227. }
  228. .dialog-box {
  229. padding: 10px;
  230. }
  231. .dialog .button,
  232. .share .button {
  233. /* #ifndef APP-NVUE */
  234. width: 100%;
  235. /* #endif */
  236. margin: 0;
  237. margin-top: 10px;
  238. padding: 3px 0;
  239. flex: 1;
  240. }
  241. .dialog-text {
  242. font-size: 14px;
  243. color: #333;
  244. }
  245. </style>