transition.uvue 9.4 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>
张磊 已提交
28 29 30 31
      <view class="container">
        <text class="text">动态修改Background</text>
        <view class="base-style" id="propertyStyleBackground" @click="propertyChangeBackground"></view>
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
32 33
      <view class="container">
        <text class="text">点击修改Transform</text>
34
        <view class="base-style transition-transform" id="styleTransform" @click="changeTransform"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
35
      </view>
张磊 已提交
36 37 38 39
      <view class="container">
        <text class="text">点击修改Transform和宽</text>
        <view class="base-style transition-transform-width" id="styleTransformWithWidth" @click="changeTransformWithWidth"></view>
      </view>
40
      <view class="container" @click="changeTransformWithOrigin">
张磊 已提交
41
        <text class="text">点击修改Transform(含transform-origin)</text>
42
        <view class="base-style transition-transform" style="transform-origin: 0 0;" id="styleTransformWithOrigin"></view>
张磊 已提交
43
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
44 45
      <view class="container">
        <text class="text">点击修改Border</text>
46
        <view class="base-style transition-border" id="styleBorder" @click="changeBorder"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
47 48 49
      </view>
      <view class="container">
        <text class="text">点击修改Position</text>
50
        <view class="base-style transition-position" id="stylePosition" @click="changestylePosition"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
51 52
      </view>
    </view>
53
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
54 55
  </scroll-view>
  <!-- #endif -->
56 57 58
</template>

<script>
59 60 61 62
  export default {
    data() {
      return {
        isTranstionWidthOrHeight: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
63
        widthOrHeight: null as UniElement | null,
64 65
        widthProgress: null as UniElement | null,
        progressWidth: 200,
66
        isTranstionChangeMargin: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
67
        styleMargin: null as UniElement | null,
68
        isTransitionStylePadding: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
69
        stylePadding: null as UniElement | null,
70
        isTransitionstyleBackground: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
71
        styleBackground: null as UniElement | null,
72
        isTransitionStyleTransform: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
73
        styleTransform: null as UniElement | null,
张磊 已提交
74 75
        isTransitionStyleTransformWithWidth:false,
        styleTransformWithWidth: null as UniElement | null,
76
        isTransitionstyleBorder: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
77
        styleBorder: null as UniElement | null,
78
        isTransitionstylePosition: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
79
        stylePosition: null as UniElement | null,
张磊 已提交
80 81
        isSetTransition: false,
        isTransitionpropertystyleBackground: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
82
        propertyStyleBackground: null as UniElement | null,
张磊 已提交
83
        isTransitionStyleTransformWithOrigin: false,
DCloud-yyl's avatar
DCloud-yyl 已提交
84
        styleTransformWithOrigin: null as UniElement | null,
85
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
86
    },
87 88
    onReady() {
      this.widthOrHeight = uni.getElementById("widthOrHeight")
89
      this.widthProgress = uni.getElementById("widthProgress")
90 91 92 93 94 95
      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")
张磊 已提交
96 97
      this.propertyStyleBackground = uni.getElementById("propertyStyleBackground")
      this.styleTransformWithOrigin = uni.getElementById("styleTransformWithOrigin")
张磊 已提交
98
      this.styleTransformWithWidth = uni.getElementById("styleTransformWithWidth")
DCloud-WZF's avatar
DCloud-WZF 已提交
99
    },
100 101 102
    methods: {
      changeWidthOrHeight() {
        this.widthOrHeight?.style?.setProperty("width", this.isTranstionWidthOrHeight
张磊 已提交
103
          ? '200px'
104
          : '300px')
105 106
        this.isTranstionWidthOrHeight = !this.isTranstionWidthOrHeight
      },
107 108 109 110
      changeWidthProgress() {
        this.progressWidth += 20
        this.widthProgress?.style?.setProperty("width", this.progressWidth + 'px')
      },
111 112
      changeMargin() {
        this.styleMargin?.style?.setProperty("margin-top", this.isTranstionChangeMargin
113 114
          ? '0px'
          : '50px'
115 116
        )
        this.styleMargin?.style?.setProperty("margin-left", this.isTranstionChangeMargin
117 118
          ? '0px'
          : '50px'
119 120 121
        )
        this.isTranstionChangeMargin = !this.isTranstionChangeMargin
      },
122

123 124
      changePadding() {
        this.stylePadding?.style?.setProperty("padding-top", this.isTransitionStylePadding
125 126
          ? '0px'
          : '50px')
127
        this.stylePadding?.style?.setProperty("padding-left", this.isTransitionStylePadding
128 129
          ? '0px'
          : '50px')
130 131 132 133 134 135 136 137 138 139 140 141 142
        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
      },
张磊 已提交
143 144 145
      propertyChangeBackground() {
        if (!this.isSetTransition) {
          this.propertyStyleBackground?.style?.setProperty("transition-property", "background-color")
张磊 已提交
146
          this.propertyStyleBackground?.style?.setProperty("transition-duration", "1000ms")
张磊 已提交
147 148 149 150 151 152 153 154
          this.isSetTransition = true
        }
        this.propertyStyleBackground?.style?.setProperty("background-color", this.isTransitionpropertystyleBackground
          ? 'brown'
          : 'black'
        )
        this.isTransitionpropertystyleBackground = !this.isTransitionpropertystyleBackground
      },
155 156 157 158 159 160 161
      changeTransform() {
        this.styleTransform?.style?.setProperty("transform", this.isTransitionStyleTransform
          ? 'rotate(0deg)'
          : 'rotate(135deg)'
        )
        this.isTransitionStyleTransform = !this.isTransitionStyleTransform
      },
张磊 已提交
162 163 164 165 166 167 168 169 170 171 172
      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
      },
张磊 已提交
173 174 175 176 177 178 179
      changeTransformWithOrigin() {
        this.styleTransformWithOrigin?.style?.setProperty("transform", this.isTransitionStyleTransformWithOrigin
          ? 'scaleX(1)'
          : 'scaleX(0)'
        )
        this.isTransitionStyleTransformWithOrigin = !this.isTransitionStyleTransformWithOrigin
      },
180 181 182 183 184 185 186 187 188
      changeBorder() {
        this.styleBorder?.style?.setProperty("border-color", this.isTransitionstyleBorder
          ? 'brown'
          : 'yellow'
        )
        this.isTransitionstyleBorder = !this.isTransitionstyleBorder
      },
      changestylePosition() {
        this.stylePosition?.style?.setProperty("left", this.isTransitionstylePosition
189 190
          ? '0px'
          : '100px'
191 192 193
        )
        this.isTransitionstylePosition = !this.isTransitionstylePosition
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
194
    },
195
  }
196 197 198
</script>

<style>
199
  .container {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
200
    margin: 7px;
201 202 203 204
    background-color: white;
  }

  .text {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
205 206
    margin-top: 10px;
    margin-bottom: 16px;
207
  }
208

209
  .base-style {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
210 211
    width: 200px;
    height: 200px;
212 213
    background-color: brown;
  }
214

215 216 217 218 219 220 221 222 223 224 225
  .width-progress {
    width: 200px;
    height: 200px;
    background-color: brown;
  }

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

226 227
  .transition-width {
    transition-property: width;
张磊 已提交
228
    transition-duration: 1s;
229
  }
230

231 232
  .transition-margin {
    transition-property: margin-left, margin-top;
张磊 已提交
233
    transition-duration: 1s;
234
  }
235

236 237
  .transition-padding {
    transition-property: padding-left, padding-top;
张磊 已提交
238
    transition-duration: 1s;
239
  }
240

241 242
  .transition-background {
    transition-property: background-color, opacity;
张磊 已提交
243
    transition-duration: 1s;
244
  }
245

246 247
  .transition-transform {
    transition-property: transform;
张磊 已提交
248
    transition-duration: 1s;
249
  }
250

251 252 253 254 255
  .transition-border {
    border-width: 5px;
    border-color: brown;
    border-style: solid;
    transition-property: border-color;
张磊 已提交
256
    transition-duration: 1s;
257
  }
258

259
  .transition-position {
张磊 已提交
260
    left: 0px;
261
    transition-property: left;
张磊 已提交
262
    transition-duration: 1s;
263
  }
张磊 已提交
264 265 266 267
  .transition-transform-width {
    transition-property: transform, width;
    transition-duration: 1s;
  }
张磊 已提交
268
</style>