App.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <div id="app">
  3. <div class="view-container">
  4. <AppHeader :visible="visible"></AppHeader>
  5. <router-view />
  6. <AppBackTop></AppBackTop>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. import AppHeader from '@/components/AppHeader.vue';
  12. import AppBackTop from '@/components/AppBackTop.vue';
  13. export default {
  14. name: 'app',
  15. components: {
  16. AppHeader,
  17. AppBackTop
  18. },
  19. data() {
  20. return {
  21. isGray: false,
  22. visible:true,
  23. };
  24. },
  25. methods:{
  26. // 保存滚动值,这是兼容的写法
  27. handleScroll () {
  28. var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
  29. //变量scrollTop是滚动条滚动时,距离顶部的距离
  30. // var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  31. // //变量windowHeight是可视区的高度
  32. // var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
  33. // //变量windowHeight是可视区的高度
  34. // var scrollHeight =document.documentElement.scrollHeight || document.body.scrollHeight;
  35. // //滚动条到底部的条件
  36. // if(scrollTop + windowHeight == scrollHeight){
  37. // //你要触发的方法
  38. // }
  39. if(scrollTop >= 80){
  40. this.visible = false;
  41. }else{
  42. this.visible = true;
  43. }
  44. },
  45. },
  46. mounted () {
  47. window.addEventListener('scroll', this.handleScroll)
  48. },
  49. destroyed () {
  50. // 离开该页面需要移除这个监听的事件,不然会报错
  51. window.removeEventListener('scroll', this.handleScroll)
  52. }
  53. }
  54. </script>
  55. <style>
  56. .view-container{
  57. position: relative;
  58. margin: 0 auto;
  59. width: 100%;
  60. max-width: 1200px;
  61. }
  62. </style>