transition.uvue 10.8 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
      </view>
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
10 11 12 13
      <view class="container">
        <text class="text">点击修改宽度(递增)</text>
        <view class="width-progress transition-width" id="widthProgress" @click="changeWidthProgress"></view>
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
14 15
      <view class="container">
        <text class="text">点击修改Margin</text>
16
        <view class="base-style transition-margin" id="styleMargin" @click="changeMargin"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
17 18 19
      </view>
      <view class="container">
        <text class="text">点击修改Padding</text>
20 21
        <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 已提交
22 23 24
        </view>
      </view>
      <view class="container">
25
        <text class="text">点击修改background-color和opacity</text>
26
        <view class="base-style transition-background" id="styleBackground" @click="changeBackground"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
27
      </view>
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
28
      <view class="container">
29
        <text class="text">点击修改background-color(rgba)</text>
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
30 31 32 33
        <view style="flex: 1;">
          <view class="base-style transition-background" id="styleBackground2" @click="changeBackground2"></view>
        </view>
      </view>
张磊 已提交
34
      <view class="container">
35
        <text class="text">动态修改background-color和duration</text>
张磊 已提交
36 37
        <view class="base-style" id="propertyStyleBackground" @click="propertyChangeBackground"></view>
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
38 39
      <view class="container">
        <text class="text">点击修改Transform</text>
40
        <view class="base-style transition-transform" id="styleTransform" @click="changeTransform"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
41
      </view>
42 43 44 45
      <view class="container">
        <text class="text">点击修改TransformTranslate</text>
        <view class="base-style transition-transform" id="transformTranslate" @click="changeTransformTranslate"></view>
      </view>
张磊 已提交
46 47 48 49
      <view class="container">
        <text class="text">点击修改Transform和宽</text>
        <view class="base-style transition-transform-width" id="styleTransformWithWidth" @click="changeTransformWithWidth"></view>
      </view>
50
      <view class="container" @click="changeTransformWithOrigin">
张磊 已提交
51
        <text class="text">点击修改Transform(含transform-origin)</text>
52
        <view class="base-style transition-transform" style="transform-origin: 0 0;" id="styleTransformWithOrigin"></view>
张磊 已提交
53
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
54 55
      <view class="container">
        <text class="text">点击修改Border</text>
56
        <view class="base-style transition-border" id="styleBorder" @click="changeBorder"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
57 58 59
      </view>
      <view class="container">
        <text class="text">点击修改Position</text>
60
        <view class="base-style transition-position" id="stylePosition" @click="changestylePosition"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
61 62
      </view>
    </view>
63
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
64 65
  </scroll-view>
  <!-- #endif -->
66 67 68
</template>

<script>
69 70 71 72
  export default {
    data() {
      return {
        isTranstionWidthOrHeight: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
73
        widthOrHeight: null as UniElement | null,
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
74 75
        widthProgress: null as UniElement | null,
        progressWidth: 200,
76
        isTranstionChangeMargin: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
77
        styleMargin: null as UniElement | null,
78
        isTransitionStylePadding: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
79
        stylePadding: null as UniElement | null,
80
        isTransitionstyleBackground: false,
81
        isTransitionstyleBackground2: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
82
        styleBackground: null as UniElement | null,
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
83
        styleBackground2: null as UniElement | null,
84
        isTransitionStyleTransform: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
85
        styleTransform: null as UniElement | null,
张磊 已提交
86 87
        isTransitionStyleTransformWithWidth:false,
        styleTransformWithWidth: null as UniElement | null,
88
        isTransitionstyleBorder: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
89
        styleBorder: null as UniElement | null,
90
        isTransitionstylePosition: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
91
        stylePosition: null as UniElement | null,
张磊 已提交
92 93
        isSetTransition: false,
        isTransitionpropertystyleBackground: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
94
        propertyStyleBackground: null as UniElement | null,
张磊 已提交
95
        isTransitionStyleTransformWithOrigin: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
96
        styleTransformWithOrigin: null as UniElement | null,
97 98
        styleTransformTranslate: null as UniElement | null,
        isTransformTranslate: false
99
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
100
    },
101 102
    onReady() {
      this.widthOrHeight = uni.getElementById("widthOrHeight")
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
103
      this.widthProgress = uni.getElementById("widthProgress")
104 105 106
      this.styleMargin = uni.getElementById("styleMargin")
      this.stylePadding = uni.getElementById("stylePadding")
      this.styleBackground = uni.getElementById("styleBackground")
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
107
      this.styleBackground2 = uni.getElementById("styleBackground2")
108 109 110
      this.styleTransform = uni.getElementById("styleTransform")
      this.styleBorder = uni.getElementById("styleBorder")
      this.stylePosition = uni.getElementById("stylePosition")
张磊 已提交
111 112
      this.propertyStyleBackground = uni.getElementById("propertyStyleBackground")
      this.styleTransformWithOrigin = uni.getElementById("styleTransformWithOrigin")
张磊 已提交
113
      this.styleTransformWithWidth = uni.getElementById("styleTransformWithWidth")
114
      this.styleTransformTranslate = uni.getElementById("transformTranslate")
DCloud-WZF's avatar
DCloud-WZF 已提交
115
    },
116 117 118
    methods: {
      changeWidthOrHeight() {
        this.widthOrHeight?.style?.setProperty("width", this.isTranstionWidthOrHeight
张磊 已提交
119
          ? '200px'
120
          : '300px')
121 122
        this.isTranstionWidthOrHeight = !this.isTranstionWidthOrHeight
      },
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
123 124 125 126
      changeWidthProgress() {
        this.progressWidth += 20
        this.widthProgress?.style?.setProperty("width", this.progressWidth + 'px')
      },
127 128
      changeMargin() {
        this.styleMargin?.style?.setProperty("margin-top", this.isTranstionChangeMargin
129 130
          ? '0px'
          : '50px'
131 132
        )
        this.styleMargin?.style?.setProperty("margin-left", this.isTranstionChangeMargin
133 134
          ? '0px'
          : '50px'
135 136 137
        )
        this.isTranstionChangeMargin = !this.isTranstionChangeMargin
      },
138

139 140
      changePadding() {
        this.stylePadding?.style?.setProperty("padding-top", this.isTransitionStylePadding
141 142
          ? '0px'
          : '50px')
143
        this.stylePadding?.style?.setProperty("padding-left", this.isTransitionStylePadding
144 145
          ? '0px'
          : '50px')
146 147 148 149 150 151 152 153 154 155 156 157 158
        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
      },
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
159
      changeBackground2() {
160
        this.styleBackground2?.style?.setProperty("background-color", this.isTransitionstyleBackground2
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
161 162 163
          ? 'brown'
          : 'rgba(0, 0, 0, 0.5);'
        )
164
        this.isTransitionstyleBackground2 = !this.isTransitionstyleBackground2
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
165
      },
张磊 已提交
166 167 168
      propertyChangeBackground() {
        if (!this.isSetTransition) {
          this.propertyStyleBackground?.style?.setProperty("transition-property", "background-color")
张磊 已提交
169
          this.propertyStyleBackground?.style?.setProperty("transition-duration", "1000ms")
张磊 已提交
170 171 172 173 174 175 176 177
          this.isSetTransition = true
        }
        this.propertyStyleBackground?.style?.setProperty("background-color", this.isTransitionpropertystyleBackground
          ? 'brown'
          : 'black'
        )
        this.isTransitionpropertystyleBackground = !this.isTransitionpropertystyleBackground
      },
178 179 180 181 182 183 184
      changeTransform() {
        this.styleTransform?.style?.setProperty("transform", this.isTransitionStyleTransform
          ? 'rotate(0deg)'
          : 'rotate(135deg)'
        )
        this.isTransitionStyleTransform = !this.isTransitionStyleTransform
      },
185 186 187 188 189
      changeTransformTranslate() {
        const translate = this.isTransformTranslate ? `translate(0%,0%)` : `translate(100%,0%)`
        this.styleTransformTranslate?.style?.setProperty('transform', translate)
        this.isTransformTranslate = !this.isTransformTranslate
      },
张磊 已提交
190 191 192 193 194 195 196 197 198 199 200
      changeTransformWithWidth() {
        this.styleTransformWithWidth?.style?.setProperty("transform", this.isTransitionStyleTransformWithWidth
          ? 'rotate(0deg)'
          : 'rotate(135deg)'
        )
        this.styleTransformWithWidth?.style?.setProperty("width", this.isTransitionStyleTransformWithWidth
          ? '200px'
          : '100px'
        )
        this.isTransitionStyleTransformWithWidth = !this.isTransitionStyleTransformWithWidth
      },
张磊 已提交
201 202 203 204 205 206 207
      changeTransformWithOrigin() {
        this.styleTransformWithOrigin?.style?.setProperty("transform", this.isTransitionStyleTransformWithOrigin
          ? 'scaleX(1)'
          : 'scaleX(0)'
        )
        this.isTransitionStyleTransformWithOrigin = !this.isTransitionStyleTransformWithOrigin
      },
208 209 210 211 212 213 214 215 216
      changeBorder() {
        this.styleBorder?.style?.setProperty("border-color", this.isTransitionstyleBorder
          ? 'brown'
          : 'yellow'
        )
        this.isTransitionstyleBorder = !this.isTransitionstyleBorder
      },
      changestylePosition() {
        this.stylePosition?.style?.setProperty("left", this.isTransitionstylePosition
217 218
          ? '0px'
          : '100px'
219 220 221
        )
        this.isTransitionstylePosition = !this.isTransitionstylePosition
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
222
    },
223
  }
224 225 226
</script>

<style>
227
  .container {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
228
    margin: 7px;
229 230 231 232
    background-color: white;
  }

  .text {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
233 234
    margin-top: 10px;
    margin-bottom: 16px;
235
  }
236

237
  .base-style {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
238 239
    width: 200px;
    height: 200px;
240
    background-color: brown;
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
241 242 243 244 245 246
  }

  .width-progress {
    width: 200px;
    height: 200px;
    background-color: brown;
247
  }
248

DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
249 250 251 252 253
  .transform-bgColor {
    transition-property: background-color;
    transition-duration: 0.5s;
  }

254 255
  .transition-width {
    transition-property: width;
张磊 已提交
256
    transition-duration: 1s;
257
  }
258

259 260
  .transition-margin {
    transition-property: margin-left, margin-top;
张磊 已提交
261
    transition-duration: 1s;
262
  }
263

264 265
  .transition-padding {
    transition-property: padding-left, padding-top;
张磊 已提交
266
    transition-duration: 1s;
267
  }
268

269 270
  .transition-background {
    transition-property: background-color, opacity;
张磊 已提交
271
    transition-duration: 1s;
272
  }
273

274 275
  .transition-transform {
    transition-property: transform;
张磊 已提交
276
    transition-duration: 1s;
277
  }
278

279 280 281 282 283
  .transition-border {
    border-width: 5px;
    border-color: brown;
    border-style: solid;
    transition-property: border-color;
张磊 已提交
284
    transition-duration: 1s;
285
  }
286

287
  .transition-position {
张磊 已提交
288
    left: 0px;
289
    transition-property: left;
张磊 已提交
290
    transition-duration: 1s;
291
  }
张磊 已提交
292 293 294 295
  .transition-transform-width {
    transition-property: transform, width;
    transition-duration: 1s;
  }
张磊 已提交
296
</style>