夜来随笔-js中监听滚动条变化,动态配置头部样式,及返回顶部

271 阅读2分钟

在做pc端或者wap端开发中,时常会有动态配置头部需求。

头部太高,会使整个页面可见内容变少,这时候会想到根据滑动滚动条过程中隐藏头部、或者是头部变窄。接下来就以pc端为例,如下是正常页面样式:

088a31e62df57e0a76c98ee486a21bd.png

当页面向上滑动,随着滚动变化,使头部缩小,如下图:

060d034e1464a6146c1e20c4df5fd6c.png

另外,页面还有一个控制返回顶部的按钮。

那么我们如何实现这个功能呢?

首先,将头部样式写好。

头部样式: 定位顶部,高度80px,设置背景,最大宽度1200px,居中,左右flex布局,logo和菜单。 再配置,缩小后的样式,头部高度40px,logo以左边为基点缩小0.5,菜单以右边为基点缩小0.7。 最后写上返回顶部按钮,定位底部上方150px. 代码如下:

<div id="head" class="head">
  <div class="top">
    <img src="../../public/imgs/logo.png" alt="">
    <div class="right">
      <div class="active">首页</div>
      <div>新闻</div>
    </div>
  </div>
  <div id="upIcon" class="to-top">
    <i class="icon ion-chevron-up"></i>
  </div>
</div>

/* 头部 */
.head{
    background-color: #7675fe;
    color: #fff;
    position: fixed;
    top: 0;
    width: 100%;
    height: 80px;
    z-index: 9;
    transition: .3s;
}
.head.scale{
    height: 40px;
}
.head .top{
    width: 1200px;
    min-width: 1200px;
    height: 100%;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 50%;
}
.head .to-top{
    position: fixed;
    z-index: 9;
    right: 20px;
    bottom: 150px;
    padding: 10px;
    box-sizing: border-box;
    box-shadow: 1px 1px #eee;
    background-color: #fff;
    border-radius: 50%;
    overflow: hidden;
    width: 40px;
    height: 40px;
    text-align: center;
    line-height: 20px;
    opacity: 0;
    transition: .5s;
    cursor: pointer;
}
.head .to-top.active{
    opacity: 1;
}
.head .to-top i{
    color: #7675fe;
    font-size: 20px;
}
.head >div img{
    width: 200px;
    height: 50px;
    object-fit: cover;
    transition: .3s;
}
.head.scale >div img{
    transform-origin: left;
    transform: scale(.5);
}
.head >div .right{
    display: flex;
    align-items: center;
    font-size: 20px;
    transition: .3s;
}
/* transform: scale(1) 缩放 ; transform-origin: right top; 基点变换*/
.head.scale >div .right{
    transform-origin: right;
    transform: scale(.7); 
}
.head >div .right >div{
    padding: 20px 20px 8px;
    box-sizing: border-box;
    color: #e4e4e4;
    position: relative;
    cursor: pointer;
    transition: .3s;
}
.head >div .right >div:hover{
    color: #fff;
}
.head >div .right >div.active{
    color: #fff;
}
.head >div .right >div.active::after{
    content: '';
    height: 3px;
    width: 50px;
    background-color: #fff;
    position: absolute;
    bottom: 0;
    left: 16px;
}

之后写js代码控制样式。第一步,监听滚动条变化,高于80px重置样式,反之也重置样式。第二部,返回听不按钮配置点击事件,注意,有时候直接加点击事件不生效,可能何渲染前后有关。代码如下:

// 监听滚动条变化 头部缩放
$(window).scroll(function(){
  let scrollHeight = document.documentElement.scrollTop||document.body.scrollTop;
  let nodeId = document.getElementById('head')
  let upIcon = document.getElementById('upIcon')
  if(scrollHeight>80){
    nodeId.setAttribute('class', 'head scale')
    upIcon.setAttribute('class', 'to-top active')
  } else {
    nodeId.setAttribute('class', 'head')
    upIcon.setAttribute('class', 'to-top')
  }
})

// 返回顶部
$(function(){
  // 不能直接写
  $('#upIcon').on('click',function () {
    $('body,html').animate({scrollTop:0},500);
  });
});

如此,我们就将此功能完成了。