简体   繁体   English

重新排列元素时将活动类添加到锚点

[英]Adding active class to anchor when elements are rearranged

I've 4 divs and their relative anchor tags.我有 4 个 div 及其相关的锚标签。 Active class is being added to anchors on scroll when the order of the display elements is not changed.当显示元素的顺序没有改变时,活动类被添加到滚动的锚点。 When the order of the elements changes, active classes are only being added to the sorted elements.当元素的顺序发生变化时,活动类只会被添加到已排序的元素中。 I sorted nav anchors too based on the order.我也根据顺序对导航锚进行了排序。 Active class is still not being added to anchors based on the scroll of the element.活动类仍未根据元素的滚动添加到锚点。

Please help me add an active class to anchors based on scroll when nav and elements have the "order" attribute on them.当导航和元素具有“顺序”属性时,请帮助我根据滚动向锚点添加一个活动类。 Thanks!谢谢!

Below is the jsfiddle for the code: http://jsfiddle.net/n2yds3u9/16/下面是代码的jsfiddle:http: http://jsfiddle.net/n2yds3u9/16/

<section id="main">
  <nav>
        <a href="#1" class="active anchors" class="anchors">Punkt 1</a>
        <a href="#2" class="anchors order1">Punkt 2</a>
        <a href="#3" class="anchors">Punkt 3</a>
        <a href="#4" class="anchors order2">Punkt 4</a>
    </nav>
    <div class="target" id="1">TARGET 1
    </div>
    <div class="target order1" id="2">TARGET 2</div>
    <div class="target" id="3">TARGET 3</div>
    <div class="target order2" id="4">TARGET 4</div>
</section>

* {
    margin: 0;
    padding: 0;
}

#main {
    width: 75%;
    float: right;
    display: flex;
    flex-direction: column;
}

#main div.target {
    background: #ccc;
    height: 400px;
}

#main div.target:nth-child(even) {
    background: #eee;
}

nav {
    width: 25%;
    position: relative;
    position: fixed;
    display: flex;
    flex-direction: column;
}


nav a {
    border-bottom: 1px solid #666;
    color: #333;
    display: block;
    padding: 10px;
    text-align: center;
    text-decoration: none;
    order: 999;
}

nav a .anchors{
  
}


nav a:hover, nav a.active {
    background: #666;
    color: #fff;
}
.target{
  order: 999;
}
.target.order1{
  order: 1;
}
.target.order2{
  order: 2;
}

a.anchors.order1{
  order: 1;
}
a.anchors.order2{
  order: 2;
}

$('#nav nav a').on('click', function(event) {
    $(this).parent().find('a').removeClass('active');
    $(this).addClass('active');
});

$(window).on('scroll', function() {
    $('.target').each(function() {
        if($(window).scrollTop() >= $(this).offset().top) {
            var id = $(this).attr('id');
            $('nav a').removeClass('active');
            $('nav a[href=#'+ id +']').addClass('active');
        }
    });
});

Since anchors are sorted via css, they are not changed in the DOM, hens javascript is not aware of their visible position.由于锚点是通过 css 排序的,它们在 DOM 中没有改变,母鸡 javascript 不知道它们的可见位置。 So, one solution is to create a list of ordered elements and loop through them:因此,一种解决方案是创建一个有序元素列表并遍历它们:

 const anchors = getOrder("#main > nav > a"), targets = getOrder(".target") $('#nav nav a').on('click', function(event) { $(this).parent().find('a').removeClass('active'); $(this).addClass('active'); }); $(window).on('scroll', function() { const top = $(window).scrollTop(), height = $(window).height() / 2; for(let i = targets.length-1, active, found; i > -1; i--) { active = !found && top >= $(targets[i]).offset().top - height; anchors[i].classList.toggle("active", active); if (active) found = true; } }).trigger("scroll"); //initialize function getOrder(query) { return Array.from(document.querySelectorAll(query)).reduce((a,b,i) => { const order = +getComputedStyle(b).order; a[order == 999 ? a.length + order : order] = b; return a; }, []).filter(a => a !== undefined); }
 * { margin: 0; padding: 0; } #main { width: 75%; float: right; display: flex; flex-direction: column; } #main div.target { background: #ccc; height: 100vh; } #main div.target:nth-child(even) { background: #eee; } nav { width: 25%; position: relative; position: fixed; display: flex; flex-direction: column; } nav a { border-bottom: 1px solid #666; color: #333; display: block; padding: 10px; text-align: center; text-decoration: none; order: 999; } nav a .anchors{ } nav a:hover, nav a.active { background: #666; color: #fff; } .target{ order: 999; } .target.order1{ order: 1; } .target.order2{ order: 2; } a.anchors.order1{ order: 1; } a.anchors.order2{ order: 2; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <section id="main"> <nav> <a href="#1" class="active anchors" class="anchors">Punkt 1</a> <a href="#2" class="anchors order1">Punkt 2</a> <a href="#3" class="anchors">Punkt 3</a> <a href="#4" class="anchors order2">Punkt 4</a> <a href="#5" class="anchors">Punkt 5</a> <a href="#6" class="anchors">Punkt 6</a> </nav> <div class="target" id="1">TARGET 1 </div> <div class="target order1" id="2">TARGET 2</div> <div class="target" id="3">TARGET 3</div> <div class="target order2" id="4">TARGET 4</div> <div class="target" id="5">TARGET 5</div> <div class="target" id="6">TARGET 6</div> </section>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM