transition.uvue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex: 1">
  4. <!-- #endif -->
  5. <view class="container">
  6. <text class="text">点击修改宽度</text>
  7. <view class="base-style transition-width" id="widthOrHeight" @click="changeWidthOrHeight"></view>
  8. </view>
  9. <view class="container">
  10. <text class="text">点击修改宽度(递增)</text>
  11. <view class="width-progress transition-width" id="widthProgress" @click="changeWidthProgress"></view>
  12. </view>
  13. <view class="container">
  14. <text class="text">点击修改宽度(通过style设置transition)</text>
  15. <view class="base-style" style="transition-property: width; transition-duration: 1s;" id="widthOrHeightStyle"
  16. @click="changeWidthOrHeightStyle"></view>
  17. </view>
  18. <view class="container">
  19. <text class="text">点击修改Margin</text>
  20. <view class="base-style transition-margin" id="styleMargin" @click="changeMargin"></view>
  21. </view>
  22. <view class="container">
  23. <text class="text">点击修改Padding</text>
  24. <view class="base-style transition-padding" id="stylePadding" @click="changePadding">
  25. <view style="background-color: black; height: 50px; width: 50px"></view>
  26. </view>
  27. </view>
  28. <view class="container">
  29. <text class="text">点击修改background-color和opacity</text>
  30. <view class="base-style transition-background" id="styleBackground" @click="changeBackground"></view>
  31. </view>
  32. <view class="container">
  33. <text class="text">点击修改background-color(rgba)</text>
  34. <view style="flex: 1;">
  35. <view class="base-style transition-background" id="styleBackground2" @click="changeBackground2"></view>
  36. </view>
  37. </view>
  38. <view class="container">
  39. <text class="text">点击修改opacity渐隐渐现</text>
  40. <view class="base-style transition-opacity" id="styleOpacity" @click="changeStyleOpacity"></view>
  41. </view>
  42. <view class="container">
  43. <text class="text">动态修改background-color和duration</text>
  44. <view class="base-style" id="propertyStyleBackground" @click="propertyChangeBackground"></view>
  45. </view>
  46. <view class="container">
  47. <text class="text">点击修改Transform</text>
  48. <view class="base-style transition-transform" id="styleTransform" @click="changeTransform"></view>
  49. </view>
  50. <view class="container">
  51. <text class="text">点击修改TransformTranslate</text>
  52. <view class="base-style transition-transform" id="transformTranslate" @click="changeTransformTranslate"></view>
  53. </view>
  54. <view class="container">
  55. <text class="text">点击修改Transform和宽</text>
  56. <view class="base-style transition-transform-width" id="styleTransformWithWidth"
  57. @click="changeTransformWithWidth"></view>
  58. </view>
  59. <view class="container" @click="changeTransformWithOrigin">
  60. <text class="text">点击修改Transform(含transform-origin)</text>
  61. <view class="base-style transition-transform" style="transform-origin: 0 0;" id="styleTransformWithOrigin"></view>
  62. </view>
  63. <view class="container">
  64. <text class="text">点击修改Border</text>
  65. <view class="base-style transition-border" id="styleBorder" @click="changeBorder"></view>
  66. </view>
  67. <view class="container">
  68. <text class="text">点击修改Position</text>
  69. <view class="base-style transition-position" id="stylePosition" @click="changestylePosition"></view>
  70. </view>
  71. <view class="container">
  72. <text class="text">点击修改Scale
  73. </text>
  74. <view class="base-style transition-transform" id="styleChangScale" @click="changestyleScale"></view>
  75. </view>
  76. <view class="container">
  77. <text class="text">仅指定transition-duration背影有动画为正常</text>
  78. <view style="width: 100%;height: 50px;" :style="changestyleTransitionDuration"
  79. @click="doChangeTransitionDuration">
  80. </view>
  81. </view>
  82. <view class="container">
  83. <text class="text">transition-property: all/none 演示</text>
  84. <view class="base-style transition-all" id="styleTransitionAll" @click="changeTransitionAll"></view>
  85. </view>
  86. <view class="container">
  87. <text class="text">在组件内滑动测试是否闪动</text>
  88. <view class="base-style transition-transform" id="styleTransformTranslateScale" @touchmove="handleTouchMove"
  89. @touchstart="handleTouchStart" @touchend="handleTouchEnd">
  90. 这是一段文字
  91. </view>
  92. </view>
  93. <view class="container">
  94. <text class="text">测试在style中同时修改transform和width</text>
  95. <view style="width:100%;height: 100%;">
  96. <view class="base-style transition-transform-width" id="styleTransformTranslateScale"
  97. :style="{transform:`translateX(${lineLeft}px)`,width:`${lineWidth}px`}" @click="changeTransform61">
  98. </view>
  99. </view>
  100. </view>
  101. <!-- #ifndef APP-ANDROID -->
  102. <view style="margin: 7px; height: 60px;">
  103. <navigator url="/pages/CSS/transition/transition-transform" hover-class="none">
  104. <button type="primary">
  105. transform 多个属性示例
  106. </button>
  107. </navigator>
  108. </view>
  109. <!-- #endif -->
  110. <!-- #ifdef APP -->
  111. </scroll-view>
  112. <!-- #endif -->
  113. </template>
  114. <script>
  115. export default {
  116. data() {
  117. return {
  118. isTranstionWidthOrHeight: false,
  119. widthOrHeight: null as UniElement | null,
  120. widthProgress: null as UniElement | null,
  121. isTranstionWidthOrHeightStyle: false,
  122. widthOrHeightStyle: null as UniElement | null,
  123. progressWidth: 200,
  124. isTranstionChangeMargin: false,
  125. styleMargin: null as UniElement | null,
  126. isTransitionStylePadding: false,
  127. stylePadding: null as UniElement | null,
  128. isTransitionstyleBackground: false,
  129. isTransitionstyleBackground2: false,
  130. isTransitionstyleOpacity: false,
  131. styleBackground: null as UniElement | null,
  132. styleBackground2: null as UniElement | null,
  133. styleOpacity: null as UniElement | null,
  134. isTransitionStyleTransform: false,
  135. styleTransform: null as UniElement | null,
  136. isTransitionStyleTransformWithWidth: false,
  137. styleTransformWithWidth: null as UniElement | null,
  138. isTransitionstyleBorder: false,
  139. styleBorder: null as UniElement | null,
  140. isTransitionstylePosition: false,
  141. stylePosition: null as UniElement | null,
  142. isSetTransition: false,
  143. isTransitionpropertystyleBackground: false,
  144. propertyStyleBackground: null as UniElement | null,
  145. isTransitionStyleTransformWithOrigin: false,
  146. styleTransformWithOrigin: null as UniElement | null,
  147. styleTransformTranslate: null as UniElement | null,
  148. isTransformTranslate: false,
  149. styleTransformTranslateScale: null as UniElement | null,
  150. startX: 0,
  151. moveX: 0,
  152. oldX: 0,
  153. changestyleTransitionDuration: 'background-color:brown;',
  154. styleTransitionAll: null as UniElement | null,
  155. isTransitionAll: false,
  156. lineLeft: 0,
  157. lineWidth: 200,
  158. }
  159. },
  160. onReady() {
  161. this.widthOrHeight = uni.getElementById("widthOrHeight")
  162. this.widthProgress = uni.getElementById("widthProgress")
  163. this.widthOrHeightStyle = uni.getElementById("widthOrHeightStyle")
  164. this.styleMargin = uni.getElementById("styleMargin")
  165. this.stylePadding = uni.getElementById("stylePadding")
  166. this.styleBackground = uni.getElementById("styleBackground")
  167. this.styleBackground2 = uni.getElementById("styleBackground2")
  168. this.styleOpacity = uni.getElementById("styleOpacity")
  169. this.styleTransform = uni.getElementById("styleTransform")
  170. this.styleBorder = uni.getElementById("styleBorder")
  171. this.stylePosition = uni.getElementById("stylePosition")
  172. this.propertyStyleBackground = uni.getElementById("propertyStyleBackground")
  173. this.styleTransformWithOrigin = uni.getElementById("styleTransformWithOrigin")
  174. this.styleTransformWithWidth = uni.getElementById("styleTransformWithWidth")
  175. this.styleTransformTranslate = uni.getElementById("transformTranslate")
  176. this.styleTransformTranslateScale = uni.getElementById("styleTransformTranslateScale")
  177. this.styleTransitionAll = uni.getElementById("styleTransitionAll")
  178. },
  179. methods: {
  180. changeWidthOrHeight() {
  181. this.widthOrHeight?.style?.setProperty("width", this.isTranstionWidthOrHeight
  182. ? '200px'
  183. : '300px')
  184. this.isTranstionWidthOrHeight = !this.isTranstionWidthOrHeight
  185. },
  186. changeWidthProgress() {
  187. this.progressWidth += 20
  188. this.widthProgress?.style?.setProperty("width", this.progressWidth + 'px')
  189. },
  190. changeWidthOrHeightStyle() {
  191. this.widthOrHeightStyle?.style?.setProperty("width", this.isTranstionWidthOrHeightStyle
  192. ? '200px'
  193. : '300px')
  194. this.isTranstionWidthOrHeightStyle = !this.isTranstionWidthOrHeightStyle
  195. },
  196. changeMargin() {
  197. this.styleMargin?.style?.setProperty("margin-top", this.isTranstionChangeMargin
  198. ? '0px'
  199. : '50px'
  200. )
  201. this.styleMargin?.style?.setProperty("margin-left", this.isTranstionChangeMargin
  202. ? '0px'
  203. : '50px'
  204. )
  205. this.isTranstionChangeMargin = !this.isTranstionChangeMargin
  206. },
  207. changePadding() {
  208. this.stylePadding?.style?.setProperty("padding-top", this.isTransitionStylePadding
  209. ? '0px'
  210. : '50px')
  211. this.stylePadding?.style?.setProperty("padding-left", this.isTransitionStylePadding
  212. ? '0px'
  213. : '50px')
  214. this.isTransitionStylePadding = !this.isTransitionStylePadding
  215. },
  216. changeBackground() {
  217. this.styleBackground?.style?.setProperty("background-color", this.isTransitionstyleBackground
  218. ? 'brown'
  219. : 'black'
  220. )
  221. this.styleBackground?.style?.setProperty("opacity", this.isTransitionstyleBackground
  222. ? '1'
  223. : '0.5'
  224. )
  225. this.isTransitionstyleBackground = !this.isTransitionstyleBackground
  226. },
  227. changeBackground2() {
  228. this.styleBackground2?.style?.setProperty("background-color", this.isTransitionstyleBackground2
  229. ? 'brown'
  230. : 'rgba(0, 0, 0, 0.5)'
  231. )
  232. this.isTransitionstyleBackground2 = !this.isTransitionstyleBackground2
  233. },
  234. changeStyleOpacity() {
  235. this.styleOpacity?.style?.setProperty("opacity", this.isTransitionstyleOpacity
  236. ? '1'
  237. : '0'
  238. )
  239. this.isTransitionstyleOpacity = !this.isTransitionstyleOpacity
  240. },
  241. propertyChangeBackground() {
  242. if (!this.isSetTransition) {
  243. this.propertyStyleBackground?.style?.setProperty("transition-property", "background-color")
  244. this.propertyStyleBackground?.style?.setProperty("transition-duration", "1000ms")
  245. this.isSetTransition = true
  246. }
  247. this.propertyStyleBackground?.style?.setProperty("background-color", this.isTransitionpropertystyleBackground
  248. ? 'brown'
  249. : 'black'
  250. )
  251. this.isTransitionpropertystyleBackground = !this.isTransitionpropertystyleBackground
  252. },
  253. changeTransform() {
  254. this.styleTransform?.style?.setProperty("transform", this.isTransitionStyleTransform
  255. ? 'rotate(0deg)'
  256. : 'rotate(135deg)'
  257. )
  258. this.isTransitionStyleTransform = !this.isTransitionStyleTransform
  259. },
  260. changeTransformTranslate() {
  261. const translate = this.isTransformTranslate ? `translate(0%,0%)` : `translate(100%,0%)`
  262. this.styleTransformTranslate?.style?.setProperty('transform', translate)
  263. this.isTransformTranslate = !this.isTransformTranslate
  264. },
  265. changeTransformWithWidth() {
  266. this.styleTransformWithWidth?.style?.setProperty("transform", this.isTransitionStyleTransformWithWidth
  267. ? 'rotate(0deg)'
  268. : 'rotate(135deg)'
  269. )
  270. this.styleTransformWithWidth?.style?.setProperty("width", this.isTransitionStyleTransformWithWidth
  271. ? '200px'
  272. : '100px'
  273. )
  274. this.isTransitionStyleTransformWithWidth = !this.isTransitionStyleTransformWithWidth
  275. },
  276. changeTransformWithOrigin() {
  277. this.styleTransformWithOrigin?.style?.setProperty("transform", this.isTransitionStyleTransformWithOrigin
  278. ? 'scaleX(1)'
  279. : 'scaleX(0)'
  280. )
  281. this.isTransitionStyleTransformWithOrigin = !this.isTransitionStyleTransformWithOrigin
  282. },
  283. changeBorder() {
  284. this.styleBorder?.style?.setProperty("border-color", this.isTransitionstyleBorder
  285. ? 'brown'
  286. : 'yellow'
  287. )
  288. this.isTransitionstyleBorder = !this.isTransitionstyleBorder
  289. },
  290. changestylePosition() {
  291. this.stylePosition?.style?.setProperty("left", this.isTransitionstylePosition
  292. ? '0px'
  293. : '100px'
  294. )
  295. this.isTransitionstylePosition = !this.isTransitionstylePosition
  296. },
  297. changestyleScale() {
  298. let el = uni.getElementById("styleChangScale")
  299. el?.style.setProperty("transition-duration", "0ms")
  300. el?.style.setProperty("transform", "translate(10px,10px) scale(1)")
  301. setTimeout(() => {
  302. el?.style.setProperty("transition-duration", "200ms")
  303. el?.style.setProperty("transform", "translate(10px,10px) scale(0.5)")
  304. }, 200)
  305. },
  306. handleTouchStart(e : UniTouchEvent) {
  307. this.startX = e.changedTouches[0].clientX
  308. },
  309. handleTouchMove(e : UniTouchEvent) {
  310. console.log("touchmove:" + e.touches[0].clientX + "," + e.touches[0].clientY);
  311. e.preventDefault()
  312. e.stopPropagation()
  313. const difX = e.changedTouches[0].clientX
  314. this.moveX = difX - this.startX + this.oldX
  315. this.styleTransformTranslateScale?.style?.setProperty('transition-duration', '0ms')
  316. this.styleTransformTranslateScale?.style?.setProperty('transform', `translate(${this.moveX}px,0px) scale(0.5) rotate(90deg)`)
  317. },
  318. handleTouchEnd(_ : UniTouchEvent) {
  319. this.oldX = this.moveX
  320. },
  321. doChangeTransitionDuration() {
  322. console.log("doChangeTransitionDuration")
  323. this.changestyleTransitionDuration
  324. = this.changestyleTransitionDuration === 'background-color:brown;' ? 'transition-duration: 2000ms;background-color: blue;' : 'background-color:brown;'
  325. },
  326. changeTransitionAll() {
  327. if (!this.isTransitionAll) {
  328. // 设置为 all 时的效果
  329. this.styleTransitionAll?.style?.setProperty("transition-property", "all")
  330. this.styleTransitionAll?.style?.setProperty("width", "300px")
  331. this.styleTransitionAll?.style?.setProperty("height", "300px")
  332. this.styleTransitionAll?.style?.setProperty("background-color", "blue")
  333. this.styleTransitionAll?.style?.setProperty("transform", "rotate(45deg)")
  334. } else {
  335. // 设置为 none 时的效果
  336. this.styleTransitionAll?.style?.setProperty("transition-property", "none")
  337. this.styleTransitionAll?.style?.setProperty("width", "200px")
  338. this.styleTransitionAll?.style?.setProperty("height", "200px")
  339. this.styleTransitionAll?.style?.setProperty("background-color", "brown")
  340. this.styleTransitionAll?.style?.setProperty("transform", "rotate(0deg)")
  341. }
  342. this.isTransitionAll = !this.isTransitionAll
  343. },
  344. changeTransform61() {
  345. if (this.lineLeft == 0) {
  346. this.lineLeft = 70
  347. this.lineWidth = 300
  348. } else {
  349. this.lineLeft = 0
  350. this.lineWidth = 200
  351. }
  352. },
  353. },
  354. }
  355. </script>
  356. <style>
  357. .container {
  358. margin: 7px;
  359. background-color: white;
  360. }
  361. .text {
  362. margin-top: 10px;
  363. margin-bottom: 16px;
  364. }
  365. .base-style {
  366. width: 200px;
  367. height: 200px;
  368. background-color: brown;
  369. }
  370. .width-progress {
  371. width: 200px;
  372. height: 200px;
  373. background-color: brown;
  374. }
  375. .transform-bgColor {
  376. transition-property: background-color;
  377. transition-duration: 0.5s;
  378. }
  379. .transition-width {
  380. transition-property: width;
  381. transition-duration: 1s;
  382. }
  383. .transition-margin {
  384. transition-property: margin-left, margin-top;
  385. transition-duration: 1s;
  386. }
  387. .transition-padding {
  388. transition-property: padding-left, padding-top;
  389. transition-duration: 1s;
  390. }
  391. .transition-background {
  392. transition-property: background-color, opacity;
  393. transition-duration: 1s;
  394. }
  395. .transition-opacity {
  396. transition-property: opacity;
  397. transition-duration: 1s;
  398. }
  399. .transition-transform {
  400. transform: translate(0%, 0%) scaleX(1) rotate(0deg);
  401. transition-property: transform;
  402. transition-duration: 1s;
  403. }
  404. .transition-border {
  405. border-width: 5px;
  406. border-color: brown;
  407. border-style: solid;
  408. transition-property: border-color;
  409. transition-duration: 1s;
  410. }
  411. .transition-position {
  412. left: 0px;
  413. transition-property: left;
  414. transition-duration: 1s;
  415. }
  416. .transition-transform-width {
  417. transform: rotate(0deg);
  418. transition-property: transform, width;
  419. transition-duration: 1s;
  420. }
  421. .transition-all {
  422. transition-property: all;
  423. transition-duration: 1s;
  424. }
  425. </style>