uni-match-media.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <template>
  2. <view v-show="matches">
  3. <slot />
  4. </view>
  5. </template>
  6. <script>
  7. let mediaQueryObserver
  8. export default {
  9. name: 'UniMatchMedia',
  10. props: {
  11. width: {
  12. type: [Number, String],
  13. default: ''
  14. },
  15. minWidth: {
  16. type: [Number, String],
  17. default: ''
  18. },
  19. maxWidth: {
  20. type: [Number, String],
  21. default: ''
  22. },
  23. height: {
  24. type: [Number, String],
  25. default: ''
  26. },
  27. minHeight: {
  28. type: [Number, String],
  29. default: ''
  30. },
  31. maxHeight: {
  32. type: [Number, String],
  33. default: ''
  34. },
  35. orientation: {
  36. type: String,
  37. default: ''
  38. }
  39. },
  40. data () {
  41. return {
  42. matches: true
  43. }
  44. },
  45. mounted () {
  46. mediaQueryObserver = uni.createMediaQueryObserver(this)
  47. mediaQueryObserver.observe({
  48. width: this.width,
  49. maxWidth: this.maxWidth,
  50. minWidth: this.minWidth,
  51. height: this.height,
  52. minHeight: this.minHeight,
  53. maxHeight: this.maxHeight,
  54. orientation: this.orientation
  55. }, matches => {
  56. this.matches = matches
  57. })
  58. },
  59. destroyed () {
  60. mediaQueryObserver.disconnect()
  61. }
  62. }
  63. </script>
  64. <style>
  65. view {
  66. display: block;
  67. }
  68. </style>