<template>
  <transition :name="'dir' + dir.toUpperCase()">
    <div class="slider-item" v-if="selfIndex === currentIndex">
      <slot></slot>
    </div>
  </transition>
</template>

<script>
import { getCurrentInstance, reactive, toRefs, watch } from "vue";
export default {
  name: "SliderItem",
  setup(props,ctx) {
    const instance = getCurrentInstance();
    const state = reactive({
      selfIndex: instance.vnode.key,
      currentIndex: instance.proxy.$parent.currentIndex,
      dir:instance.proxy.$parent.dir
    });
    /*                                                                                                                                                                        
     *监听currentIndex变化
    */
    watch(
      () => {
        return instance.proxy.$parent.currentIndex;
      },
      (value) => {
        state.currentIndex = value;
      }
    );
    return {
      ...toRefs(state),
    };
  },
};
</script>

<style lang="scss" scoped>
.slider-item {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
} 
.dirX-enter-active,
.dirX-leave-active,
.dirY-enter-active,
.dirY-leave-active
 {
  transition: all 1s linear;
}

.dirX-enter-active {
  transform: translateX(100%);
}
.dirY-enter-active {
  transform: translateY(100%);
}


.dirX-enter-to {
  transform: translateX(0);
}
.dirY-enter-to {
  transform: translateY(0);
}


.dirX-leave-active {
  transform: translateX(0);
}
.dirY-leave-active {
  transform: translateY(0);
}

.dirX-leave-to {
  transform: translateX(-100%);
}
.dirY-leave-to {
  transform: translateY(-100%);
}
</style>