vue滚动监听按钮显示隐藏+返回顶部功能

需求是移动端的返回顶部和按钮显示与隐藏。

html+css

<div id="app">

        <div style="height:1000px;">

        </div>

        <button @click="backtop" v-if="flag" style="position:fixed; bottom:0" ref="backtop">reverse</button>

    </div>


js部分
var vm = new Vue({

            el: "#app",

            data: {

                flag: ""

            },

            methods: {

                getScroll() {

                    this.scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;

                    console.log(this.scrollTop)

                    if (!!document.documentElement.scrollTop && document.documentElement.scrollTop >= 300) {

                        this.flag = true

                    } else {

                        this.flag = false

                    }

                },

         backtop(){

          const timer = setInterval(function(){

            const osTop = document.documentElement.scrollTop || document.body.scrollTop;

            const ispeed = Math.floor(-osTop / 5);

            document.documentElement.scrollTop = document.body.scrollTop = osTop + ispeed;

            this.isTop = true;

            if(osTop === 0){

              clearInterval(timer);

            }

          },30)

      },

            },

            mounted() {

                window.addEventListener('scroll', this.getScroll);

            }

        })

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,068评论 0 2
  • 100个常用的javascript函数 1、原生JavaScript实现字符串长度截取 复制代码代码如下: fun...
    老头子_d0ec阅读 376评论 0 0
  • 源站:http://fengyuanchen.github.io/viewer/ 应用: html: 源码上是正常...
    羊绘霖阅读 5,595评论 0 2
  • 1.第一种方式:锚链接 优点:简单快速,没有兼容性问题 缺点: 视觉上不够直观,用户体验不太好 2.js的方式: ...
    love2013阅读 816评论 0 0
  • 对佳联(1、2、3) 友出上联1:思有心,恨有心,情随秋水。 我对下联1:恋无奈,愁无奈,梦断鹊桥。 友对下联...
    文斌今嗨阅读 482评论 6 21