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

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

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

<style>
188
  .container {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
189
    margin: 7px;
190 191 192 193
    background-color: white;
  }

  .text {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
194 195
    margin-top: 10px;
    margin-bottom: 16px;
196
  }
197

198
  .base-style {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
199 200
    width: 200px;
    height: 200px;
201 202
    background-color: brown;
  }
203

204 205
  .transition-width {
    transition-property: width;
张磊 已提交
206
    transition-duration: 1s;
207
  }
208

209 210
  .transition-margin {
    transition-property: margin-left, margin-top;
张磊 已提交
211
    transition-duration: 1s;
212
  }
213

214 215
  .transition-padding {
    transition-property: padding-left, padding-top;
张磊 已提交
216
    transition-duration: 1s;
217
  }
218

219 220
  .transition-background {
    transition-property: background-color, opacity;
张磊 已提交
221
    transition-duration: 1s;
222
  }
223

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

229 230 231 232 233
  .transition-border {
    border-width: 5px;
    border-color: brown;
    border-style: solid;
    transition-property: border-color;
张磊 已提交
234
    transition-duration: 1s;
235
  }
236

237
  .transition-position {
张磊 已提交
238
    left: 0px;
239
    transition-property: left;
张磊 已提交
240
    transition-duration: 1s;
241
  }
张磊 已提交
242 243 244 245
  .transition-transform-width {
    transition-property: transform, width;
    transition-duration: 1s;
  }
张磊 已提交
246
</style>