transition.uvue 10.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
      </view>
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 25
        </view>
      </view>
      <view class="container">
        <text class="text">点击修改Background</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 29 30 31 32 33
      <view class="container">
        <text class="text">点击修改Background(颜色带透明度)</text>
        <view style="flex: 1;">
          <view class="base-style transition-background" id="styleBackground2" @click="changeBackground2"></view>
        </view>
      </view>
张磊 已提交
34 35 36 37
      <view class="container">
        <text class="text">动态修改Background</text>
        <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,
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,
DCloud-yyl's avatar
DCloud-yyl 已提交
81
        styleBackground: null as UniElement | null,
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
82
        styleBackground2: null as UniElement | null,
83
        isTransitionStyleTransform: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
84
        styleTransform: null as UniElement | null,
张磊 已提交
85 86
        isTransitionStyleTransformWithWidth:false,
        styleTransformWithWidth: null as UniElement | null,
87
        isTransitionstyleBorder: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
88
        styleBorder: null as UniElement | null,
89
        isTransitionstylePosition: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
90
        stylePosition: null as UniElement | null,
张磊 已提交
91 92
        isSetTransition: false,
        isTransitionpropertystyleBackground: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
93
        propertyStyleBackground: null as UniElement | null,
张磊 已提交
94
        isTransitionStyleTransformWithOrigin: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
95
        styleTransformWithOrigin: null as UniElement | null,
96 97
        styleTransformTranslate: null as UniElement | null,
        isTransformTranslate: false
98
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
99
    },
100 101
    onReady() {
      this.widthOrHeight = uni.getElementById("widthOrHeight")
102
      this.widthProgress = uni.getElementById("widthProgress")
103 104 105
      this.styleMargin = uni.getElementById("styleMargin")
      this.stylePadding = uni.getElementById("stylePadding")
      this.styleBackground = uni.getElementById("styleBackground")
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
106
      this.styleBackground2 = uni.getElementById("styleBackground2")
107 108 109
      this.styleTransform = uni.getElementById("styleTransform")
      this.styleBorder = uni.getElementById("styleBorder")
      this.stylePosition = uni.getElementById("stylePosition")
张磊 已提交
110 111
      this.propertyStyleBackground = uni.getElementById("propertyStyleBackground")
      this.styleTransformWithOrigin = uni.getElementById("styleTransformWithOrigin")
张磊 已提交
112
      this.styleTransformWithWidth = uni.getElementById("styleTransformWithWidth")
113
      this.styleTransformTranslate = uni.getElementById("transformTranslate")
DCloud-WZF's avatar
DCloud-WZF 已提交
114
    },
115 116 117
    methods: {
      changeWidthOrHeight() {
        this.widthOrHeight?.style?.setProperty("width", this.isTranstionWidthOrHeight
张磊 已提交
118
          ? '200px'
119
          : '300px')
120 121
        this.isTranstionWidthOrHeight = !this.isTranstionWidthOrHeight
      },
122 123 124 125
      changeWidthProgress() {
        this.progressWidth += 20
        this.widthProgress?.style?.setProperty("width", this.progressWidth + 'px')
      },
126 127
      changeMargin() {
        this.styleMargin?.style?.setProperty("margin-top", this.isTranstionChangeMargin
128 129
          ? '0px'
          : '50px'
130 131
        )
        this.styleMargin?.style?.setProperty("margin-left", this.isTranstionChangeMargin
132 133
          ? '0px'
          : '50px'
134 135 136
        )
        this.isTranstionChangeMargin = !this.isTranstionChangeMargin
      },
137

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

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

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

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

242 243 244 245 246 247 248 249 250 251 252
  .width-progress {
    width: 200px;
    height: 200px;
    background-color: brown;
  }

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

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

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

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

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

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

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

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