uni-dropdown.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <template>
  2. <view class="dropdown">
  3. <view class="dropdown-link" @click="open">
  4. <slot name="dropdown-link"></slot>
  5. </view>
  6. <uni-transition key="1" name="mask" mode-class="fade" :styles="maskClass" :duration="300" :show="showTrans" @click="close" />
  7. <uni-transition key="2" mode-class="slide-top" class="dropdown-box" name="content" :style="dropdownClass" :duration="300" :show="showTrans">
  8. <view class="dropdown-content" @click="close">
  9. <slot name="dropdown"></slot>
  10. </view>
  11. </uni-transition>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. name: 'uniDropdown',
  17. data(){
  18. return {
  19. showTrans: false,
  20. maskClass: {
  21. position: 'fixed',
  22. zIndex: 1,
  23. bottom: 0,
  24. top: 0,
  25. left: 0,
  26. right: 0,
  27. backgroundColor: 'rgba(0, 0, 0, 0)'
  28. },
  29. dropdownClass: {}
  30. }
  31. },
  32. methods:{
  33. open(){
  34. this.showTrans = true;
  35. const query = uni.createSelectorQuery().in(this);
  36. query.select('.dropdown-link').boundingClientRect(data => {
  37. this.dropdownClass = {
  38. position: 'absolute',
  39. zIndex: 1,
  40. top: '100%',
  41. right: '0px'
  42. }
  43. }).exec();
  44. },
  45. close(){
  46. this.showTrans = false;
  47. }
  48. }
  49. }
  50. </script>
  51. <style lang="scss">
  52. .dropdown-mask {
  53. position: fixed;
  54. top: 0;
  55. left: 0;
  56. width: 100vw;
  57. height: 100vh;
  58. background: none;
  59. z-index: 9999;
  60. }
  61. .dropdown {
  62. position: relative;
  63. }
  64. .dropdown-box{
  65. z-index: 999 !important;
  66. }
  67. </style>