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

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

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

<style>
203
  .container {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
204
    margin: 7px;
205 206 207 208
    background-color: white;
  }

  .text {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
209 210
    margin-top: 10px;
    margin-bottom: 16px;
211
  }
212

213
  .base-style {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
214 215
    width: 200px;
    height: 200px;
216 217
    background-color: brown;
  }
218

DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
219 220 221 222 223
  .transform-bgColor {
    transition-property: background-color;
    transition-duration: 0.5s;
  }

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

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

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

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

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

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

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