提交 a84f81bf 编写于 作者: 张磊

更新transition示例使用uni.getElementById操作style

上级 58824d38
<template> <template>
<!-- #ifdef APP --> <!-- #ifdef APP -->
<scroll-view style="flex: 1"> <scroll-view style="flex: 1">
<!-- #endif --> <!-- #endif -->
<view> <view>
<view class="container"> <view class="container">
<text class="text">点击修改宽度</text> <text class="text">点击修改宽度</text>
<view <view class="base-style transition-width" id="widthOrHeight" @click="changeWidthOrHeight"></view>
class="base-style transition-width"
:style="widthOrHeight"
@click="changeWidthOrHeight"
></view>
</view> </view>
<view class="container"> <view class="container">
<text class="text">点击修改Margin</text> <text class="text">点击修改Margin</text>
<view <view class="base-style transition-margin" id="styleMargin" @click="changeMargin"></view>
class="base-style transition-margin"
:style="styleMargin"
@click="changeMargin"
></view>
</view> </view>
<view class="container"> <view class="container">
<text class="text">点击修改Padding</text> <text class="text">点击修改Padding</text>
<view <view class="base-style transition-padding" id="stylePadding" @click="changePadding">
class="base-style transition-padding" <view style="background-color: black; height: 50px; width: 50px"></view>
:style="stylePadding"
@click="changePadding"
>
<view
style="background-color: black; height: 50px; width: 50px"
></view>
</view> </view>
</view> </view>
<view class="container"> <view class="container">
<text class="text">点击修改Background</text> <text class="text">点击修改Background</text>
<view <view class="base-style transition-background" id="styleBackground" @click="changeBackground"></view>
class="base-style transition-background"
:style="styleBackground"
@click="changeBackground"
></view>
</view> </view>
<view class="container"> <view class="container">
<text class="text">点击修改Transform</text> <text class="text">点击修改Transform</text>
<view <view class="base-style transition-transform" id="styleTransform" @click="changeTransform"></view>
class="base-style transition-transform"
:style="styleTransform"
@click="changeTransform"
></view>
</view> </view>
<view class="container"> <view class="container">
<text class="text">点击修改Border</text> <text class="text">点击修改Border</text>
<view <view class="base-style transition-border" id="styleBorder" @click="changeBorder"></view>
class="base-style transition-border"
:style="styleBorder"
@click="changeBorder"
></view>
</view> </view>
<view class="container"> <view class="container">
<text class="text">点击修改Position</text> <text class="text">点击修改Position</text>
<view <view class="base-style transition-position" id="stylePosition" @click="changestylePosition"></view>
class="base-style transition-position"
:style="stylePosition"
@click="changestylePosition"
></view>
</view> </view>
</view> </view>
<!-- #ifdef APP --> <!-- #ifdef APP -->
</scroll-view> </scroll-view>
<!-- #endif --> <!-- #endif -->
</template> </template>
<script> <script>
export default { export default {
data() { data() {
return { return {
isTranstionWidthOrHeight: false, isTranstionWidthOrHeight: false,
widthOrHeight: '', widthOrHeight: null as Element | null,
isTranstionChangeMargin: false, isTranstionChangeMargin: false,
styleMargin: '', styleMargin: null as Element | null,
isTransitionStylePadding: false, isTransitionStylePadding: false,
stylePadding: '', stylePadding: null as Element | null,
isTransitionstyleBackground: false, isTransitionstyleBackground: false,
styleBackground: '', styleBackground: null as Element | null,
isTransitionStyleTransform: false, isTransitionStyleTransform: false,
styleTransform: '', styleTransform: null as Element | null,
isTransitionstyleBorder: false, isTransitionstyleBorder: false,
styleBorder: '', styleBorder: null as Element | null,
isTransitionstylePosition: false, isTransitionstylePosition: false,
stylePosition: '', stylePosition: null as Element | null,
} }
},
methods: {
changeWidthOrHeight() {
this.widthOrHeight = this.isTranstionWidthOrHeight
? 'width:400rpx'
: 'width:600rpx'
this.isTranstionWidthOrHeight = !this.isTranstionWidthOrHeight
}, },
changeMargin() { onReady() {
this.styleMargin = this.isTranstionChangeMargin this.widthOrHeight = uni.getElementById("widthOrHeight")
? 'margin-top:0rpx;margin-left:0rpx' this.styleMargin = uni.getElementById("styleMargin")
: 'margin-top:100rpx;margin-left:100rpx' this.stylePadding = uni.getElementById("stylePadding")
this.isTranstionChangeMargin = !this.isTranstionChangeMargin this.styleBackground = uni.getElementById("styleBackground")
this.styleTransform = uni.getElementById("styleTransform")
this.styleBorder = uni.getElementById("styleBorder")
this.stylePosition = uni.getElementById("stylePosition")
}, },
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
},
changePadding() { changePadding() {
this.stylePadding = this.isTransitionStylePadding this.stylePadding?.style?.setProperty("padding-top", this.isTransitionStylePadding
? 'padding-top:0rpx;padding-left:0rpx' ? '0rpx'
: 'padding-top:100rpx;padding-left:100rpx' : '100rpx')
this.isTransitionStylePadding = !this.isTransitionStylePadding this.stylePadding?.style?.setProperty("padding-left", this.isTransitionStylePadding
}, ? '0rpx'
changeBackground() { : '100rpx')
this.styleBackground = this.isTransitionstyleBackground this.isTransitionStylePadding = !this.isTransitionStylePadding
? 'background-color: brown;opacity:1' },
: 'background-color: black;opacity:0.5' changeBackground() {
this.isTransitionstyleBackground = !this.isTransitionstyleBackground this.styleBackground?.style?.setProperty("background-color", this.isTransitionstyleBackground
}, ? 'brown'
changeTransform() { : 'black'
this.styleTransform = this.isTransitionStyleTransform )
? 'transform: rotate(0deg)' this.styleBackground?.style?.setProperty("opacity", this.isTransitionstyleBackground
: 'transform: rotate(135deg)' ? '1'
this.isTransitionStyleTransform = !this.isTransitionStyleTransform : '0.5'
)
this.isTransitionstyleBackground = !this.isTransitionstyleBackground
},
changeTransform() {
this.styleTransform?.style?.setProperty("transform", this.isTransitionStyleTransform
? 'rotate(0deg)'
: 'rotate(135deg)'
)
this.isTransitionStyleTransform = !this.isTransitionStyleTransform
},
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
},
}, },
changeBorder() { }
this.styleBorder = this.isTransitionstyleBorder
? 'border-color: brown;'
: 'border-color: yellow;'
this.isTransitionstyleBorder = !this.isTransitionstyleBorder
},
changestylePosition() {
this.stylePosition = this.isTransitionstylePosition
? 'left:0rpx'
: 'left:150rpx;'
this.isTransitionstylePosition = !this.isTransitionstylePosition
},
},
}
</script> </script>
<style> <style>
.container { .container {
margin: 15rpx; margin: 15rpx;
background-color: white; background-color: white;
} }
.text {
margin-top: 20rpx; .text {
margin-bottom: 32rpx; margin-top: 20rpx;
} margin-bottom: 32rpx;
}
.base-style { .base-style {
width: 400rpx; width: 400rpx;
height: 400rpx; height: 400rpx;
background-color: brown; background-color: brown;
} }
.transition-width { .transition-width {
transition-property: width; transition-property: width;
transition-duration: 1000; transition-duration: 1000;
} }
.transition-margin { .transition-margin {
transition-property: margin-left, margin-top; transition-property: margin-left, margin-top;
transition-duration: 1000; transition-duration: 1000;
} }
.transition-padding { .transition-padding {
transition-property: padding-left, padding-top; transition-property: padding-left, padding-top;
transition-duration: 1000; transition-duration: 1000;
} }
.transition-background { .transition-background {
transition-property: background-color, opacity; transition-property: background-color, opacity;
transition-duration: 1000; transition-duration: 1000;
} }
.transition-transform { .transition-transform {
transition-property: transform; transition-property: transform;
transition-duration: 1000; transition-duration: 1000;
} }
.transition-border { .transition-border {
border-width: 5px; border-width: 5px;
border-color: brown; border-color: brown;
border-style: solid; border-style: solid;
transition-property: border-color; transition-property: border-color;
transition-duration: 1000; transition-duration: 1000;
} }
.transition-position { .transition-position {
transition-property: left; transition-property: left;
transition-duration: 1000; transition-duration: 1000;
} }
</style> </style>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册