transition.uvue 7.7 KB
Newer Older
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2 3
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
4
  <!-- #endif -->
DCloud-WZF's avatar
DCloud-WZF 已提交
5 6 7
    <view>
      <view class="container">
        <text class="text">点击修改宽度</text>
8
        <view class="base-style transition-width" id="widthOrHeight" @click="changeWidthOrHeight"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
9 10 11
      </view>
      <view class="container">
        <text class="text">点击修改Margin</text>
12
        <view class="base-style transition-margin" id="styleMargin" @click="changeMargin"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
13 14 15
      </view>
      <view class="container">
        <text class="text">点击修改Padding</text>
16 17
        <view class="base-style transition-padding" id="stylePadding" @click="changePadding">
          <view style="background-color: black; height: 50px; width: 50px"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
18 19 20 21
        </view>
      </view>
      <view class="container">
        <text class="text">点击修改Background</text>
22
        <view class="base-style transition-background" id="styleBackground" @click="changeBackground"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
23
      </view>
张磊 已提交
24 25 26 27
      <view class="container">
        <text class="text">动态修改Background</text>
        <view class="base-style" id="propertyStyleBackground" @click="propertyChangeBackground"></view>
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
28 29
      <view class="container">
        <text class="text">点击修改Transform</text>
30
        <view class="base-style transition-transform" id="styleTransform" @click="changeTransform"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
31
      </view>
32
      <view class="container" @click="changeTransformWithOrigin">
张磊 已提交
33
        <text class="text">点击修改Transform(含transform-origin)</text>
34
        <view class="base-style transition-transform" style="transform-origin: 0 0;" id="styleTransformWithOrigin"></view>
张磊 已提交
35
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
36 37
      <view class="container">
        <text class="text">点击修改Border</text>
38
        <view class="base-style transition-border" id="styleBorder" @click="changeBorder"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
39 40 41
      </view>
      <view class="container">
        <text class="text">点击修改Position</text>
42
        <view class="base-style transition-position" id="stylePosition" @click="changestylePosition"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
43 44
      </view>
    </view>
45
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
46 47
  </scroll-view>
  <!-- #endif -->
48 49 50
</template>

<script>
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  export default {
    data() {
      return {
        isTranstionWidthOrHeight: false,
        widthOrHeight: null as Element | null,
        isTranstionChangeMargin: false,
        styleMargin: null as Element | null,
        isTransitionStylePadding: false,
        stylePadding: null as Element | null,
        isTransitionstyleBackground: false,
        styleBackground: null as Element | null,
        isTransitionStyleTransform: false,
        styleTransform: null as Element | null,
        isTransitionstyleBorder: false,
        styleBorder: null as Element | null,
        isTransitionstylePosition: false,
        stylePosition: null as Element | null,
张磊 已提交
68 69 70 71 72
        isSetTransition: false,
        isTransitionpropertystyleBackground: false,
        propertyStyleBackground: null as Element | null,
        isTransitionStyleTransformWithOrigin: false,
        styleTransformWithOrigin: null as Element | null,
73
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
74
    },
75 76 77 78 79 80 81 82
    onReady() {
      this.widthOrHeight = uni.getElementById("widthOrHeight")
      this.styleMargin = uni.getElementById("styleMargin")
      this.stylePadding = uni.getElementById("stylePadding")
      this.styleBackground = uni.getElementById("styleBackground")
      this.styleTransform = uni.getElementById("styleTransform")
      this.styleBorder = uni.getElementById("styleBorder")
      this.stylePosition = uni.getElementById("stylePosition")
张磊 已提交
83 84
      this.propertyStyleBackground = uni.getElementById("propertyStyleBackground")
      this.styleTransformWithOrigin = uni.getElementById("styleTransformWithOrigin")
DCloud-WZF's avatar
DCloud-WZF 已提交
85
    },
86 87 88
    methods: {
      changeWidthOrHeight() {
        this.widthOrHeight?.style?.setProperty("width", this.isTranstionWidthOrHeight
89 90
          ? '190px'
          : '300px')
91 92 93 94
        this.isTranstionWidthOrHeight = !this.isTranstionWidthOrHeight
      },
      changeMargin() {
        this.styleMargin?.style?.setProperty("margin-top", this.isTranstionChangeMargin
95 96
          ? '0px'
          : '50px'
97 98
        )
        this.styleMargin?.style?.setProperty("margin-left", this.isTranstionChangeMargin
99 100
          ? '0px'
          : '50px'
101 102 103
        )
        this.isTranstionChangeMargin = !this.isTranstionChangeMargin
      },
104

105 106
      changePadding() {
        this.stylePadding?.style?.setProperty("padding-top", this.isTransitionStylePadding
107 108
          ? '0px'
          : '50px')
109
        this.stylePadding?.style?.setProperty("padding-left", this.isTransitionStylePadding
110 111
          ? '0px'
          : '50px')
112 113 114 115 116 117 118 119 120 121 122 123 124
        this.isTransitionStylePadding = !this.isTransitionStylePadding
      },
      changeBackground() {
        this.styleBackground?.style?.setProperty("background-color", this.isTransitionstyleBackground
          ? 'brown'
          : 'black'
        )
        this.styleBackground?.style?.setProperty("opacity", this.isTransitionstyleBackground
          ? '1'
          : '0.5'
        )
        this.isTransitionstyleBackground = !this.isTransitionstyleBackground
      },
张磊 已提交
125 126 127
      propertyChangeBackground() {
        if (!this.isSetTransition) {
          this.propertyStyleBackground?.style?.setProperty("transition-property", "background-color")
张磊 已提交
128
          this.propertyStyleBackground?.style?.setProperty("transition-duration", "1000ms")
张磊 已提交
129 130 131 132 133 134 135 136
          this.isSetTransition = true
        }
        this.propertyStyleBackground?.style?.setProperty("background-color", this.isTransitionpropertystyleBackground
          ? 'brown'
          : 'black'
        )
        this.isTransitionpropertystyleBackground = !this.isTransitionpropertystyleBackground
      },
137 138 139 140 141 142 143
      changeTransform() {
        this.styleTransform?.style?.setProperty("transform", this.isTransitionStyleTransform
          ? 'rotate(0deg)'
          : 'rotate(135deg)'
        )
        this.isTransitionStyleTransform = !this.isTransitionStyleTransform
      },
张磊 已提交
144 145 146 147 148 149 150
      changeTransformWithOrigin() {
        this.styleTransformWithOrigin?.style?.setProperty("transform", this.isTransitionStyleTransformWithOrigin
          ? 'scaleX(1)'
          : 'scaleX(0)'
        )
        this.isTransitionStyleTransformWithOrigin = !this.isTransitionStyleTransformWithOrigin
      },
151 152 153 154 155 156 157 158 159
      changeBorder() {
        this.styleBorder?.style?.setProperty("border-color", this.isTransitionstyleBorder
          ? 'brown'
          : 'yellow'
        )
        this.isTransitionstyleBorder = !this.isTransitionstyleBorder
      },
      changestylePosition() {
        this.stylePosition?.style?.setProperty("left", this.isTransitionstylePosition
160 161
          ? '0px'
          : '100px'
162 163 164
        )
        this.isTransitionstylePosition = !this.isTransitionstylePosition
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
165
    },
166
  }
167 168 169
</script>

<style>
170
  .container {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
171
    margin: 7px;
172 173 174 175
    background-color: white;
  }

  .text {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
176 177
    margin-top: 10px;
    margin-bottom: 16px;
178
  }
179

180
  .base-style {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
181 182
    width: 200px;
    height: 200px;
183 184
    background-color: brown;
  }
185

186 187
  .transition-width {
    transition-property: width;
张磊 已提交
188
    transition-duration: 1s;
189
  }
190

191 192
  .transition-margin {
    transition-property: margin-left, margin-top;
张磊 已提交
193
    transition-duration: 1s;
194
  }
195

196 197
  .transition-padding {
    transition-property: padding-left, padding-top;
张磊 已提交
198
    transition-duration: 1s;
199
  }
200

201 202
  .transition-background {
    transition-property: background-color, opacity;
张磊 已提交
203
    transition-duration: 1s;
204
  }
205

206 207
  .transition-transform {
    transition-property: transform;
张磊 已提交
208
    transition-duration: 1s;
209
  }
210

211 212 213 214 215
  .transition-border {
    border-width: 5px;
    border-color: brown;
    border-style: solid;
    transition-property: border-color;
张磊 已提交
216
    transition-duration: 1s;
217
  }
218

219
  .transition-position {
张磊 已提交
220
    left: 0px;
221
    transition-property: left;
张磊 已提交
222
    transition-duration: 1s;
223
  }
张磊 已提交
224
</style>