【问题标题】:Changing CSS parameters based on scroll position根据滚动位置更改 CSS 参数
【发布时间】:2019-11-18 05:30:36
【问题描述】:

在下面的示例中,我有两个不可见的按钮——右半部分的按钮,单击时水平滚动到下一部分,左半部分的按钮到上一部分。当用户将鼠标悬停在按钮上以显示滚动方向时,按钮会显示左右箭头光标。

scrollPosition === 0 时,我想将goToPreviousSectionButtoncursor 切换为default,当scrollPosition === maxScrollPosition 时,goToNextSectionButton 也一样。

说白了:运行代码 sn-p 时,将鼠标光标悬停在数字 1 的左侧。您可以看到它现在是一个左箭头光标。我希望它是一个默认光标,因为你不能向左走,因为你在开始。当你到达数字 3 时也是如此,但这次是右箭头,因为你不能再往前走了,所以显示右箭头是没有意义的。

我无法让它工作。任何帮助将不胜感激。


let scrollPosition = 0
const maxScrollPosition = document.body.scrollWidth - window.innerWidth

const goToPreviousSectionButton = document.createElement("button")
const goToNextSectionButton = document.createElement("button")

document.body.appendChild(goToPreviousSectionButton)
document.body.appendChild(goToNextSectionButton)

if (scrollPosition === 0) {
  // If scroll position is at the beginning
}

if (scrollPosition === maxScrollPosition) {
  // If scroll position at the end
}

goToPreviousSectionButton.addEventListener("click", () => {
  window.scrollBy(-window.innerWidth, 0)
})

goToNextSectionButton.addEventListener("click", () => {
  window.scrollBy(window.innerWidth, 0)
})
* {
  margin: 0;
  padding: 0
}

html { height: 100% }

html, body, main {
  display: flex;
  flex-grow: 1
}

section {
  display: grid;
  place-items: center;
  flex: 1 0 100%
}

section:nth-of-type(1) { background: orange }
section:nth-of-type(2) { background: limeGreen }
section:nth-of-type(3) { background: royalBlue }

h2 { color: white }

button {
  background: transparent;
  position: fixed;
  top: 0;
  width: 50%;
  height: 100%;
  border: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent
}

:focus { outline: none }

button:nth-of-type(1) { /* Left button */
  left: 0;
  cursor: w-resize
}
button:nth-of-type(2) { /* Right button */
  right: 0;
  cursor: e-resize
}
<main>
  <section>
    <h2>1</h2>
  </section>
  <section>
    <h2>2</h2>
  </section>
  <section>
    <h2>3</h2>
  </section>
</main>

【问题讨论】:

    标签: javascript css ecmascript-6 scroll toggle


    【解决方案1】:

    一个想法是使用伪元素添加额外的元素,您将覆盖不可见的按钮并禁用对它们的操作:

    const goToPreviousSectionButton = document.createElement("button")
    const goToNextSectionButton = document.createElement("button")
    
    document.body.appendChild(goToPreviousSectionButton)
    document.body.appendChild(goToNextSectionButton)
    
    goToPreviousSectionButton.addEventListener("click", () => {
      window.scrollBy(-window.innerWidth, 0)
    })
    
    goToNextSectionButton.addEventListener("click", () => {
      window.scrollBy(window.innerWidth, 0)
    })
    * {
      margin: 0;
      padding: 0
    }
    
    html,body { height: 100% }
    
    body, main {
      display: flex;
      flex-grow: 1
    }
    
    section {
      display: grid;
      place-items: center;
      flex: 1 0 100%
    }
    
    section:nth-of-type(1) { background: orange }
    section:nth-of-type(2) { background: limeGreen }
    section:nth-of-type(3) { background: royalBlue }
    
    h2 { color: white }
    
    button {
      background: transparent;
      position: fixed;
      top: 0;
      width: 50%;
      height: 100%;
      border: none;
      touch-action: manipulation;
      -webkit-tap-highlight-color: transparent
    }
    
    :focus { outline: none }
    
    button:nth-of-type(1) { /* Left button */
      left: 0;
      cursor: w-resize
    }
    button:nth-of-type(2) { /* Right button */
      right: 0;
      cursor: e-resize
    }
    
    /* Added */
    main:before,
    main:after{
      content:"";
      z-index:9;
      position:relative;
      flex: 1 0 50%;
    }
    main:before {
      margin-right:-50%;
    }
    main:after {
      margin-left:-50%;
    }
    /**/
    <main>
      <section>
        <h2>1</h2>
      </section>
      <section>
        <h2>2</h2>
      </section>
      <section>
        <h2>3</h2>
      </section>
    </main>

    【讨论】:

    • 这是我想到的解决方案之一,但我认为这个问题可以更优雅地解决。谢谢!
    • @Tzar 一个 CSS 解决方案,即使它有点 hacky(我猜)总是比 JS 更好,因为它可以让你的 JS 代码更小并避免出现错误
    • 你是对的。确实如此。现在我只想知道为什么我在寻找 JavaScript 解决方案时遇到问题。
    • @Tzar 您的代码的问题是您需要在 evenlistener 中执行您的逻辑。每次单击时,您都会获得滚动位置并应用您的 if 逻辑
    【解决方案2】:
    if (scrollPosition === 0) {
      goToPreviousSectionButton.classList.add('default')
    }
    
    if (scrollPosition === maxScrollPosition) {
      goToPreviousSectionButton.classList.add('default')
    }
    
    css:
    
    .default {
     cursor: default
    }
    

    【讨论】:

    • 然后在滚动更改时以相同的方式删除该类
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    相关资源
    最近更新 更多