z-index.uvue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <view style="flex-grow: 1;">
  3. <view style="position:absolute;z-index:0;">
  4. <view class="common fixed default">
  5. <text>position: fixed</text>
  6. <text>z-index: 10</text>
  7. </view>
  8. <view class="common fixed specified">
  9. <text>position: fixed</text>
  10. <text>z-index: 5</text>
  11. </view>
  12. <view class="common fixed floor">
  13. <text>position: fixed</text>
  14. <text>z-index: -1</text>
  15. </view>
  16. </view>
  17. <view style="top: 250px;">
  18. <view class="common" style="background-color: red;z-index: 10;">
  19. <text>z-index: 10</text>
  20. </view>
  21. <view ref="view" class="common" style="background-color: green;z-index: 5;top: -37px;left: 87px;"
  22. @click="changeZIndex(20)">
  23. <text>z-index: {{zIndex}}</text>
  24. <text>点击修改z-index</text>
  25. </view>
  26. <view class="common" style="background-color: blue;top: -75px;left: 175px;">
  27. <text>z-index: 0</text>
  28. </view>
  29. </view>
  30. <view>
  31. <view>
  32. <view class="common fixed popup" style="background-color: aqua;z-index: 5;">
  33. <text>position: fixed</text>
  34. <text>z-index: 5</text>
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. <view v-if="autoTest">
  40. <view style="z-index: 1;position: fixed;">111</view>
  41. <view style="width: 750rpx;">222</view>
  42. </view>
  43. </template>
  44. <script>
  45. export default {
  46. data () {
  47. return {
  48. zIndex: 5,
  49. // 自动化测试
  50. autoTest: false
  51. }
  52. },
  53. methods: {
  54. changeZIndex (zIndex: number) {
  55. this.zIndex = 20;
  56. (this.$refs['view'] as UniElement).style.setProperty('z-index', zIndex);
  57. }
  58. }
  59. }
  60. </script>
  61. <style>
  62. .common {
  63. width: 125px;
  64. height: 125px;
  65. justify-content: center;
  66. align-items: center;
  67. }
  68. .fixed {
  69. position: fixed;
  70. }
  71. .default {
  72. background-color: red;
  73. z-index: 10;
  74. top: var(--uni-safe-area-inset-top);
  75. left: var(--uni-safe-area-inset-left);
  76. }
  77. .specified {
  78. background-color: green;
  79. z-index: 5;
  80. /* #ifdef APP || MP */
  81. top: 87px;
  82. left: 87px;
  83. /* #endif */
  84. /* #ifdef WEB */
  85. top: calc(var(--uni-safe-area-inset-top) + 87px);
  86. left: calc(var(--uni-safe-area-inset-left) + 87px);
  87. /* #endif */
  88. }
  89. .floor {
  90. background-color: chocolate;
  91. /* #ifdef APP || MP */
  92. top: 250px;
  93. left: 175px;
  94. /* #endif */
  95. /* #ifdef WEB */
  96. top: calc(var(--uni-safe-area-inset-top) + 250px);
  97. left: calc(var(--uni-safe-area-inset-left) + 175px);
  98. /* #endif */
  99. z-index: -1;
  100. }
  101. .popup {
  102. /* #ifdef APP || MP */
  103. top: 320px;
  104. left: 87px;
  105. /* #endif */
  106. /* #ifdef WEB */
  107. top: calc(var(--uni-safe-area-inset-top) + 320px);
  108. left: calc(var(--uni-safe-area-inset-left) + 87px);
  109. /* #endif */
  110. height: 40px;
  111. }
  112. </style>