123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <template>
- <!-- #ifdef APP -->
- <scroll-view style="flex: 1">
- <!-- #endif -->
- <view class="container">
- <text class="text">点击修改宽度</text>
- <view class="base-style transition-width" id="widthOrHeight" @click="changeWidthOrHeight"></view>
- </view>
- <view class="container">
- <text class="text">点击修改宽度(递增)</text>
- <view class="width-progress transition-width" id="widthProgress" @click="changeWidthProgress"></view>
- </view>
- <view class="container">
- <text class="text">点击修改宽度(通过style设置transition)</text>
- <view class="base-style" style="transition-property: width; transition-duration: 1s;" id="widthOrHeightStyle"
- @click="changeWidthOrHeightStyle"></view>
- </view>
- <view class="container">
- <text class="text">点击修改Margin</text>
- <view class="base-style transition-margin" id="styleMargin" @click="changeMargin"></view>
- </view>
- <view class="container">
- <text class="text">点击修改Padding</text>
- <view class="base-style transition-padding" id="stylePadding" @click="changePadding">
- <view style="background-color: black; height: 50px; width: 50px"></view>
- </view>
- </view>
- <view class="container">
- <text class="text">点击修改background-color和opacity</text>
- <view class="base-style transition-background" id="styleBackground" @click="changeBackground"></view>
- </view>
- <view class="container">
- <text class="text">点击修改background-color(rgba)</text>
- <view style="flex: 1;">
- <view class="base-style transition-background" id="styleBackground2" @click="changeBackground2"></view>
- </view>
- </view>
- <view class="container">
- <text class="text">点击修改opacity渐隐渐现</text>
- <view class="base-style transition-opacity" id="styleOpacity" @click="changeStyleOpacity"></view>
- </view>
- <view class="container">
- <text class="text">动态修改background-color和duration</text>
- <view class="base-style" id="propertyStyleBackground" @click="propertyChangeBackground"></view>
- </view>
- <view class="container">
- <text class="text">点击修改Transform</text>
- <view class="base-style transition-transform" id="styleTransform" @click="changeTransform"></view>
- </view>
- <view class="container">
- <text class="text">点击修改TransformTranslate</text>
- <view class="base-style transition-transform" id="transformTranslate" @click="changeTransformTranslate"></view>
- </view>
- <view class="container">
- <text class="text">点击修改Transform和宽</text>
- <view class="base-style transition-transform-width" id="styleTransformWithWidth"
- @click="changeTransformWithWidth"></view>
- </view>
- <view class="container" @click="changeTransformWithOrigin">
- <text class="text">点击修改Transform(含transform-origin)</text>
- <view class="base-style transition-transform" style="transform-origin: 0 0;" id="styleTransformWithOrigin"></view>
- </view>
- <view class="container">
- <text class="text">点击修改Border</text>
- <view class="base-style transition-border" id="styleBorder" @click="changeBorder"></view>
- </view>
- <view class="container">
- <text class="text">点击修改Position</text>
- <view class="base-style transition-position" id="stylePosition" @click="changestylePosition"></view>
- </view>
- <view class="container">
- <text class="text">点击修改Scale
- </text>
- <view class="base-style transition-transform" id="styleChangScale" @click="changestyleScale"></view>
- </view>
- <view class="container">
- <text class="text">仅指定transition-duration背影有动画为正常</text>
- <view style="width: 100%;height: 50px;" :style="changestyleTransitionDuration"
- @click="doChangeTransitionDuration">
- </view>
- </view>
- <view class="container">
- <text class="text">transition-property: all/none 演示</text>
- <view class="base-style transition-all" id="styleTransitionAll" @click="changeTransitionAll"></view>
- </view>
- <view class="container">
- <text class="text">在组件内滑动测试是否闪动</text>
- <view class="base-style transition-transform" id="styleTransformTranslateScale" @touchmove="handleTouchMove"
- @touchstart="handleTouchStart" @touchend="handleTouchEnd">
- 这是一段文字
- </view>
- </view>
- <view class="container">
- <text class="text">测试在style中同时修改transform和width</text>
- <view style="width:100%;height: 100%;">
- <view class="base-style transition-transform-width" id="styleTransformTranslateScale"
- :style="{transform:`translateX(${lineLeft}px)`,width:`${lineWidth}px`}" @click="changeTransform61">
- </view>
- </view>
- </view>
- <!-- #ifndef APP-ANDROID -->
- <view style="margin: 7px; height: 60px;">
- <navigator url="/pages/CSS/transition/transition-transform" hover-class="none">
- <button type="primary">
- transform 多个属性示例
- </button>
- </navigator>
- </view>
- <!-- #endif -->
- <!-- #ifdef APP -->
- </scroll-view>
- <!-- #endif -->
- </template>
- <script>
- export default {
- data() {
- return {
- isTranstionWidthOrHeight: false,
- widthOrHeight: null as UniElement | null,
- widthProgress: null as UniElement | null,
- isTranstionWidthOrHeightStyle: false,
- widthOrHeightStyle: null as UniElement | null,
- progressWidth: 200,
- isTranstionChangeMargin: false,
- styleMargin: null as UniElement | null,
- isTransitionStylePadding: false,
- stylePadding: null as UniElement | null,
- isTransitionstyleBackground: false,
- isTransitionstyleBackground2: false,
- isTransitionstyleOpacity: false,
- styleBackground: null as UniElement | null,
- styleBackground2: null as UniElement | null,
- styleOpacity: null as UniElement | null,
- isTransitionStyleTransform: false,
- styleTransform: null as UniElement | null,
- isTransitionStyleTransformWithWidth: false,
- styleTransformWithWidth: null as UniElement | null,
- isTransitionstyleBorder: false,
- styleBorder: null as UniElement | null,
- isTransitionstylePosition: false,
- stylePosition: null as UniElement | null,
- isSetTransition: false,
- isTransitionpropertystyleBackground: false,
- propertyStyleBackground: null as UniElement | null,
- isTransitionStyleTransformWithOrigin: false,
- styleTransformWithOrigin: null as UniElement | null,
- styleTransformTranslate: null as UniElement | null,
- isTransformTranslate: false,
- styleTransformTranslateScale: null as UniElement | null,
- startX: 0,
- moveX: 0,
- oldX: 0,
- changestyleTransitionDuration: 'background-color:brown;',
- styleTransitionAll: null as UniElement | null,
- isTransitionAll: false,
- lineLeft: 0,
- lineWidth: 200,
- }
- },
- onReady() {
- this.widthOrHeight = uni.getElementById("widthOrHeight")
- this.widthProgress = uni.getElementById("widthProgress")
- this.widthOrHeightStyle = uni.getElementById("widthOrHeightStyle")
- this.styleMargin = uni.getElementById("styleMargin")
- this.stylePadding = uni.getElementById("stylePadding")
- this.styleBackground = uni.getElementById("styleBackground")
- this.styleBackground2 = uni.getElementById("styleBackground2")
- this.styleOpacity = uni.getElementById("styleOpacity")
- this.styleTransform = uni.getElementById("styleTransform")
- this.styleBorder = uni.getElementById("styleBorder")
- this.stylePosition = uni.getElementById("stylePosition")
- this.propertyStyleBackground = uni.getElementById("propertyStyleBackground")
- this.styleTransformWithOrigin = uni.getElementById("styleTransformWithOrigin")
- this.styleTransformWithWidth = uni.getElementById("styleTransformWithWidth")
- this.styleTransformTranslate = uni.getElementById("transformTranslate")
- this.styleTransformTranslateScale = uni.getElementById("styleTransformTranslateScale")
- this.styleTransitionAll = uni.getElementById("styleTransitionAll")
- },
- methods: {
- changeWidthOrHeight() {
- this.widthOrHeight?.style?.setProperty("width", this.isTranstionWidthOrHeight
- ? '200px'
- : '300px')
- this.isTranstionWidthOrHeight = !this.isTranstionWidthOrHeight
- },
- changeWidthProgress() {
- this.progressWidth += 20
- this.widthProgress?.style?.setProperty("width", this.progressWidth + 'px')
- },
- changeWidthOrHeightStyle() {
- this.widthOrHeightStyle?.style?.setProperty("width", this.isTranstionWidthOrHeightStyle
- ? '200px'
- : '300px')
- this.isTranstionWidthOrHeightStyle = !this.isTranstionWidthOrHeightStyle
- },
- changeMargin() {
- this.styleMargin?.style?.setProperty("margin-top", this.isTranstionChangeMargin
- ? '0px'
- : '50px'
- )
- this.styleMargin?.style?.setProperty("margin-left", this.isTranstionChangeMargin
- ? '0px'
- : '50px'
- )
- this.isTranstionChangeMargin = !this.isTranstionChangeMargin
- },
- changePadding() {
- this.stylePadding?.style?.setProperty("padding-top", this.isTransitionStylePadding
- ? '0px'
- : '50px')
- this.stylePadding?.style?.setProperty("padding-left", this.isTransitionStylePadding
- ? '0px'
- : '50px')
- 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
- },
- changeBackground2() {
- this.styleBackground2?.style?.setProperty("background-color", this.isTransitionstyleBackground2
- ? 'brown'
- : 'rgba(0, 0, 0, 0.5)'
- )
- this.isTransitionstyleBackground2 = !this.isTransitionstyleBackground2
- },
- changeStyleOpacity() {
- this.styleOpacity?.style?.setProperty("opacity", this.isTransitionstyleOpacity
- ? '1'
- : '0'
- )
- this.isTransitionstyleOpacity = !this.isTransitionstyleOpacity
- },
- propertyChangeBackground() {
- if (!this.isSetTransition) {
- this.propertyStyleBackground?.style?.setProperty("transition-property", "background-color")
- this.propertyStyleBackground?.style?.setProperty("transition-duration", "1000ms")
- this.isSetTransition = true
- }
- this.propertyStyleBackground?.style?.setProperty("background-color", this.isTransitionpropertystyleBackground
- ? 'brown'
- : 'black'
- )
- this.isTransitionpropertystyleBackground = !this.isTransitionpropertystyleBackground
- },
- changeTransform() {
- this.styleTransform?.style?.setProperty("transform", this.isTransitionStyleTransform
- ? 'rotate(0deg)'
- : 'rotate(135deg)'
- )
- this.isTransitionStyleTransform = !this.isTransitionStyleTransform
- },
- changeTransformTranslate() {
- const translate = this.isTransformTranslate ? `translate(0%,0%)` : `translate(100%,0%)`
- this.styleTransformTranslate?.style?.setProperty('transform', translate)
- this.isTransformTranslate = !this.isTransformTranslate
- },
- 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
- },
- changeTransformWithOrigin() {
- this.styleTransformWithOrigin?.style?.setProperty("transform", this.isTransitionStyleTransformWithOrigin
- ? 'scaleX(1)'
- : 'scaleX(0)'
- )
- this.isTransitionStyleTransformWithOrigin = !this.isTransitionStyleTransformWithOrigin
- },
- changeBorder() {
- this.styleBorder?.style?.setProperty("border-color", this.isTransitionstyleBorder
- ? 'brown'
- : 'yellow'
- )
- this.isTransitionstyleBorder = !this.isTransitionstyleBorder
- },
- changestylePosition() {
- this.stylePosition?.style?.setProperty("left", this.isTransitionstylePosition
- ? '0px'
- : '100px'
- )
- this.isTransitionstylePosition = !this.isTransitionstylePosition
- },
- changestyleScale() {
- let el = uni.getElementById("styleChangScale")
- el?.style.setProperty("transition-duration", "0ms")
- el?.style.setProperty("transform", "translate(10px,10px) scale(1)")
- setTimeout(() => {
- el?.style.setProperty("transition-duration", "200ms")
- el?.style.setProperty("transform", "translate(10px,10px) scale(0.5)")
- }, 200)
- },
- handleTouchStart(e : UniTouchEvent) {
- this.startX = e.changedTouches[0].clientX
- },
- handleTouchMove(e : UniTouchEvent) {
- console.log("touchmove:" + e.touches[0].clientX + "," + e.touches[0].clientY);
- e.preventDefault()
- e.stopPropagation()
- const difX = e.changedTouches[0].clientX
- this.moveX = difX - this.startX + this.oldX
- this.styleTransformTranslateScale?.style?.setProperty('transition-duration', '0ms')
- this.styleTransformTranslateScale?.style?.setProperty('transform', `translate(${this.moveX}px,0px) scale(0.5) rotate(90deg)`)
- },
- handleTouchEnd(_ : UniTouchEvent) {
- this.oldX = this.moveX
- },
- doChangeTransitionDuration() {
- console.log("doChangeTransitionDuration")
- this.changestyleTransitionDuration
- = this.changestyleTransitionDuration === 'background-color:brown;' ? 'transition-duration: 2000ms;background-color: blue;' : 'background-color:brown;'
- },
- changeTransitionAll() {
- if (!this.isTransitionAll) {
- // 设置为 all 时的效果
- this.styleTransitionAll?.style?.setProperty("transition-property", "all")
- this.styleTransitionAll?.style?.setProperty("width", "300px")
- this.styleTransitionAll?.style?.setProperty("height", "300px")
- this.styleTransitionAll?.style?.setProperty("background-color", "blue")
- this.styleTransitionAll?.style?.setProperty("transform", "rotate(45deg)")
- } else {
- // 设置为 none 时的效果
- this.styleTransitionAll?.style?.setProperty("transition-property", "none")
- this.styleTransitionAll?.style?.setProperty("width", "200px")
- this.styleTransitionAll?.style?.setProperty("height", "200px")
- this.styleTransitionAll?.style?.setProperty("background-color", "brown")
- this.styleTransitionAll?.style?.setProperty("transform", "rotate(0deg)")
- }
- this.isTransitionAll = !this.isTransitionAll
- },
- changeTransform61() {
- if (this.lineLeft == 0) {
- this.lineLeft = 70
- this.lineWidth = 300
- } else {
- this.lineLeft = 0
- this.lineWidth = 200
- }
- },
- },
- }
- </script>
- <style>
- .container {
- margin: 7px;
- background-color: white;
- }
- .text {
- margin-top: 10px;
- margin-bottom: 16px;
- }
- .base-style {
- width: 200px;
- height: 200px;
- background-color: brown;
- }
- .width-progress {
- width: 200px;
- height: 200px;
- background-color: brown;
- }
- .transform-bgColor {
- transition-property: background-color;
- transition-duration: 0.5s;
- }
- .transition-width {
- transition-property: width;
- transition-duration: 1s;
- }
- .transition-margin {
- transition-property: margin-left, margin-top;
- transition-duration: 1s;
- }
- .transition-padding {
- transition-property: padding-left, padding-top;
- transition-duration: 1s;
- }
- .transition-background {
- transition-property: background-color, opacity;
- transition-duration: 1s;
- }
- .transition-opacity {
- transition-property: opacity;
- transition-duration: 1s;
- }
- .transition-transform {
- transform: translate(0%, 0%) scaleX(1) rotate(0deg);
- transition-property: transform;
- transition-duration: 1s;
- }
- .transition-border {
- border-width: 5px;
- border-color: brown;
- border-style: solid;
- transition-property: border-color;
- transition-duration: 1s;
- }
- .transition-position {
- left: 0px;
- transition-property: left;
- transition-duration: 1s;
- }
- .transition-transform-width {
- transform: rotate(0deg);
- transition-property: transform, width;
- transition-duration: 1s;
- }
- .transition-all {
- transition-property: all;
- transition-duration: 1s;
- }
- </style>
|