Commit 1f63183c9a190cdb6e8132e3131dbbece128d078

Authored by liuqimichale
1 parent 7675bc90

回到顶部

Showing 1 changed file with 37 additions and 1 deletions
webintroduce/src/App.vue
... ... @@ -7,6 +7,7 @@
7 7 </keep-alive>
8 8 </transition>
9 9 <VFooter v-if="footerShow"></VFooter>
  10 + <div id="backToTop" class="backToTop" @click="scrollIntoView">回到顶部</div>
10 11 </div>
11 12 </template>
12 13  
... ... @@ -23,7 +24,32 @@ export default {
23 24 return {
24 25 footerShow: true,
25 26 headerShow: true,
26   - transitionName: 'transitionLeft'
  27 + transitionName: 'transitionLeft',
  28 + scrollShow: false,
  29 + }
  30 + },
  31 + mounted() {
  32 + window.addEventListener('scroll',this.scrollToTop)
  33 + },
  34 + methods: {
  35 + scrollToTop() {
  36 + const topBtn = document.getElementById('backToTop')
  37 + const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
  38 + if(scrollTop > 20){
  39 + topBtn.style.display = 'block'
  40 + }else{
  41 + topBtn.style.display = 'none'
  42 + }
  43 + },
  44 + scrollIntoView() {
  45 + const timer = setInterval(function () {
  46 + let osTop = document.documentElement.scrollTop || document.body.scrollTop
  47 + let ispeed = Math.floor(-osTop / 5)
  48 + document.documentElement.scrollTop = document.body.scrollTop = osTop + ispeed
  49 + if (osTop === 0) {
  50 + clearInterval(timer)
  51 + }
  52 + }, 30)
27 53 }
28 54 },
29 55 watch: {
... ... @@ -47,6 +73,7 @@ export default {
47 73 display: flex;
48 74 flex-flow: column;
49 75 min-height: 100vh;
  76 + position: relative;
50 77 font-family: 'Avenir', Helvetica, Arial, sans-serif;
51 78 -webkit-font-smoothing: antialiased;
52 79 -moz-osx-font-smoothing: grayscale;
... ... @@ -69,4 +96,13 @@ export default {
69 96 transform: translate(-100%, 0);
70 97 -webkit-transform: translate(-100%, 0);
71 98 }
  99 + .backToTop{
  100 + position: fixed;
  101 + width: 100px;
  102 + height: 40px;
  103 + background-color: #f00;
  104 + right:10px;
  105 + bottom: 10px;
  106 + display: none;
  107 + }
72 108 </style>
... ...