猿问

水平网站上的 scrollTop 或 scrollLeft?

我有一个我正在开发的网站(这是一个基本示例),昨天我得到了一些帮助来在单选按钮导航上实现活动状态,我现在正在尝试将其链接起来,以便它在页面滚动时也会发生变化/当当前查看时它只是onClick。

我大致知道如何实现这一点,因为我之前做过类似的事情,但后来我想到,因为页面和滚动条被旋转以适应水平效果,我不知道它现在是 scrollTop 还是 scrollLeft。我以前从未使用过 scrollLeft,所以我不确定如何正确使用它。我想知道是否有人以前实现过类似的东西,正确的方法是什么?我已经尝试了两者,但似乎没有任何效果。这就是我大致想要实现的目标(但一次只有一个课程处于活动状态)。

我想也许使用 Waypoints 可能是另一种选择,但同样很难在网上找到任何内容来解释当网站多次旋转时这是如何工作的。

我的 JS 知识不稳定(仍在学习!),我只是想实现我认为可行的方法,所以这可能甚至不正确,任何理解我做错了什么的帮助将不胜感激!

// --- change span classes on click 


const setIconState = (icon, state) => icon.className = state

    ? icon.className.replace('button-off', 'button-on')

    : icon.className.replace('button-on', 'button-off')


const toggleIcon = element => {

  const className = element.className;

  element.className = className.indexOf('button-on') > -1

    ? setIconState(element, false)

    : setIconState(element, true);

}


const setIconActiveState = (icon, state) => icon.className = state

  ? icon.className = `${icon.className} active`

  : icon.className = icon.className.replace('active', '')


document.querySelectorAll('.bottomnav span.icon')

  .forEach(icon => {

    icon.onclick = (e) => {

      const {

        target: clickedSpan

      } = e;

      

      const siblings = [...clickedSpan.parentElement.parentElement.querySelectorAll('span.icon')]

        .filter(sibling => sibling != clickedSpan);


      siblings.forEach(icon => {

        setIconState(icon, false);

        setIconActiveState(icon, false);

      });

      setIconState(clickedSpan, true);

      setIconActiveState(clickedSpan, true);

    };

  });

  

// --- change span classes on scroll test


function onScroll(event){

    var scrollPos = $(document).scrollTop();

    $('.bottomnav a').each(function () {

        var currLink = $(this);

        var refElement = $(currLink.attr("href"));

        if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {

            currLink.addClass("active");

        }

        else{

            currLink.removeClass("active");

        }

    });

}


胡子哥哥
浏览 258回答 2
2回答

潇潇雨雨

对于水平滚动,我会走以下路线,简化您的 HTML 和您收听的内容。由于触摸设备只需轻扫即可轻松滚动,您所需要做的就是让有滚轮的人可以访问它。您也可以添加动画,但这会使此代码段太长。const main = document.querySelector( 'main' );const nav = document.querySelector( 'nav' );let scrollend;function onwheel(){&nbsp;&nbsp;&nbsp; /* When using the scrollwheel, translate Y direction scrolls to X direction. This way scrollwheel users get the benefit of scrolling down to go right, while touch and other users get default behaviour. */&nbsp;&nbsp;&nbsp; event.preventDefault();&nbsp; event.stopImmediatePropagation();&nbsp;&nbsp;&nbsp; main.scrollLeft += event.wheelDeltaY;&nbsp;&nbsp;}function onscroll(){&nbsp;&nbsp;&nbsp; /* When scrolling, find the nearest element to the center of the screen. Then find the link in the nav that links to it and activate it while deactivating all others. */&nbsp;&nbsp;&nbsp; const current = Array.from( main.children ).find(child => {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; return child.offsetLeft >= main.scrollLeft - innerWidth / 2;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; });&nbsp; const link = Array.from( nav.children ).reduce((find, child) => {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; child.classList.remove( 'selected' );&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return find || (child.href.indexOf( current.id ) >= 0 ? child : find);&nbsp; &nbsp;&nbsp;&nbsp; }, false);&nbsp;&nbsp;&nbsp; if( link ) link.classList.add( 'selected' );&nbsp;&nbsp;&nbsp; clearTimeout( scrollend );&nbsp; scrollend = setTimeout( onscrollend, 100 );&nbsp;&nbsp;}function onscrollend(){&nbsp;&nbsp;&nbsp; /* After scrolling ends, snap the appropriate element. This could be done with an animation. */&nbsp; clearTimeout( scrollend );&nbsp;&nbsp;&nbsp; const current = Array.from( main.children ).find(child => {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; return child.offsetLeft >= main.scrollLeft - innerWidth / 2;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; });&nbsp;&nbsp;&nbsp; main.scrollLeft = current.offsetLeft;&nbsp;&nbsp;}/* Bind and initial call */main.addEventListener( 'wheel', onwheel );main.addEventListener( 'scroll', onscroll );onscroll();html,body,main {&nbsp; &nbsp; height: 100%;}body {&nbsp; &nbsp; padding: 0;&nbsp; &nbsp; margin: 0;}main {&nbsp; &nbsp; display: flex;&nbsp; &nbsp; overflow: auto;&nbsp; &nbsp; width: 100%;&nbsp; &nbsp; height: 100%;&nbsp; &nbsp; scroll-snap-type: x proximity;}main section {&nbsp; &nbsp; width: 100%;&nbsp; &nbsp; height: 100%;&nbsp; &nbsp; flex: 0 0 100%;}nav {&nbsp; &nbsp; position: fixed;&nbsp; &nbsp; bottom: 0;&nbsp; &nbsp; left: 0;&nbsp; &nbsp; width: 100%;&nbsp; &nbsp; display: flex;&nbsp; &nbsp; justify-content: center;&nbsp; &nbsp; align-items: center;}nav a {&nbsp; &nbsp; width: 1em;&nbsp; &nbsp; height: 1em;&nbsp; &nbsp; margin: 1em;&nbsp; &nbsp; display: block;&nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; color: transparent;&nbsp; &nbsp; border: 1px solid black;&nbsp; &nbsp; border-radius: 50%;}nav a.selected {&nbsp; &nbsp; background: black;}.bland { background: gray; }.dark { background: darkgray; color: white; }.bright { background: yellow; }<nav>&nbsp;&nbsp;&nbsp; <a href="#section-1">Section 1</a>&nbsp; <a href="#section-2">Section 2</a>&nbsp; <a href="#section-3">Section 3</a>&nbsp;&nbsp;</nav><main>&nbsp; &nbsp;&nbsp; &nbsp;<section class="bright" id="section-1">&nbsp; &nbsp; &nbsp; <h2>Section 1</h2>&nbsp; &nbsp;</section>&nbsp; &nbsp;&nbsp; &nbsp;<section class="dark" id="section-2">&nbsp; &nbsp; &nbsp; <h2>Section 2</h2>&nbsp; &nbsp;</section>&nbsp; &nbsp;&nbsp; &nbsp;<section class="bland" id="section-3">&nbsp; &nbsp; &nbsp; <h2>Section 3</h2>&nbsp; &nbsp;</section>&nbsp;&nbsp;</main>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答