transition.uvue 10.0 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 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">点击修改Transform和宽</text>
        <view class="base-style transition-transform-width" id="styleTransformWithWidth" @click="changeTransformWithWidth"></view>
      </view>
46
      <view class="container" @click="changeTransformWithOrigin">
张磊 已提交
47
        <text class="text">点击修改Transform(含transform-origin)</text>
48
        <view class="base-style transition-transform" style="transform-origin: 0 0;" id="styleTransformWithOrigin"></view>
张磊 已提交
49
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
50 51
      <view class="container">
        <text class="text">点击修改Border</text>
52
        <view class="base-style transition-border" id="styleBorder" @click="changeBorder"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
53 54 55
      </view>
      <view class="container">
        <text class="text">点击修改Position</text>
56
        <view class="base-style transition-position" id="stylePosition" @click="changestylePosition"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
57 58
      </view>
    </view>
59
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
60 61
  </scroll-view>
  <!-- #endif -->
62 63 64
</template>

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

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

<style>
214
  .container {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
215
    margin: 7px;
216 217 218 219
    background-color: white;
  }

  .text {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
220 221
    margin-top: 10px;
    margin-bottom: 16px;
222
  }
223

224
  .base-style {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
225 226
    width: 200px;
    height: 200px;
227
    background-color: brown;
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
228 229 230 231 232 233
  }

  .width-progress {
    width: 200px;
    height: 200px;
    background-color: brown;
234
  }
235

DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
236 237 238 239 240
  .transform-bgColor {
    transition-property: background-color;
    transition-duration: 0.5s;
  }

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

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

251 252
  .transition-padding {
    transition-property: padding-left, padding-top;
张磊 已提交
253
    transition-duration: 1s;
254
  }
255

256 257
  .transition-background {
    transition-property: background-color, opacity;
张磊 已提交
258
    transition-duration: 1s;
259
  }
260

261 262
  .transition-transform {
    transition-property: transform;
张磊 已提交
263
    transition-duration: 1s;
264
  }
265

266 267 268 269 270
  .transition-border {
    border-width: 5px;
    border-color: brown;
    border-style: solid;
    transition-property: border-color;
张磊 已提交
271
    transition-duration: 1s;
272
  }
273

274
  .transition-position {
张磊 已提交
275
    left: 0px;
276
    transition-property: left;
张磊 已提交
277
    transition-duration: 1s;
278
  }
张磊 已提交
279 280 281 282
  .transition-transform-width {
    transition-property: transform, width;
    transition-duration: 1s;
  }
张磊 已提交
283
</style>