Vue组件(点击返回顶部)

  1. 在components文件夹下创建 ClientHeight.vue 和 index.js 文件
 //ClientHeight.vue配置
<template>
    <div class="th-back-top" v-show="flag" @click="clickHidden">
        <span class="bg-back2top2"></span>
    </div>
</template>
<script>
export default {
    name:"BackTop",
    data(){
        return{
            flag:false
        }
    },
    mounted(){
        // console.log(window.document.documentElement.clientHeight);  //获取页面可视化高度

       window.addEventListener("scroll",()=>{
        //    console.log(document.documentElement.scrollTop);  //获取页面滚动的高度
           let scrollTop = document.documentElement.scrollTop;
           if(scrollTop > 100){
               this.flag = true;
           }else{
               this.flag = false;
           }

       }) 
    },
    methods:{
       clickHidden(){
           document.documentElement.scrollTop = 0;   //点击返回顶部
       }
    }
}
</script>
<style scoped>

.th-back-top{
    position:fixed;
    right:15px;
    bottom:104px;
    height: 39px;
    width: 80px;
    z-index:9;
    opacity:0.8;
    text-align:right;
}
.bg-back2top2 {
    display: inline-block;
    background: url(//static1.qianqian.com/web/st/images/icon-back2top2.af69f845890e1a53.png)
        no-repeat 0 0;
    -webkit-background-size: 39px 39px;
    background-size: 39px 39px;
    width: 39px;
    height: 39px;
}
</style>
  1. 同级 index.js 导出
import ClientHeight from './AA/ClientHeight'
export default (Vue)=>{
    Vue.component(ClientHeight.name,ClientHeight);
}
  1. main.js 文件引入
import ClientHeight from './components/ClientHeight/index'
Vue.use(ClientHeight);
  1. App 中使用 :<ClientHeight/>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值