popup.nvue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <template>
  2. <view>
  3. <text class="example-info">弹出层组件用于弹出一个覆盖到页面上的内容,使用场景如:底部弹出分享弹窗、页面插屏广告等。</text>
  4. <uni-section title="基本示例" type="line"></uni-section>
  5. <view class="example-body message">
  6. <button class="button" type="primary" @click="toggle('top')"><text class="button-text">顶部弹出</text></button>
  7. <button class="button" type="primary" @click="toggle('bottom')"><text class="button-text">底部弹出</text></button>
  8. <button class="button" type="primary" @click="toggle('center')"><text class="button-text">居中弹出</text></button>
  9. </view>
  10. <uni-section title="自定义弹出层(dialog + message) 示例" type="line"></uni-section>
  11. <view class="example-body message">
  12. <button class="button popup__success" @click="toggleMessage('success')"><text class="button-text">成功</text></button>
  13. <button class="button popup__error" @click="toggleMessage('error')"><text class="button-text">错误</text></button>
  14. <button class="button popup__warn" @click="toggleMessage('warn')"><text class="button-text">警告</text></button>
  15. <button class="button popup__info" @click="toggleMessage('info')"><text class="button-text">信息</text></button>
  16. </view>
  17. <uni-section title="提交信息 (input + 延迟关闭)" type="line"></uni-section>
  18. <view class="example-body dialog">
  19. <view class="dialog-box">
  20. <text class="dialog-text">输入内容:{{value}}</text>
  21. </view>
  22. <button class="button" type="primary" @click="confirmDialog"><text class="button-text">输入对话框</text></button>
  23. </view>
  24. <uni-section title="底部分享示例" type="line"></uni-section>
  25. <view class="example-body share">
  26. <button class="button" type="primary" @click="confirmShare"><text class="button-text">分享模版示例</text></button>
  27. </view>
  28. <!-- 基本示例 -->
  29. <uni-popup id="popup" ref="popup" :type="type" :animation="false" @change="change">
  30. <view class="popup-content">popup 内容,此示例没有动画效果</view>
  31. </uni-popup>
  32. <!-- 消息提示 -->
  33. <uni-popup id="popupMessage" ref="popupMessage" type="message" @change="change">
  34. <uni-popup-message :type="msgType" :message="message" :duration="2000"></uni-popup-message>
  35. </uni-popup>
  36. <!-- 对话框 -->
  37. <uni-popup id="popupDialog" ref="popupDialog" type="dialog" @change="change">
  38. <uni-popup-dialog :type="msgType" title="通知" content="欢迎使用 uni-popup!" :before-close="true" @confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
  39. </uni-popup>
  40. <!-- 提交信息 -->
  41. <uni-popup id="dialogInput" ref="dialogInput" type="dialog" @change="change">
  42. <uni-popup-dialog mode="input" title="输入内容" value="对话框预置提示内容!" placeholder="请输入内容" @confirm="dialogInputConfirm"></uni-popup-dialog>
  43. </uni-popup>
  44. <!-- 分享示例 -->
  45. <uni-popup id="popupShare" ref="popupShare" type="share" @change="change">
  46. <uni-popup-share title="分享到" @select="select"></uni-popup-share>
  47. </uni-popup>
  48. </view>
  49. </template>
  50. <script>
  51. // import uniPopupMessage from '@/components/uni-popup/uni-popup-message.vue'
  52. // import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
  53. // import uniPopupShare from '@/components/uni-popup/uni-popup-share.vue'
  54. export default {
  55. // components: {
  56. // uniPopupMessage,
  57. // uniPopupDialog,
  58. // uniPopupShare
  59. // },
  60. data() {
  61. return {
  62. type: 'top',
  63. msgType: 'success',
  64. message: '这是一条成功消息提示',
  65. value: '默认输入的内容'
  66. }
  67. },
  68. onReady() {
  69. // 页面打开自动打开对话框
  70. setTimeout(() => {
  71. this.msgType = 'success'
  72. this.$refs.popupDialog.open()
  73. }, 500)
  74. },
  75. methods: {
  76. /**
  77. * 打开基础内容
  78. */
  79. toggle(type) {
  80. this.type = type
  81. this.$refs.popup.open()
  82. },
  83. /**
  84. * 打开消息提示
  85. * @param {Object} type
  86. */
  87. toggleMessage(type) {
  88. this.msgType = type
  89. switch (type) {
  90. case 'success':
  91. this.message = '这是一条成功消息提示'
  92. break
  93. case 'warn':
  94. this.message = '这是一条警告消息提示'
  95. break
  96. case 'info':
  97. this.message = '这是一条消息提示'
  98. break
  99. case 'error':
  100. this.message = '这是一条错误消息提示'
  101. break
  102. }
  103. this.$refs.popupDialog.open()
  104. },
  105. /**
  106. * 打开提交信息
  107. */
  108. confirmDialog() {
  109. this.$refs.dialogInput.open()
  110. },
  111. /**
  112. * 分享模版
  113. */
  114. confirmShare() {
  115. this.$refs.popupShare.open()
  116. },
  117. /**
  118. * 选择内容
  119. */
  120. select(e, done) {
  121. uni.showToast({
  122. title: `您选择了第${e.index+1}项:${e.item.text}`,
  123. icon: 'none'
  124. })
  125. done()
  126. },
  127. /**
  128. * 对话框点击确认按钮
  129. */
  130. dialogConfirm(done) {
  131. this.$refs.popupMessage.open()
  132. console.log('点击确认');
  133. // 需要执行 done 才能关闭对话框
  134. done()
  135. },
  136. /**
  137. * 输入对话框的确定事件
  138. */
  139. dialogInputConfirm(done, val) {
  140. uni.showLoading({
  141. title: '3秒后会关闭'
  142. })
  143. console.log(val);
  144. this.value = val
  145. setTimeout(() => {
  146. uni.hideLoading()
  147. // 关闭窗口后,恢复默认内容
  148. done()
  149. }, 3000)
  150. },
  151. /**
  152. * 对话框取消按钮
  153. */
  154. dialogClose(done) {
  155. this.msgType = 'info'
  156. this.message = '点击了对话框的取消按钮'
  157. this.$refs.popupMessage.open()
  158. // 需要执行 done 才能关闭对话框
  159. done()
  160. },
  161. /**
  162. * popup 状态发生变化触发
  163. * @param {Object} e
  164. */
  165. change(e) {
  166. console.log('popup ' + e.type + ' 状态', e.show)
  167. },
  168. },
  169. /**
  170. * 拦截应用返回事件,仅仅 app 端生效
  171. */
  172. onBackPress() {
  173. }
  174. }
  175. </script>
  176. <style>
  177. @charset "UTF-8";
  178. /* 头条小程序组件内不能引入字体 */
  179. /* #ifdef MP-TOUTIAO */
  180. @font-face {
  181. font-family: uniicons;
  182. font-weight: normal;
  183. font-style: normal;
  184. src: url("~@/static/uni.ttf") format("truetype");
  185. }
  186. /* #endif */
  187. /* #ifndef APP-NVUE */
  188. page {
  189. display: flex;
  190. flex-direction: column;
  191. box-sizing: border-box;
  192. background-color: #efeff4;
  193. min-height: 100%;
  194. height: auto;
  195. }
  196. view {
  197. font-size: 14px;
  198. line-height: inherit;
  199. }
  200. .example {
  201. padding: 0 15px 15px;
  202. }
  203. .example-info {
  204. padding: 15px;
  205. color: #3b4144;
  206. background: #ffffff;
  207. }
  208. .example-body {
  209. /* #ifndef APP-NVUE */
  210. display: flex;
  211. /* #endif */
  212. flex-direction: row;
  213. flex-wrap: wrap;
  214. justify-content: center;
  215. padding: 0;
  216. font-size: 14px;
  217. background-color: #ffffff;
  218. }
  219. /* #endif */
  220. .example {
  221. padding: 0 15px;
  222. }
  223. .example-info {
  224. /* #ifndef APP-NVUE */
  225. display: block;
  226. /* #endif */
  227. padding: 15px;
  228. color: #3b4144;
  229. background-color: #ffffff;
  230. font-size: 14px;
  231. line-height: 20px;
  232. }
  233. .example-info-text {
  234. font-size: 14px;
  235. line-height: 20px;
  236. color: #3b4144;
  237. }
  238. .example-body {
  239. flex-direction: column;
  240. padding: 15px;
  241. background-color: #ffffff;
  242. }
  243. .word-btn-white {
  244. font-size: 18px;
  245. color: #FFFFFF;
  246. }
  247. .word-btn {
  248. /* #ifndef APP-NVUE */
  249. display: flex;
  250. /* #endif */
  251. flex-direction: row;
  252. align-items: center;
  253. justify-content: center;
  254. border-radius: 6px;
  255. height: 48px;
  256. margin: 15px;
  257. background-color: #007AFF;
  258. }
  259. .word-btn--hover {
  260. background-color: #4ca2ff;
  261. }
  262. .example-body {
  263. /* #ifndef APP-NVUE */
  264. display: block;
  265. /* #endif */
  266. padding: 5px 15px;
  267. }
  268. .popup-content {
  269. background-color: #fff;
  270. padding: 15px;
  271. }
  272. /* #ifndef APP-NVUE */
  273. .button {
  274. margin: 0;
  275. padding: 0;
  276. line-height: 1;
  277. }
  278. /* #endif */
  279. .button {
  280. /* #ifndef APP-NVUE */
  281. display: flex;
  282. /* #endif */
  283. flex-direction: row;
  284. align-items: center;
  285. justify-content: center;
  286. flex: 1;
  287. height: 40px;
  288. margin: 0 15px;
  289. }
  290. .button-text {
  291. color: #fff;
  292. font-size: 14px;
  293. }
  294. .message {
  295. /* #ifndef APP-NVUE */
  296. display: flex;
  297. /* #endif */
  298. flex-direction: row;
  299. justify-content: center;
  300. padding: 15px 0;
  301. }
  302. .dialog {
  303. padding: 15px 0;
  304. }
  305. .dialog-box {
  306. margin-left: 15px;
  307. margin-bottom: 15px;
  308. }
  309. .dialog-text {
  310. font-size: 14px;
  311. color: #666;
  312. }
  313. .share {
  314. padding: 15px 0;
  315. }
  316. .popup__success {
  317. color: #fff;
  318. background-color: #4cd964;
  319. }
  320. .popup__warn {
  321. color: #fff;
  322. background-color: #f0ad4e;
  323. }
  324. .popup__error {
  325. color: #fff;
  326. background-color: #dd524d;
  327. }
  328. .popup__info {
  329. color: #fff;
  330. background-color: #909399;
  331. }
  332. </style>