z-index.uvue 2.6 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
<template>
  <view style="flex-grow: 1;">
    <view style="position:absolute;z-index:0;">
      <view class="common fixed default">
        <text>position: fixed</text>
        <text>z-index: 10</text>
      </view>
      <view class="common fixed specified">
        <text>position: fixed</text>
        <text>z-index: 5</text>
      </view>
      <view class="common fixed floor">
        <text>position: fixed</text>
        <text>z-index: -1</text>
      </view>
    </view>
    <view style="top: 250px;">
      <view class="common" style="background-color: red;z-index: 10;">
        <text>z-index: 10</text>
      </view>
      <view ref="view" class="common" style="background-color: green;z-index: 5;top: -37px;left: 87px;"
        @click="changeZIndex(20)">
        <text>z-index: {{zIndex}}</text>
        <text>点击修改z-index</text>
      </view>
      <view class="common" style="background-color: blue;top: -75px;left: 175px;">
        <text>z-index: 0</text>
      </view>
    </view>
    <view>
      <view>
        <view class="common fixed popup" style="background-color: aqua;z-index: 5;">
          <text>position: fixed</text>
          <text>z-index: 5</text>
        </view>
      </view>
    </view>
  </view>
  <view v-if="autoTest">
    <view style="z-index: 1;position: fixed;">111</view>
    <view style="width: 750rpx;">222</view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        zIndex: 5,
        // 自动化测试
        autoTest: false
      }
    },
    methods: {
      changeZIndex(zIndex : number) {
        this.zIndex = 20;
        (this.$refs['view'] as UniElement).style.setProperty('z-index', zIndex);
      }
    }
  }
</script>

<style>
  .common {
    width: 125px;
    height: 125px;
    justify-content: center;
    align-items: center;
  }

  .fixed {
    position: fixed;
  }

  .default {
    background-color: red;
    z-index: 10;
    top: var(--window-top);
  }

  .specified {
    background-color: green;
    z-index: 5;
    left: 87px;
    /* #ifdef APP */
    top: 87px;
    /* #endif */
    /* #ifdef H5 */
    top: calc(var(--window-top) + 87px);
    /* #endif */
  }

  .floor {
    background-color: chocolate;
    /* #ifdef APP */
    top: 250px;
    /* #endif */
    /* #ifdef H5 */
    top: calc(var(--window-top) + 250px);
    /* #endif */
    left: 175px;
    z-index: -1;
  }

  .popup {
    /* #ifdef APP */
    top: 320px;
    /* #endif */
    /* #ifdef H5 */
    top: calc(var(--window-top) + 320px);
    /* #endif */
    left: 87px;
    height: 40px;
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
115
</style>