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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    methods: {
      changeWidthOrHeight() {
        this.widthOrHeight?.style?.setProperty("width", this.isTranstionWidthOrHeight
          ? '400rpx'
          : '600rpx')
        this.isTranstionWidthOrHeight = !this.isTranstionWidthOrHeight
      },
      changeMargin() {
        this.styleMargin?.style?.setProperty("margin-top", this.isTranstionChangeMargin
          ? '0rpx'
          : '100rpx'
        )
        this.styleMargin?.style?.setProperty("margin-left", this.isTranstionChangeMargin
          ? '0rpx'
          : '100rpx'
        )
        this.isTranstionChangeMargin = !this.isTranstionChangeMargin
      },
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
      changePadding() {
        this.stylePadding?.style?.setProperty("padding-top", this.isTransitionStylePadding
          ? '0rpx'
          : '100rpx')
        this.stylePadding?.style?.setProperty("padding-left", this.isTransitionStylePadding
          ? '0rpx'
          : '100rpx')
        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 128 129 130 131 132 133 134 135 136
      propertyChangeBackground() {
        if (!this.isSetTransition) {
          this.propertyStyleBackground?.style?.setProperty("transition-property", "background-color")
          this.propertyStyleBackground?.style?.setProperty("transition-duration", "1000")
          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 160 161 162 163 164
      changeBorder() {
        this.styleBorder?.style?.setProperty("border-color", this.isTransitionstyleBorder
          ? 'brown'
          : 'yellow'
        )
        this.isTransitionstyleBorder = !this.isTransitionstyleBorder
      },
      changestylePosition() {
        this.stylePosition?.style?.setProperty("left", this.isTransitionstylePosition
          ? '0rpx'
          : '150rpx'
        )
        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 188 189
  .transition-width {
    transition-property: width;
    transition-duration: 1000;
  }
190

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

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

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

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

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

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