前端吸顶功能,滚动到一定位置时固定到顶部

适用环境:菜单滑动后固定,搜索框的固定

(只做全了功能,没有写完美样式,明白原理,样式可自行调整,为了区分效果,颜色反差比较明显)先看图:

9117ef958c6361191e34b5a6b3bf9e1b7af.jpg5125a3ce6a4dd9b3a8e9829ff01b25840f3.jpg

下面贴上代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>吸顶容器</title>
  <style>
    .body {
      margin: 0px !important;
      padding: 0;
      box-sizing: border-box;
    }

    .road-tab-fixed {
      position: fixed;
      top: 0%;
      width: 100%;
    }

    .top {
      background: #c4f8b4;
      height: 50px;
    }

    .list {
      background: #ccc;
      width: 100%;
    }

    .list li {
      height: 50px;
      border-bottom: 2px solid #f00;
    }

    .banner {
      width: 100%;
      background: #c19aff;
      height: 150px;
    }
  </style>
</head>

<body>
  <div class="cont">
    <div class="banner">这是广告图</div>
    <div class="top" id="road-tab">菜单</div>
    <div class="list">
      <ul>
        <li>0</li>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
        <li>0</li>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
      </ul>
    </div>
  </div>
  <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
  <script>
    const fixedDom = $("#road-tab"),
      tabclass = "road-tab-fixed";
    let beforeElementScrollTop = 0;
    let beforeOffsetTop = fixedDom[0].offsetTop;
    //scroll节流
    const throttle = (func, wait, mustRun) => {
      var timeout,
        startTime = new Date();

      return function () {
        var context = this,
          args = arguments,
          curTime = new Date()
        clearTimeout(timeout)
        // 如果达到了规定的触发时间间隔,触发 handler
        if (curTime - startTime >= mustRun) {
          beforeElementScrollTop = document.documentElement.scrollTop
          console.log("beforelementScrollTop----------", document.body.scrollTop);
          func.apply(context, args);
          startTime = curTime
          // 没达到触发间隔,重新设定定时器
        } else {
          timeout = setTimeout(func, wait)
        }
      }
    }
    const winScroll = (e) => {
      const elementScrollTop = document.documentElement.scrollTop
      console.log('elementScrollTop--------------', elementScrollTop);
      if (beforeElementScrollTop - elementScrollTop <= 0) {//up
        console.log('up');
        if (beforeOffsetTop - elementScrollTop < 0) {
          fixedDom.addClass("road-tab-fixed")
        }
      } else {
        if (beforeOffsetTop - elementScrollTop >= 0) {
          console.log("UUUUUU");
          fixedDom.removeClass("road-tab-fixed")
        }
      }
    };
    $(window).off("scroll").on("scroll", throttle(winScroll, 10, 100));
  </script>
</body>
</html>

主要运用到了scrollTop和offsetTop。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中实现元素吸顶固定位置的方式是通过监听页面滚动事件并动态改变元素的样式或位置。具体步骤如下: 1. 在Vue的组件中,通过`mounted`钩子函数监听页面滚动事件: ``` mounted() { window.addEventListener('scroll', this.handleScroll); } ``` 2. 在`methods`中定义`handleScroll`方法来处理滚动事件,并使用`scrollTop`属性获取页面滚动位置: ``` methods: { handleScroll() { const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; // 根据scrollTop的值来决定元素的样式或位置 // ... } } ``` 3. 在模板中,使用动态绑定的方式来设置元素的样式或位置。可以通过绑定CSS类名或行内样式实现,以下是两种常见的实现示例: - 使用CSS类名: 在data中定义一个`isFixed`变量,根据滚动位置的条件来切换`isFixed`的值,在模板中绑定CSS类名根据`isFixed`的值设置元素的样式: ``` data() { return { isFixed: false, }; }, methods: { handleScroll() { const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; // 根据scrollTop的值来判断是否满足条件,如果满足条件则设置isFixed为true,否则为false this.isFixed = scrollTop > 100; }, }, ``` 在模板中绑定CSS类名,并设置元素的样式: ``` <div :class="{ 'fixed': isFixed }">固定的元素</div> <style> .fixed { position: fixed; top: 0; left: 0; } </style> ``` - 使用行内样式: 直接通过绑定`style`属性来设置元素的样式: ``` data() { return { topPosition: 0, }; }, methods: { handleScroll() { const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; // 根据scrollTop的值来判断是否满足条件,如果满足条件则设置topPosition为固定位置,否则为0 this.topPosition = scrollTop > 100 ? '100px' : '0'; }, }, ``` 在模板中绑定行内样式: ``` <div :style="{ top: topPosition }">固定的元素</div> ``` 通过以上步骤就可以实现元素吸顶固定位置的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值