【问题标题】:locomotive-scroll toggle header onscroll机车滚动切换标题 onscroll
【发布时间】:2021-03-01 23:00:48
【问题描述】:

我是第一次尝试 locomotive-scroll,到目前为止它一直很好,直到我不得不尝试切换标题样式 onscroll,因为事件监听器不起作用。我研究了 gsap 滚动触发器和交叉点观察器,但我很难弄清楚,因为我对该领域知之甚少。

我怎样才能使这项工作/在机车卷轴上做这样的事情?

let header = document.querySelector("header");

function toggleHeader(ev) {
  if (window.pageYOffset > 300) {
    header.classList.add("header--active");
  } else {
    header.classList.remove("header--active");
  }
}
window.addEventListener('scroll', function() {
  toggleHeader();
});

HTML/CSS/JS

<div data-scroll-container=''>
    <div class='header'>
      Header
    </div>
    <section data-scroll-section=''></section>
    <section data-scroll-section=''></section>
  </div>
.header {
  height: 80px;
  background: #f00;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  position: fixed;
  width: 100%;
  left: 0;
  top: 0;
  transition: ease 0.4s;
}

.header--active {
  height: 40px;
  background-color: #000;
}

section {
  height: 500px;
  border-bottom: 1px solid #f00;
}
const scroller = new LocomotiveScroll({
  el: document.querySelector('[data-scroll-container]'),
  smooth: true
})

【问题讨论】:

    标签: javascript html locomotive-scroll


    【解决方案1】:

    一个功能样本:

    var el = document.getElementById("test");
    var wheel = "nothing";
    var scroll = new LocomotiveScroll({
      el: el,
      smooth: true,
      repeat: true,
      getDirection: true
    });
    
    scroll.on("call", (fun, dir, obj) => {
    
      console.log("call", fun, dir);
    
    });
    
    scroll.on("scroll", (obj) => {
      if (obj.direction == wheel) return;
      wheel = obj.direction;
    
      console.log("scroll--", obj.direction);
      console.log(obj.currentElements['0'],obj.currentElements['1'], Object.keys(obj.currentElements));
    
    });
    <script src="https://cdn.jsdelivr.net/npm/locomotive-scroll@4.1.0/dist/locomotive-scroll.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/locomotive-scroll@4.1.0/dist/locomotive-scroll.min.css">
    
    <div id="test" data-scroll-container=''>
      <div class='header'>
        Header
      </div>
      <section data-scroll-section=''>
        <div data-toggle data-toggle-call="text1">
    
          <h2 class="intro__title" data-scroll data-scroll-call="text0">If I scroll to the second intro section (AND the first intro is in the viewport) it will change to a black background. But if I scroll up it will not change to a white background as the section did not get out from the viewport and the last call
            was with a black background.</h2>
    
          <p>You have to completely pass through a section to be able to reverse back the colors.<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></p>
    
        </div>
    
        <div data-toggle data-toggle-call="text2">
          <h2 class="intro__title" data-scroll data-scroll-call="text1">If I scroll to the second intro section (AND the first intro is in the viewport) it will change to a black background. But if I scroll up it will not change to a white background as the section did not get out from the viewport and the last call
            was with a black background.</h2>
    
          <p>You have to completely pass through a section to be able to reverse back the colors.<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></p>
        </div>
      </section>
    </div>

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-21
      • 2021-05-23
      • 2019-11-19
      • 2020-11-12
      相关资源
      最近更新 更多