第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

水平網(wǎng)站上的 scrollTop 或 scrollLeft?

水平網(wǎng)站上的 scrollTop 或 scrollLeft?

胡子哥哥 2021-08-26 17:46:32
我有一個我正在開發(fā)的網(wǎng)站(這是一個基本示例),昨天我得到了一些幫助來在單選按鈕導(dǎo)航上實現(xiàn)活動狀態(tài),我現(xiàn)在正在嘗試將其鏈接起來,以便它在頁面滾動時也會發(fā)生變化/當(dāng)當(dāng)前查看時它只是onClick。我大致知道如何實現(xiàn)這一點,因為我之前做過類似的事情,但后來我想到,因為頁面和滾動條被旋轉(zhuǎn)以適應(yīng)水平效果,我不知道它現(xiàn)在是 scrollTop 還是 scrollLeft。我以前從未使用過 scrollLeft,所以我不確定如何正確使用它。我想知道是否有人以前實現(xiàn)過類似的東西,正確的方法是什么?我已經(jīng)嘗試了兩者,但似乎沒有任何效果。這就是我大致想要實現(xiàn)的目標(biāo)(但一次只有一個課程處于活動狀態(tài))。我想也許使用 Waypoints 可能是另一種選擇,但同樣很難在網(wǎng)上找到任何內(nèi)容來解釋當(dāng)網(wǎng)站多次旋轉(zhuǎn)時這是如何工作的。我的 JS 知識不穩(wěn)定(仍在學(xué)習(xí)?。?,我只是想實現(xiàn)我認(rèn)為可行的方法,所以這可能甚至不正確,任何理解我做錯了什么的幫助將不勝感激!// --- 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 testfunction 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");        }    });}
查看完整描述

2 回答

?
瀟瀟雨雨

TA貢獻(xiàn)1833條經(jīng)驗 獲得超4個贊

對于水平滾動,我會走以下路線,簡化您的 HTML 和您收聽的內(nèi)容。由于觸摸設(shè)備只需輕掃即可輕松滾動,您所需要做的就是讓有滾輪的人可以訪問它。您也可以添加動畫,但這會使此代碼段太長。


const main = document.querySelector( 'main' );

const nav = document.querySelector( 'nav' );


let scrollend;


function onwheel(){

  

  /* 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. */

  

  event.preventDefault();

  event.stopImmediatePropagation();

  

  main.scrollLeft += event.wheelDeltaY;

  

}

function onscroll(){

  

  /* 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. */

  

  const current = Array.from( main.children ).find(child => {

  

      return child.offsetLeft >= main.scrollLeft - innerWidth / 2;

      

  });

  const link = Array.from( nav.children ).reduce((find, child) => {

    

    child.classList.remove( 'selected' );

    

    return find || (child.href.indexOf( current.id ) >= 0 ? child : find);

    

  }, false);

  

  if( link ) link.classList.add( 'selected' );

  

  clearTimeout( scrollend );

  scrollend = setTimeout( onscrollend, 100 );

  

}

function onscrollend(){

  

  /* After scrolling ends, snap the appropriate element. This could be done with an animation. */

  clearTimeout( scrollend );

  

  const current = Array.from( main.children ).find(child => {

  

      return child.offsetLeft >= main.scrollLeft - innerWidth / 2;

      

  });

  

  main.scrollLeft = current.offsetLeft;

  

}


/* Bind and initial call */


main.addEventListener( 'wheel', onwheel );

main.addEventListener( 'scroll', onscroll );


onscroll();

html,

body,

main {

    height: 100%;

}

body {

    padding: 0;

    margin: 0;

}

main {

    display: flex;

    overflow: auto;

    width: 100%;

    height: 100%;

    scroll-snap-type: x proximity;

}

main section {

    width: 100%;

    height: 100%;

    flex: 0 0 100%;

}

nav {

    position: fixed;

    bottom: 0;

    left: 0;

    width: 100%;

    display: flex;

    justify-content: center;

    align-items: center;

}

nav a {

    width: 1em;

    height: 1em;

    margin: 1em;

    display: block;

    overflow: hidden;

    color: transparent;

    border: 1px solid black;

    border-radius: 50%;

}

nav a.selected {

    background: black;

}


.bland { background: gray; }

.dark { background: darkgray; color: white; }

.bright { background: yellow; }

<nav>

  

  <a href="#section-1">Section 1</a>

  <a href="#section-2">Section 2</a>

  <a href="#section-3">Section 3</a>

  

</nav>


<main>

   

   <section class="bright" id="section-1">

      <h2>Section 1</h2>

   </section>

   

   <section class="dark" id="section-2">

      <h2>Section 2</h2>

   </section>

   

   <section class="bland" id="section-3">

      <h2>Section 3</h2>

   </section>

  

</main>


查看完整回答
反對 回復(fù) 2021-08-26
  • 2 回答
  • 0 關(guān)注
  • 276 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號