vue 页面监听滚动条

项目要求写商城商品详情的时候需要的效果跟大疆官网商城效果一样。

 样式:(右侧商品基本信息高度超过左侧商品照片时,页面整体滚动到浏览器最顶部时左侧照片固定,右侧继续滚动,滚到左右两侧底部对齐的时候,一起向上滚)

1、app.vue

   @scroll="showHeader"事件绑到 #app元素上

     <router-view :scrollTopForApp='scrollTopForApp' v-if="$route.meta.keepAlive" class="main"></router-view>

data(){

 return {

      scrollTopForApp:0,

    }

}

  method:showHeader (e) {

      this.scrollTopForApp = e.target.scrollTop || 0

    },

2、你需要监听的页面

左侧照片元素 <div class="detail-left  :class="{xcq:topx,xcqb:bottomx}" ref="leftHeight" >

右侧信息元素 <div class="detail-right" :style="{left:topx || bottomx?'600px':'80px'}" ref="Fixed">

  props: ['scrollTopForApp'],

data(){

return:{

      rightDom: {},  //

      leftDom: {},

      topx:0,  

      bottomx:false,

}}

method:{

   async getDetail() {

        //获取商品。。。。。

        this.$nextTick(() => {

        console.log(this.scrollTopForApp)

        this.rightDom.height = this.$refs.Fixed.offsetHeight    //获取右侧商品基本介绍元素高度

        this.leftDom.height = this.$refs.leftHeight.offsetHeight     //获取左侧商品照片元素高度

        console.log(this.rightDom)

        console.log(this.leftDom)

      })

      }

      return detail

    },

 watch: {

    scrollTopForApp(n,m){    //监听scrollTopForApp

      let a = this.$refs.leftHeight.getBoundingClientRect().top    //获取左侧照片距离顶部距离

       let a_h = this.$refs.leftHeight.offsetHeight

    if(a<0){

      this.topx=true

    }

     let b = this.$refs.detail.offsetHeight

     let c =  this.$refs.detail.getBoundingClientRect().top

      if(c>0){

      this.topx=false

    }

     if(d<a_h){

       this.topx = false

       }

      this.bottomx = d<a_h

    },

}

}

css:  

  左侧:.xcq{   //首先要定位

    position: fixed;

    top:0

  }

  .xcqb{      //左侧在与右侧底部距离一致时,position需要变为absolute可实现与右侧一起向上继续滚动

    position: absolute;

    bottom: 0;

  }

右侧:&-right {    右侧始终 position: relative;

    position: relative;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值