<template>
  <div id="app">
    <div class="view-container">
      <AppHeader :visible="visible"></AppHeader>
      <transition :name="transitionName">
        <router-view />
      </transition>
      <AppBackTop></AppBackTop>
    </div>
  </div>
</template>
<script>
import AppHeader from '@/components/AppHeader.vue';
import AppBackTop from '@/components/AppBackTop.vue';
export default {
  name: 'app',
  components: {
    AppHeader,
    AppBackTop
  },
  data() {
    return {
      transitionName: "",
      isGray: false,
      visible:true,
    };
  },
  methods:{
    // 保存滚动值，这是兼容的写法
    handleScroll () {
      var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
          //变量scrollTop是滚动条滚动时，距离顶部的距离
      // var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    //   //变量windowHeight是可视区的高度
    //   var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
    //   //变量windowHeight是可视区的高度
    //   var scrollHeight =document.documentElement.scrollHeight || document.body.scrollHeight;
    //       //滚动条到底部的条件
    //    if(scrollTop + windowHeight == scrollHeight){
    //         //你要触发的方法
    //     }
        if(scrollTop >= 80){
            this.visible = false;
        }else{
          this.visible = true;
        }
    },
  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll)
  },

  destroyed () {
    // 离开该页面需要移除这个监听的事件，不然会报错
    window.removeEventListener('scroll', this.handleScroll)
  },
  watch: {
    //使用watch 监听$router的变化
    $route(to, from) {
      //如果to索引大于from索引,判断为前进状态,反之则为后退状态
      console.log(to, "to");
      console.log(from, "from");
      if (to.meta.index > from.meta.index) {
        //设置动画名称
        this.transitionName = "slide-left";
      } else {
        this.transitionName = "slide-right";
      }
    },
  },
}
</script>
<style>
.view-container{
  position: relative;
  margin: 0 auto;
  width: 100%;
  max-width: 1200px;
}


.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
  will-change: transform;
  transition: all 250ms;
  position: fixed;
}
​
.slide-right-enter {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}
​
.slide-right-leave-active {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}
​
.slide-left-enter {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}
​
.slide-left-leave-active {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}
</style>