action-sheet.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view class="uni-btn-v">
  6. <button class="target" type="default" @tap="actionSheetTap">弹出action sheet</button>
  7. </view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. title: 'action-sheet',
  16. buttonRect: {}
  17. }
  18. },
  19. // #ifdef H5
  20. onReady() {
  21. this.getNodeInfo()
  22. window.addEventListener('resize', this.getNodeInfo)
  23. },
  24. beforeDestroy() {
  25. window.removeEventListener('resize', this.getNodeInfo)
  26. },
  27. // #endif
  28. methods: {
  29. actionSheetTap() {
  30. const that = this
  31. uni.showActionSheet({
  32. title: '标题',
  33. itemList: ['item1', 'item2', 'item3', 'item4'],
  34. popover: {
  35. // 104: navbar + topwindow 高度,暂时 fix createSelectorQuery 在 pc 上获取 top 不准确的 bug
  36. top: that.buttonRect.top + 104 + that.buttonRect.height,
  37. left: that.buttonRect.left + that.buttonRect.width / 2
  38. },
  39. success: (e) => {
  40. console.log(e.tapIndex);
  41. uni.showToast({
  42. title: "点击了第" + e.tapIndex + "个选项",
  43. icon: "none"
  44. })
  45. }
  46. })
  47. },
  48. // #ifdef H5
  49. getNodeInfo() {
  50. uni.createSelectorQuery().select('.target').boundingClientRect().exec((ret) => {
  51. const rect = ret[0]
  52. if (rect) {
  53. this.buttonRect = rect
  54. }
  55. });
  56. }
  57. // #endif
  58. }
  59. }
  60. </script>