【问题标题】:Traversing back to parent and go to all its child element with ($this)遍历父元素并使用 ($this) 转到其所有子元素
【发布时间】:2019-01-11 19:31:56
【问题描述】:

我想使用 Jquery 根据用户滚动的距离在我的锚链接上添加类。我能够添加类,但不会使用removeClass 将其删除。我确定问题是我在 jquery 中使用的选择器。我是否需要遍历并从父元素添加特定的选择器,而不是直接在我的锚链接上分配removeClass

我宁愿将类应用到锚链接本身,而不是向列表元素添加类。

HTML

<ul>
    <li><a class="scroll" href="#first">About Me</a></li>
    <li><a class="scroll" href="#second">Portfolio</a></li>
    <li><a class="scroll" href="#third">Contact</a></li>
</ul>

<div class="home" style="height:1000px; background-color:red;"></div>
<div class="about-me" id="first" style="height:1000px; background-color:green;"></div>
<div class="portfolio" id="second" style="height:1000px; background-color:blue;"></div>
<div class="contact" id="third" style="height:1000px; background-color:orange;"></div>

CSS

 .active {
  color:gray;
  }

JQUERY

$(document).ready(function(){
var scrollLink = $('.scroll');

scrollLink.click(function(event){
    event.preventDefault();
    $('body,html').animate({
        scrollTop: $(this.hash).offset().top
    }, 1000)
})

$(window).scroll(function(){
    var scrollLoc = $(this).scrollTop();

    scrollLink.each(function(){

        var traverse = $(this.hash).offset().top - 20;

        if (traverse <= scrollLoc){
            $(this).addClass('active');
            $(this).removeClass('active');
        }
    })
})

})  

我希望其他锚链接中的类在屏幕上未显示时会被删除。

【问题讨论】:

  • 我会亲自从所有元素中删除该类,然后将其添加到相关的元素中,但下面的@KevinKoobs 提供了一个干净的替代方案。

标签: jquery html css


【解决方案1】:

我想你可能想更改以下代码

if (traverse <= scrollLoc){
   $(this).addClass('active');
    $(this).removeClass('active');
}

变成这样:

if (traverse <= scrollLoc){
   $(this).addClass('active');
} else { 
    $(this).removeClass('active');
}

【讨论】:

  • 是的,我确实忘记写一个 else 语句。这以某种方式起到了作用,但不是我期望的那样。正在添加课程,但我打算仅根据您在页面部分的位置将课程应用于一个锚链接。谢谢你的想法。
【解决方案2】:

您需要调整条件以获得下限和上限。由于您的元素具有相同的高度,因此应该很容易。

$(document).ready(function() {
  var scrollLink = $('.scroll');

  scrollLink.click(function(event) {
    event.preventDefault();
    $('body,html').animate({
      scrollTop: $(this.hash).offset().top
    }, 1000)
  })

  $(window).scroll(function() {
    var scrollLoc = $(this).scrollTop();

    scrollLink.each(function() {

      var traverse = $(this.hash).offset().top - 20;

      if (traverse <= scrollLoc && traverse + 1000 >= scrollLoc ) {
        $(this).addClass('active');
      } else {
        $(this).removeClass('active');
      }
    })
  })

});
.active {
  color: gray;
}

ul {
  position: fixed;
  background:#fff;
  padding:5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li><a class="scroll" href="#first">About Me</a></li>
  <li><a class="scroll" href="#second">Portfolio</a></li>
  <li><a class="scroll" href="#third">Contact</a></li>
</ul>

<div class="home" style="height:1000px; background-color:red;"></div>
<div class="about-me" id="first" style="height:1000px; background-color:green;"></div>
<div class="portfolio" id="second" style="height:1000px; background-color:blue;"></div>
<div class="contact" id="third" style="height:1000px; background-color:orange;"></div>

如果高度不一样,你可以这样做:

$(document).ready(function() {
  var scrollLink = $('.scroll');

  scrollLink.click(function(event) {
    event.preventDefault();
    $('body,html').animate({
      scrollTop: $(this.hash).offset().top
    }, 1000)
  })

  $(window).scroll(function() {
    var scrollLoc = $(this).scrollTop();

    scrollLink.each(function() {

      var traverse = $(this.hash).offset().top - 20;
      var traverse_up = $(this.hash).offset().top - 20 + $(this.hash).height();

      if (traverse <= scrollLoc && traverse_up >= scrollLoc ) {
        $(this).addClass('active');
      } else {
        $(this).removeClass('active');
      }
    })
  })

});
.active {
  color: gray;
}

ul {
  position: fixed;
  background:#fff;
  padding:5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li><a class="scroll" href="#first">About Me</a></li>
  <li><a class="scroll" href="#second">Portfolio</a></li>
  <li><a class="scroll" href="#third">Contact</a></li>
</ul>

<div class="home" style="height:200px; background-color:red;"></div>
<div class="about-me" id="first" style="height:1000px; background-color:green;"></div>
<div class="portfolio" id="second" style="height:800px; background-color:blue;"></div>
<div class="contact" id="third" style="height:900px; background-color:orange;"></div>

【讨论】:

  • 是的,我的工作条件是我认为需要 2-3 天才能弄清楚。这是解决方案!谢谢!!你的逻辑真棒!
猜你喜欢
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
相关资源
最近更新 更多