Vue2.5 学习笔记 9.3 详情页的header渐隐渐现

效果是 在详情页的页面内容往下拉的时候  顶部的标题栏能够渐渐的显现出来

开始:

1. 建子组件

detail/components/Header.vue

 

然后那些吧子组件加到Detail.vue的步骤就不写了

直接把代码贴出来 在代码中加注释

<template>
  <div>
// 这部分是左边的那个小箭头 用于返回到首页的
    <router-link
      tag="div"
      to="/"
      class="header-abs"
// 这是用于判断顶部样式和这个返回键的切换
      v-show="showAbs"
    >
    <div class="iconfont header-abs-back">&#xeb99;</div>
    </router-link>

// 这是导航栏的部分
    <div
      class="header-fixed"
      v-show="!showAbs"
// 这里 这里是样式绑定。 等下需要改变样式
      :style="opacityStyle"
    >
      <router-link to='/'>
        <div class="iconfont header-fixed-back">&#xeb99;</div>
      </router-link>
      景点详情
    </div>
  </div>
</template>
<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showAbs: true,
// 透明度 先设置为0
      opacityStyle: {
        opacity: 0
      }
    }
  },
  methods: {
    handleScroll () {
// 滚动条距离顶部的距离 依次来计算透明度的百分比
      const top = document.documentElement.scrollTop
      if (top > 60) {
        let opacity = top / 140
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = { opacity }
        this.showAbs = false
      } else {
        this.showAbs = true
      }
    }
  },
// 教程里面是说用 activated 但是没用  我尝试着使用created 竟然可以 那我就用这个好了
  created () {
// 监听页面滚动
    window.addEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .header-abs
    position: absolute
    left: .2rem
    top: .2rem
    width: .8rem
    height: .8rem
    line-height: .8rem
    border-radius: .4rem
    text-align: center
    background: rgba(0, 0, 0, 0.8)
    .header-abs-back
      color: #fff
      font-size: .4rem
  .header-fixed
    position: fixed
    top: 0
    left: 0
    right: 0
    height: $headerHeight
    line-height: $headerHeight
    text-align: center
    color: #fff
    background: $bgColor
    font-size: .32rem
    .header-fixed-back
      position: absolute
      top: 0
      left: 0
      width: .64rem
      text-align: center
      font-size: .4rem
      color: #fff
</style>

效果图:

拉动下来的效果:

打动大于140的时候:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@凌晨三点半

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值