z-index.uvue 1.9 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2
<template>
  <view style="flex-grow: 1;">
3
    <view style="position:absolute;z-index:0;">
4
      <view class="common fixed default">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
5 6 7
        <text>position: fixed</text>
        <text>z-index: 10</text>
      </view>
8
      <view class="common fixed specified">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
9 10 11
        <text>position: fixed</text>
        <text>z-index: 5</text>
      </view>
shutao-dc's avatar
shutao-dc 已提交
12 13 14 15
      <view class="common fixed floor">
        <text>position: fixed</text>
        <text>z-index: -1</text>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
16
    </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
17
    <view style="top: 250px;">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
18 19 20
      <view class="common" style="background-color: red;z-index: 10;">
        <text>z-index: 10</text>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
21
      <view ref="view" class="common" style="background-color: green;z-index: 5;top: -37px;left: 87px;"
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
22 23 24 25
        @click="changeZIndex(20)">
        <text>z-index: {{zIndex}}</text>
        <text>点击修改z-index</text>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
26
      <view class="common" style="background-color: blue;top: -75px;left: 175px;">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
27 28
        <text>z-index: 0</text>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        zIndex: 5
      }
    },
    methods: {
      changeZIndex(zIndex : number) {
        this.zIndex = 20;
DCloud-yyl's avatar
DCloud-yyl 已提交
43
        (this.$refs['view'] as UniElement).style.setProperty('z-index', zIndex);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
44 45 46 47 48 49
      }
    }
  }
</script>

<style>
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
.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 */
}
shutao-dc's avatar
shutao-dc 已提交
75 76 77 78 79 80 81 82 83 84 85 86

.floor {
  background-color: chocolate;
  /* #ifdef APP */
  top: 250px;
  /* #endif */
  /* #ifdef H5 */
  top: calc(var(--window-top) + 250px);
  /* #endif */
  left: 175px;
  z-index: -1;
}
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
87
</style>