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

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

<style>
  .common {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
47 48
    width: 125px;
    height: 125px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
49 50 51
    justify-content: center;
    align-items: center;
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
52 53 54 55 56

  .fixed {
    position: fixed;
  }
</style>