index.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <view class="nut-animate">
  3. <view :class='classes' @click='handleClick'>
  4. <slot></slot>
  5. </view>
  6. </view>
  7. </template>
  8. <script lang="ts">
  9. import { reactive, toRefs, computed, PropType } from 'vue';
  10. import { createComponent } from '@/packages/utils/create';
  11. const { componentName, create } = createComponent('animate');
  12. export default create({
  13. props: {
  14. type: {
  15. type: String as PropType<import('./type').AnimateType>,
  16. default: ''
  17. },
  18. action: {
  19. type: String as PropType<import('./type').AnimateAction>,
  20. default: 'initial'
  21. },
  22. loop: {
  23. type: Boolean,
  24. default: false
  25. }
  26. },
  27. emits: ['click'],
  28. setup(props, { emit }) {
  29. const { type, loop, action } = toRefs(props);
  30. const state = reactive({
  31. clicked: false
  32. });
  33. let classes = computed(() => {
  34. const prefixCls = componentName;
  35. return {
  36. 'nut-ani-container': true,
  37. [`${prefixCls}-${type.value}`]: action.value === 'initial' || state.clicked ? type.value : false,
  38. 'loop': loop.value,
  39. };
  40. });
  41. const handleClick = (event: Event) => {
  42. state.clicked = true
  43. //如果不是无限循环,清除类名
  44. if (!loop.value) {
  45. setTimeout(() => {
  46. state.clicked = false
  47. }, 1000)
  48. }
  49. emit('click', event);
  50. };
  51. return { ...toRefs(state), classes, handleClick };
  52. }
  53. });
  54. </script>