transition.uvue 7.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(含transform-origin)</text>
        <view class="base-style transition-transform" style="transform-origin: 0 0;" id="styleTransformWithOrigin" @click="changeTransformWithOrigin"></view>
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
36 37
      <view class="container">
        <text class="text">点击修改Border</text>
38
        <view class="base-style transition-border" id="styleBorder" @click="changeBorder"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
39 40 41
      </view>
      <view class="container">
        <text class="text">点击修改Position</text>
42
        <view class="base-style transition-position" id="stylePosition" @click="changestylePosition"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
43 44
      </view>
    </view>
45
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
46 47
  </scroll-view>
  <!-- #endif -->
48 49 50
</template>

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

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

<style>
170 171 172 173 174 175 176 177 178
  .container {
    margin: 15rpx;
    background-color: white;
  }

  .text {
    margin-top: 20rpx;
    margin-bottom: 32rpx;
  }
179

180 181 182 183 184
  .base-style {
    width: 400rpx;
    height: 400rpx;
    background-color: brown;
  }
185

186 187
  .transition-width {
    transition-property: width;
188
    transition-duration: 1000ms;
189
  }
190

191 192
  .transition-margin {
    transition-property: margin-left, margin-top;
193
    transition-duration: 1000ms;
194
  }
195

196 197
  .transition-padding {
    transition-property: padding-left, padding-top;
198
    transition-duration: 1000ms;
199
  }
200

201 202
  .transition-background {
    transition-property: background-color, opacity;
203
    transition-duration: 1000ms;
204
  }
205

206 207
  .transition-transform {
    transition-property: transform;
208
    transition-duration: 1000ms;
209
  }
210

211 212 213 214 215
  .transition-border {
    border-width: 5px;
    border-color: brown;
    border-style: solid;
    transition-property: border-color;
216
    transition-duration: 1000ms;
217
  }
218

219 220
  .transition-position {
    transition-property: left;
221
    transition-duration: 1000ms;
222
  }
张磊 已提交
223
</style>