亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

當相應部分在屏幕上時,將活動類添加到 li

當相應部分在屏幕上時,將活動類添加到 li

寶慕林4294392 2022-10-08 10:02:39
我只有一個頁面。我的頁面上至少有四個部分。active當滾動到相應部分的底部時,我必須將類添加到菜單上的 li 標記中。例如,如果屏幕上有關于我們的部分,則將活動類添加到 li 標簽。如果畫廊部分在屏幕上,則添加活動類并從前一個 li 中刪除活動類。我必須更改顏色并將底部邊框添加到活動菜單列表中。我不想使用任何插件。這是一個例子。它正在添加活動類并更改導航列表的顏色 https://blackrockdigital.github.io/startbootstrap-scrolling-nav/可以使用 jQuery 嗎?$(function() {  $('.smothscrollclass a[href*="#"]:not([href="#"])').click(function() {    if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {      var target = $(this.hash);      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');      if (target.length) {        $('html, body').animate({          scrollTop: target.offset().top - 100        }, 1000);        return false;      }    }  });});body {  margin: 0;  padding: 0;}.headerMenu {  position: fixed;  top: 0;  width: 100%;  background-color: #d9a6a6;}.anchorLinks ul {  list-style: none;}.anchorLinks ul li {  display: inline-block;  margin: 15px;}.anchorLinks ul li a {  color: #fff;  text-decoration: none;  font-size: 18px;}.anchorLinks ul li.active a {  color: #ffef00;  border-bottom: 1px solid #000;}.WrapperInner {  margin-top: 80px;  margin-bottom: 80px;}section {  height: 400px;  padding: 40px;}section p {  color: #fff;}.aboutus {  background-color: #ec7063;}.service {  background-color: #a569bd;}.gallery {  background-color: #5dade2;}.contactus {  background-color: #2ecc71;}<div class="Wrapper">  <div class="mainInner bg_white">    <div class="linkWrappers">      <header class="headerMenu">        <div class="anchorLinks smothscrollclass">          <ul>            <li><a href="">Home</a></li>            <li><a href="#aboutus">About</a></li>            <li><a href="#service">Service</a></li>            <li><a href="#gallery">Gallery</a></li>            <li><a href="#contactus">Contact</a></li>          </ul>        </div>      </header>    </div>
查看完整描述

3 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

嗨,跟進下面的代碼片段,我在這里所做的是一個名為activateLink的函數,我為鏈接提供了 ID,然后單擊鏈接我在 jquery 的幫助下向該特定 li 添加了一個活動類


$(function() {

  $('.smothscrollclass a[href*="#"]:not([href="#"])').click(function() {

    if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {

      var target = $(this.hash);

      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');

      if (target.length) {

        $('html, body').animate({

          scrollTop: target.offset().top - 100

        }, 1000);

        return false;

      }

    }

  });

});


function activateLink (link,section) {

if(section){

    $('html, body').animate({

        'scrollTop' : $("#"+section).position().top

    },function(){    

       $( ".active" ).removeClass( "active" )

       $("#"+link).addClass("active");

    });

}else{

 $( ".active" ).removeClass( "active" )

 $("#"+link).addClass("active")

 

}

 

}


function checkSectionExistance (sectionId,linkId) {

 var element = document.querySelector('#'+sectionId);

  var position = element.getBoundingClientRect();


// checking for partial visibility

  if(position.top < window.innerHeight && position.bottom >= 0) {

      $( ".active" ).removeClass( "active" )

 $("#"+linkId).addClass("active")

  }

}


window.addEventListener('scroll', function() {

  

  checkSectionExistance('aboutus','about-link')

  checkSectionExistance('service','service-link')

  checkSectionExistance('gallery','gallery-link')

  checkSectionExistance('contactus','contact-link')

  

     

  

  

});

body {

  margin: 0;

  padding: 0;

}


.headerMenu {

  position: fixed;

  top: 0;

  width: 100%;

  background-color: #d9a6a6;

}


.anchorLinks ul {

  list-style: none;

}


.anchorLinks ul li {

  display: inline-block;

  margin: 15px;

}


.anchorLinks ul li a {

  color: #fff;

  text-decoration: none;

  font-size: 18px;

}


.anchorLinks ul li.active a {

  color: #ffef00;

  border-bottom: 1px solid #000;

}


.WrapperInner {

  margin-top: 80px;

  margin-bottom: 80px;

}


section {

  height: 400px;

  padding: 40px;

}


section p {

  color: #fff;

}


.aboutus {

  background-color: #ec7063;

}


.service {

  background-color: #a569bd;

}


.gallery {

  background-color: #5dade2;

}


.contactus {

  background-color: #2ecc71;

}

a{

cursor:pointer

}

.active {

border-bottom:1px solid white

}

<div class="Wrapper">

  <div class="mainInner bg_white">




    <div class="linkWrappers">

      <header class="headerMenu">

        <div class="anchorLinks smothscrollclass">

          <ul>

            <li><a id='home-link' onclick="activateLink('home-link')">Home</a></li>

            <li><a id='about-link' onclick="activateLink('about-link','aboutus')" >About</a></li>

            <li><a id='service-link' onclick="activateLink('service-link','service')" >Service</a></li>

            <li><a id='gallery-link' onclick="activateLink('gallery-link','gallery')" >Gallery</a></li>

            <li><a id='contact-link' onclick="activateLink('contact-link','contactus')" >Contact</a></li>

          </ul>

        </div>

      </header>


    </div>


    <div class="WrapperInner ">

      <section class="aboutus" id="aboutus">

        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute

          irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>


      </section>

      <section class="service" id="service">

        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute

          irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>



      </section>

      <section class="gallery" id="gallery">

        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute

          irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>


      </section>

      <section class="contactus" id="contactus">

        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute

          irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>


      </section>

    </div>


  </div>

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>


查看完整回答
反對 回復 2022-10-08
?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

使用純js,您需要使用document.body.scrollTop;來定義滾動了多少,然后從所有li-s中刪除活動樣式并添加到當前或僅手動為每個刪除前一個,for(let i = 0;i<liClass.length;i++){liClass[i].classList.remove("active")}然后通過ID或唯一類添加到當前定位它,工作正常對我來說 jQuery 是一樣的$("body").scrollTop()



查看完整回答
反對 回復 2022-10-08
?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

我升級了一點,現在一切都清晰了;)


對于滾動,我使用純 html 只需將 ID 添加到要滾動并添加的元素<a href="#theIdOfElement">


你問的我用 jQuery 做的課程


第二次編輯:


let  x=0;

$(document).ready(()=>{

  $("a").click((e)=>{

    //Removes class active from all a-s and all divs

    $("a").removeClass("active");

    $("div").removeClass("active");

    //Adds only to clicked one

    $(e.target).addClass("active");

    

    //Adds class active to divs that contain the text on which you clicked

    $("div:contains("+ $(e.target).text() +")").addClass("active");

  });

$(document).scroll(()=>{

    $("p").text( $(document).scrollTop());

        if($(document).scrollTop() >= 970){

        //If you want to get style permenent remove line below

    $("div").removeClass("active");

    $("#contacts").addClass("active");

  }

      else if($(document).scrollTop() >= 508){

     //If you want to get style permenent remove line below

    $("div").removeClass("active");

    $("#about").addClass("active");

  }

  else if($(document).scrollTop() >= 8){

     //If you want to get style permenent remove line below

    $("div").removeClass("active");

    $("#home").addClass("active");

  }



  });

})

/*This is for smooth scrolling*/

html {

  scroll-behavior: smooth;

}

.big {

  height: 500px;

  width: 100%;

  border: solid black 1px;

}

.active {

color:red;

}

<div id="main">

<div style="position:fixed;display:flex;justify-content:flex-end;width:100%">

  <p id="demo">0</p>

<a href="#home">Home</a>

<a href="#about">About Us</a>

<a href="#contacts">Contacts</a>

</div>

<div class="big" id ="home">Home</div>

<div class="big" id ="about">About Us</div>

<div class="big" id ="contacts">Contacts</div>

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

 

查看完整回答
反對 回復 2022-10-08
  • 3 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號