| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <template>
- <div id="app">
- <div class="view-container">
- <AppHeader :visible="visible"></AppHeader>
- <router-view />
- <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 {
- 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)
- }
- }
- </script>
- <style>
- .view-container{
- position: relative;
- margin: 0 auto;
- width: 100%;
- max-width: 1200px;
- }
- </style>
|