animate.uvue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex:1">
  4. <!-- #endif -->
  5. <view id="main" style="width: 100px;height: 100px; background-color: brown; transform: scale(1);"></view>
  6. <button @click="startAnimate">开始动画</button>
  7. <!-- #ifndef APP-HARMONY -->
  8. <!-- #ifndef MP-WEIXIN -->
  9. <button @click="pauseAnimate">暂停动画</button>
  10. <button @click="resumeAnimate">恢复动画</button>
  11. <!-- #endif -->
  12. <button @click="cancelAnimate">取消动画</button>
  13. <image src="/static/uni.png" id="roll" style="width: 100px; height: 100px;margin: 10px;"></image>
  14. <view
  15. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  16. <text style="margin-bottom: 4px;">修改宽度</text>
  17. <view id="widthProperty" style="width: 100px;height: 100px;background-color: brown;" @click="widthProperty"></view>
  18. </view>
  19. <view
  20. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  21. <text style="margin-bottom: 4px;">修改高度</text>
  22. <view id="height1" style="width: 100px;height: 100px;background-color: brown;" @click="heightProperty"></view>
  23. </view>
  24. <view
  25. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  26. <text style="margin-bottom: 4px;">修改margin</text>
  27. <view id="marginProperty" style="width: 100px;height: 100px;background-color: brown;" @click="marginProperty"></view>
  28. </view>
  29. <view
  30. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  31. <text style="margin-bottom: 4px;">修改padding</text>
  32. <view id="paddingProperty" style="width: 100px;height: 100px;background-color: brown;" @click="paddingProperty">
  33. <view style="width: 50px;height: 50px;background-color: black;"></view>
  34. </view>
  35. </view>
  36. <view
  37. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  38. <text style="margin-bottom: 4px;">修改border颜色</text>
  39. <view id="borderProperty"
  40. style="width: 100px;height: 100px;background-color: brown;border-width: 10px;border-color: black;border-style: solid;"
  41. @click="borderProperty"></view>
  42. </view>
  43. <view
  44. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  45. <text style="margin-bottom: 4px;">修改transform</text>
  46. <view id="transformProperty" style="width: 100px;height: 100px;background-color: brown;" @click="transformProperty"></view>
  47. </view>
  48. <view
  49. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  50. <text style="margin-bottom: 4px;">修改position</text>
  51. <view id="positionProperty" style="width: 100px;height: 100px;background-color: brown;" @click="positionProperty"></view>
  52. </view>
  53. <!-- #endif -->
  54. <!-- #ifndef MP-WEIXIN -->
  55. <!-- #ifndef APP-HARMONY -->
  56. <view
  57. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  58. <text style="margin-bottom: 4px;">修改背景色和宽度</text>
  59. <view id="backgroundAndWidthProperty" style="width: 100px;height: 100px;background-color: brown;" @click="backgroundAndWidthProperty"></view>
  60. </view>
  61. <!-- #endif -->
  62. <view
  63. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  64. <text style="margin-bottom: 4px;">执行的动画只有一个值1</text>
  65. <view id="oneProperty1" style="width: 100px;height: 100px;background-color: brown;" @click="oneProperty1"></view>
  66. </view>
  67. <view
  68. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  69. <text style="margin-bottom: 4px;">执行的动画只有一个值2</text>
  70. <view id="oneProperty2" style="width: 100px;height: 100px;background-color: brown;" @click="oneProperty2"></view>
  71. </view>
  72. <!-- #endif -->
  73. <view
  74. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  75. <text style="margin-bottom: 4px;">修改背景色和margin-left(关键帧)</text>
  76. <view id="backgroundAndMarginLeftProperty" style="width: 100px;height: 100px;background-color: brown;" @click="backgroundAndMarginLeftProperty"></view>
  77. </view>
  78. <view
  79. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  80. <text style="margin-bottom: 4px;">修改背景色和transform(关键帧)</text>
  81. <view id="backgroundAndTransformProperty" style="width: 100px;height: 100px;background-color: brown;" @click="backgroundAndTransformProperty"></view>
  82. </view>
  83. <view
  84. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  85. <text style="margin-bottom: 4px;">修改背景色(关键帧)</text>
  86. <view id="backgroundProperty" style="width: 100px;height: 100px;background-color: brown;" @click="backgroundProperty"></view>
  87. </view>
  88. <view
  89. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  90. <text style="margin-bottom: 4px;">修改opacity(关键帧)</text>
  91. <view id="opacityProperty" style="width: 100px;height: 100px;background-color: brown;" @click="opacityProperty"></view>
  92. </view>
  93. <!-- #ifndef APP-HARMONY -->
  94. <view
  95. style="border-radius: 5px;margin: 4px;padding: 4px;border-style: solid;background-color: #eee;border-color: #eee;">
  96. <text style="margin-bottom: 4px;">修改border-color和margin-left(关键帧)</text>
  97. <view id="borderColorMarginLeftProperty" style="width: 100px;height: 100px;background-color: brown;border-width: 5px;border-style: solid;"
  98. @click="borderColorMarginLeftProperty"></view>
  99. </view>
  100. <!-- #endif -->
  101. <!-- #ifdef APP -->
  102. </scroll-view>
  103. <!-- #endif -->
  104. </template>
  105. <script>
  106. export default {
  107. data() {
  108. return {
  109. mainView: null as UniElement | null,
  110. animation: null as UniAnimation | null
  111. }
  112. },
  113. onReady() {
  114. this.mainView = uni.getElementById("main")
  115. var roll = uni.getElementById("roll")
  116. roll!.animate([
  117. { transform: "translateX(0) rotate(0)" }, // keyframe
  118. { transform: "translateX(200px) rotate(540deg)" }, // keyframe
  119. ],
  120. {
  121. // keyframe options
  122. duration: 2000,
  123. direction: "alternate",
  124. easing: "ease-in-out",
  125. iterations: Infinity,
  126. },
  127. )
  128. },
  129. methods: {
  130. startAnimate() {
  131. this.animation = this.mainView!.animate([
  132. {
  133. transform: "scale(1)",
  134. transformOrigin: "0px 0px"
  135. },
  136. {
  137. transform: "scale(0)",
  138. transformOrigin: "50px 50px"
  139. },
  140. {
  141. transform: "scale(1)",
  142. transformOrigin: "100px 100px"
  143. }
  144. ], {
  145. duration: 5000,
  146. })
  147. this.animation!.oncancel = (_ : UniAnimationPlaybackEvent) => {
  148. uni.showToast({
  149. title: "动画被取消了"
  150. })
  151. }
  152. this.animation!.onfinish = (_ : UniAnimationPlaybackEvent) => {
  153. uni.showToast({
  154. title: "动画播放完成"
  155. })
  156. }
  157. },
  158. pauseAnimate() {
  159. this.animation?.pause()
  160. },
  161. resumeAnimate() {
  162. this.animation?.play()
  163. },
  164. cancelAnimate() {
  165. this.animation?.cancel()
  166. },
  167. widthProperty(e : UniPointerEvent) {
  168. e.currentTarget?.animate({
  169. width: ["100px", "200px", "100px"]
  170. }, {
  171. duration: 1000,
  172. fill: "forwards"
  173. })
  174. },
  175. heightProperty(e : UniPointerEvent) {
  176. e.currentTarget?.animate({
  177. height: ["100px", "200px"]
  178. }, {
  179. duration: 1000,
  180. fill: "forwards"
  181. })
  182. },
  183. marginProperty(e : UniPointerEvent) {
  184. e.currentTarget?.animate({
  185. margin: ["8px", "16px", "32px"]
  186. }, {
  187. duration: 1000,
  188. fill: "forwards"
  189. })
  190. },
  191. paddingProperty(e : UniPointerEvent) {
  192. e.currentTarget?.animate({
  193. padding: ["0px", "16px", "32px", "0px"]
  194. }, {
  195. duration: 1000,
  196. fill: "forwards"
  197. })
  198. },
  199. backgroundProperty(e : UniPointerEvent) {
  200. e.currentTarget?.animate([
  201. {
  202. offset: 0.3,
  203. backgroundColor: "yellow"
  204. },
  205. {
  206. offset: 0.6,
  207. backgroundColor: "red"
  208. },
  209. {
  210. backgroundColor: "blue"
  211. }
  212. ], {
  213. duration: 1000,
  214. fill: "forwards"
  215. })
  216. },
  217. borderProperty(e : UniPointerEvent) {
  218. e.currentTarget?.animate([
  219. {
  220. offset: 0.3,
  221. borderColor: "yellow"
  222. },
  223. {
  224. offset: 0.6,
  225. borderColor: "pink"
  226. },
  227. {
  228. borderColor: "blue"
  229. }
  230. ], {
  231. duration: 1000,
  232. fill: "forwards"
  233. })
  234. },
  235. transformProperty(e : UniPointerEvent) {
  236. e.currentTarget?.animate([
  237. {
  238. transform: "translateX(0px) scale(1) rotate(0deg)"
  239. },
  240. {
  241. transform: "translateX(100px)"
  242. },
  243. {
  244. transform: "scale(0.8) rotate(180deg)"
  245. }
  246. ], {
  247. duration: 1000,
  248. fill: "forwards"
  249. })
  250. },
  251. positionProperty(e : UniPointerEvent) {
  252. e.currentTarget?.animate({
  253. left: ["0px", "16px", "32px", "0px"]
  254. }, {
  255. duration: 1000,
  256. fill: "forwards"
  257. })
  258. },
  259. backgroundAndWidthProperty(e : UniPointerEvent) {
  260. e.currentTarget?.animate({
  261. width: ["100px", "200px"],
  262. backgroundColor: ["red", "yellow", "blue"]
  263. }, {
  264. duration: 1000,
  265. fill: "forwards"
  266. })
  267. },
  268. backgroundAndMarginLeftProperty(e : UniPointerEvent) {
  269. e.currentTarget?.animate([
  270. {
  271. offset: 0.2,
  272. backgroundColor: "red"
  273. }, {
  274. marginLeft: "10px"
  275. }, {
  276. marginLeft: "20px"
  277. }, {
  278. marginLeft: "30px",
  279. backgroundColor: "pink"
  280. }
  281. ], {
  282. duration: 1000,
  283. fill: "forwards"
  284. })
  285. },
  286. backgroundAndTransformProperty(e : UniPointerEvent) {
  287. e.currentTarget?.animate([
  288. {
  289. offset: 0.2,
  290. backgroundColor: "red"
  291. }, {
  292. transform: "translate(30px,0px)"
  293. }, {
  294. transform: "translate(50px,0px)"
  295. }, {
  296. transform: "translate(100px,0px)",
  297. backgroundColor: "pink"
  298. }
  299. ], {
  300. duration: 1000,
  301. fill: "forwards"
  302. })
  303. },
  304. oneProperty1(e : UniPointerEvent) {
  305. e.currentTarget?.animate({
  306. backgroundColor: "green"
  307. }, {
  308. duration: 1000,
  309. fill: "forwards"
  310. })
  311. },
  312. oneProperty2(e : UniPointerEvent) {
  313. e.currentTarget?.animate([{
  314. backgroundColor: "blue"
  315. }], {
  316. duration: 1000,
  317. fill: "forwards"
  318. })
  319. },
  320. borderColorMarginLeftProperty(e : UniPointerEvent) {
  321. e.currentTarget?.animate([
  322. {
  323. borderColor: "red",
  324. marginLeft: "0px",
  325. offset: 0
  326. },
  327. {
  328. marginLeft: "20px",
  329. offset: 0.5
  330. },
  331. {
  332. marginLeft: "60px",
  333. borderColor: "yellow",
  334. offset: 1
  335. }
  336. ], {
  337. duration: 1000,
  338. fill: "forwards"
  339. })
  340. },
  341. opacityProperty(e : UniPointerEvent){
  342. e.currentTarget?.animate([
  343. {
  344. offset: 0.3,
  345. opacity: "1"
  346. },
  347. {
  348. offset: 0.6,
  349. opacity: "0.1"
  350. },
  351. {
  352. opacity: "1"
  353. }
  354. ], {
  355. duration: 1000,
  356. fill: "forwards"
  357. })
  358. }
  359. }
  360. }
  361. </script>
  362. <style>
  363. .view-margin {
  364. margin: 8px;
  365. }
  366. </style>