transition.uvue 11.4 KB
Newer Older
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2 3
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
4
  <!-- #endif -->
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    <view class="container">
      <text class="text">点击修改宽度</text>
      <view class="base-style transition-width" id="widthOrHeight" @click="changeWidthOrHeight"></view>
    </view>
    <view class="container">
      <text class="text">点击修改宽度(递增)</text>
      <view class="width-progress transition-width" id="widthProgress" @click="changeWidthProgress"></view>
    </view>
    <view class="container">
      <text class="text">点击修改Margin</text>
      <view class="base-style transition-margin" id="styleMargin" @click="changeMargin"></view>
    </view>
    <view class="container">
      <text class="text">点击修改Padding</text>
      <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 已提交
21
      </view>
22 23 24 25 26 27 28 29 30
    </view>
    <view class="container">
      <text class="text">点击修改background-color和opacity</text>
      <view class="base-style transition-background" id="styleBackground" @click="changeBackground"></view>
    </view>
    <view class="container">
      <text class="text">点击修改background-color(rgba)</text>
      <view style="flex: 1;">
        <view class="base-style transition-background" id="styleBackground2" @click="changeBackground2"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
31 32
      </view>
    </view>
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
    <view class="container">
      <text class="text">点击修改opacity渐隐渐现</text>
      <view class="base-style transition-opacity" id="styleOpacity" @click="changeStyleOpacity"></view>
    </view>
    <view class="container">
      <text class="text">动态修改background-color和duration</text>
      <view class="base-style" id="propertyStyleBackground" @click="propertyChangeBackground"></view>
    </view>
    <view class="container">
      <text class="text">点击修改Transform</text>
      <view class="base-style transition-transform" id="styleTransform" @click="changeTransform"></view>
    </view>
    <view class="container">
      <text class="text">点击修改TransformTranslate</text>
      <view class="base-style transition-transform" id="transformTranslate" @click="changeTransformTranslate"></view>
    </view>
    <view class="container">
      <text class="text">点击修改Transform和宽</text>
      <view class="base-style transition-transform-width" id="styleTransformWithWidth"
        @click="changeTransformWithWidth"></view>
    </view>
    <view class="container" @click="changeTransformWithOrigin">
      <text class="text">点击修改Transform(含transform-origin)</text>
      <view class="base-style transition-transform" style="transform-origin: 0 0;" id="styleTransformWithOrigin"></view>
    </view>
    <view class="container">
      <text class="text">点击修改Border</text>
      <view class="base-style transition-border" id="styleBorder" @click="changeBorder"></view>
    </view>
    <view class="container">
      <text class="text">点击修改Position</text>
      <view class="base-style transition-position" id="stylePosition" @click="changestylePosition"></view>
    </view>
66
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
67 68
  </scroll-view>
  <!-- #endif -->
69 70 71
</template>

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

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

<style>
241
  .container {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
242
    margin: 7px;
243 244 245 246
    background-color: white;
  }

  .text {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
247 248
    margin-top: 10px;
    margin-bottom: 16px;
249
  }
250

251
  .base-style {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
252 253
    width: 200px;
    height: 200px;
254 255
    background-color: brown;
  }
256

257 258 259 260 261 262 263 264 265 266 267
  .width-progress {
    width: 200px;
    height: 200px;
    background-color: brown;
  }

  .transform-bgColor {
    transition-property: background-color;
    transition-duration: 0.5s;
  }

268 269
  .transition-width {
    transition-property: width;
张磊 已提交
270
    transition-duration: 1s;
271
  }
272

273 274
  .transition-margin {
    transition-property: margin-left, margin-top;
张磊 已提交
275
    transition-duration: 1s;
276
  }
277

278 279
  .transition-padding {
    transition-property: padding-left, padding-top;
张磊 已提交
280
    transition-duration: 1s;
281
  }
282

283 284
  .transition-background {
    transition-property: background-color, opacity;
张磊 已提交
285
    transition-duration: 1s;
286
  }
287

288 289 290 291 292
  .transition-opacity {
    transition-property: opacity;
    transition-duration: 1s;
  }

293 294
  .transition-transform {
    transition-property: transform;
张磊 已提交
295
    transition-duration: 1s;
296
  }
297

298 299 300 301 302
  .transition-border {
    border-width: 5px;
    border-color: brown;
    border-style: solid;
    transition-property: border-color;
张磊 已提交
303
    transition-duration: 1s;
304
  }
305

306
  .transition-position {
张磊 已提交
307
    left: 0px;
308
    transition-property: left;
张磊 已提交
309
    transition-duration: 1s;
310
  }
311

张磊 已提交
312 313 314 315
  .transition-transform-width {
    transition-property: transform, width;
    transition-duration: 1s;
  }
张磊 已提交
316
</style>