sidenav with dots scrolled
I asked similar question but didn't get an answer so I will try again. I have a single web page. In this page , I am trying to make a side navigation made out of Dots. Web page is broken to sections . I need to make it so every time I scroll to another section , the dot changes color. For example : First section - first dot is green, others are empty. Than if I go to 2nd section, the 2nd dot goes green and others are empty.
Here is the fiddle :
https://jsfiddle.net/c1d3tfnL/2/
My guess is that there is a problem somewhere in this part
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
console.log(theID);
if (panel.hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
but I am not sure . Instead of having only one dot colored , They are all green.
You will see the dots in the top of the page but as I have an internal database I can't post everything here . It is supposed to look like this :
I have done everything including that it is fixed . This is how it looks now.
Every circle is always green . This is making me crazy .
javascript jquery html sass
|
show 4 more comments
I asked similar question but didn't get an answer so I will try again. I have a single web page. In this page , I am trying to make a side navigation made out of Dots. Web page is broken to sections . I need to make it so every time I scroll to another section , the dot changes color. For example : First section - first dot is green, others are empty. Than if I go to 2nd section, the 2nd dot goes green and others are empty.
Here is the fiddle :
https://jsfiddle.net/c1d3tfnL/2/
My guess is that there is a problem somewhere in this part
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
console.log(theID);
if (panel.hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
but I am not sure . Instead of having only one dot colored , They are all green.
You will see the dots in the top of the page but as I have an internal database I can't post everything here . It is supposed to look like this :
I have done everything including that it is fixed . This is how it looks now.
Every circle is always green . This is making me crazy .
javascript jquery html sass
1
I believe it has something to do with the dot click function. Also keep in mind thatpanel
variable stores all elements with class..panels
so you might reconsider this ifpanel.hasClass('current')
– Mojo Allmighty
Nov 15 '18 at 8:44
@Mojo Allmighty I don't think I have any click event , if that's what you mean. But for this Variable , you think I shouldn't store it like that ? I don't think I understand clearly :(
– Bojan Kolano
Nov 15 '18 at 8:48
1
You can useif($('#'+theID).hasClass('current'))
– Mojo Allmighty
Nov 15 '18 at 8:57
1
The id must be unique, you are using it in multiple places.
– Mojo Allmighty
Nov 15 '18 at 9:04
1
Yeah, or use an attribute for that, something likesection-id="YourSectionID"
and select it in your first for. You'll need to modifyvar ahref = $(aChild).attr('id');
intovar ahref = $(aChild).attr('section-id');
– Mojo Allmighty
Nov 15 '18 at 9:16
|
show 4 more comments
I asked similar question but didn't get an answer so I will try again. I have a single web page. In this page , I am trying to make a side navigation made out of Dots. Web page is broken to sections . I need to make it so every time I scroll to another section , the dot changes color. For example : First section - first dot is green, others are empty. Than if I go to 2nd section, the 2nd dot goes green and others are empty.
Here is the fiddle :
https://jsfiddle.net/c1d3tfnL/2/
My guess is that there is a problem somewhere in this part
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
console.log(theID);
if (panel.hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
but I am not sure . Instead of having only one dot colored , They are all green.
You will see the dots in the top of the page but as I have an internal database I can't post everything here . It is supposed to look like this :
I have done everything including that it is fixed . This is how it looks now.
Every circle is always green . This is making me crazy .
javascript jquery html sass
I asked similar question but didn't get an answer so I will try again. I have a single web page. In this page , I am trying to make a side navigation made out of Dots. Web page is broken to sections . I need to make it so every time I scroll to another section , the dot changes color. For example : First section - first dot is green, others are empty. Than if I go to 2nd section, the 2nd dot goes green and others are empty.
Here is the fiddle :
https://jsfiddle.net/c1d3tfnL/2/
My guess is that there is a problem somewhere in this part
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
console.log(theID);
if (panel.hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
but I am not sure . Instead of having only one dot colored , They are all green.
You will see the dots in the top of the page but as I have an internal database I can't post everything here . It is supposed to look like this :
I have done everything including that it is fixed . This is how it looks now.
Every circle is always green . This is making me crazy .
javascript jquery html sass
javascript jquery html sass
asked Nov 15 '18 at 8:34
Bojan KolanoBojan Kolano
14210
14210
1
I believe it has something to do with the dot click function. Also keep in mind thatpanel
variable stores all elements with class..panels
so you might reconsider this ifpanel.hasClass('current')
– Mojo Allmighty
Nov 15 '18 at 8:44
@Mojo Allmighty I don't think I have any click event , if that's what you mean. But for this Variable , you think I shouldn't store it like that ? I don't think I understand clearly :(
– Bojan Kolano
Nov 15 '18 at 8:48
1
You can useif($('#'+theID).hasClass('current'))
– Mojo Allmighty
Nov 15 '18 at 8:57
1
The id must be unique, you are using it in multiple places.
– Mojo Allmighty
Nov 15 '18 at 9:04
1
Yeah, or use an attribute for that, something likesection-id="YourSectionID"
and select it in your first for. You'll need to modifyvar ahref = $(aChild).attr('id');
intovar ahref = $(aChild).attr('section-id');
– Mojo Allmighty
Nov 15 '18 at 9:16
|
show 4 more comments
1
I believe it has something to do with the dot click function. Also keep in mind thatpanel
variable stores all elements with class..panels
so you might reconsider this ifpanel.hasClass('current')
– Mojo Allmighty
Nov 15 '18 at 8:44
@Mojo Allmighty I don't think I have any click event , if that's what you mean. But for this Variable , you think I shouldn't store it like that ? I don't think I understand clearly :(
– Bojan Kolano
Nov 15 '18 at 8:48
1
You can useif($('#'+theID).hasClass('current'))
– Mojo Allmighty
Nov 15 '18 at 8:57
1
The id must be unique, you are using it in multiple places.
– Mojo Allmighty
Nov 15 '18 at 9:04
1
Yeah, or use an attribute for that, something likesection-id="YourSectionID"
and select it in your first for. You'll need to modifyvar ahref = $(aChild).attr('id');
intovar ahref = $(aChild).attr('section-id');
– Mojo Allmighty
Nov 15 '18 at 9:16
1
1
I believe it has something to do with the dot click function. Also keep in mind that
panel
variable stores all elements with class ..panels
so you might reconsider this if panel.hasClass('current')
– Mojo Allmighty
Nov 15 '18 at 8:44
I believe it has something to do with the dot click function. Also keep in mind that
panel
variable stores all elements with class ..panels
so you might reconsider this if panel.hasClass('current')
– Mojo Allmighty
Nov 15 '18 at 8:44
@Mojo Allmighty I don't think I have any click event , if that's what you mean. But for this Variable , you think I shouldn't store it like that ? I don't think I understand clearly :(
– Bojan Kolano
Nov 15 '18 at 8:48
@Mojo Allmighty I don't think I have any click event , if that's what you mean. But for this Variable , you think I shouldn't store it like that ? I don't think I understand clearly :(
– Bojan Kolano
Nov 15 '18 at 8:48
1
1
You can use
if($('#'+theID).hasClass('current'))
– Mojo Allmighty
Nov 15 '18 at 8:57
You can use
if($('#'+theID).hasClass('current'))
– Mojo Allmighty
Nov 15 '18 at 8:57
1
1
The id must be unique, you are using it in multiple places.
– Mojo Allmighty
Nov 15 '18 at 9:04
The id must be unique, you are using it in multiple places.
– Mojo Allmighty
Nov 15 '18 at 9:04
1
1
Yeah, or use an attribute for that, something like
section-id="YourSectionID"
and select it in your first for. You'll need to modify var ahref = $(aChild).attr('id');
into var ahref = $(aChild).attr('section-id');
– Mojo Allmighty
Nov 15 '18 at 9:16
Yeah, or use an attribute for that, something like
section-id="YourSectionID"
and select it in your first for. You'll need to modify var ahref = $(aChild).attr('id');
into var ahref = $(aChild).attr('section-id');
– Mojo Allmighty
Nov 15 '18 at 9:16
|
show 4 more comments
4 Answers
4
active
oldest
votes
The problem is selecting the sections. What you're doing right now is to take all elements with class panel
and try to see if it contains the class current
. Having if (panel.hasClass('current'))
will always return true because there will always be an element with class panel
who has that class therefor all the dots will be green. You need a new selector to match your a
with your sections.
<a section-id="hero" href="#hero" class="dot"><span class="hide">dot</span></a>
<a section-id="whole" href="#whole" class="dot"><span class="hide">dot</span></a>
<a section-id="split" href="#split" class="dot"><span class="hide">dot</span></a>
<a section-id="two-split" href="#two-split" class="dot"><span class="hide">dot</span></a>
<a section-id="three-split" href="#three-split" class="dot"><span class="hide">dot</span></a>
<a section-id="footer" href="#footer" class="dot"><span class="hide">dot</span></a>
As for your javascript code, you'll need to select the items with section-id
attribute and check if the associated ID from sections has class current
var aChildren = $('.side ul').children();
var aArray = ;
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('section-id');
aArray.push(ahref);
}
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
if($('#'+theID).hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
}
add a comment |
I don't know how is your html looks, here I am giving sample code as it work same as you want.
EDIT: Added on scroll too
$("ul li a").click(function(){
$current=$(this);
$("ul li").each(function(){
$(this).removeClass("current");
})
$current.parent().addClass("current");
});
var one_pos=$('#one').offset().top;
var two_pos=$('#two').offset().top;
var three_pos=$('#three').offset().top;
var four_pos=$('#four').offset().top;
var five_pos=$('#five').offset().top;
$(window).on('scroll', function() {
var current_pos = window.pageYOffset;
if(current_pos>one_pos && current_pos<two_pos){
$("ul li a[href='#one']").click();
}else if(current_pos>two_pos && current_pos<three_pos){
$("ul li a[href='#two']").click();
}else if(current_pos>three_pos && current_pos<four_pos){
$("ul li a[href='#three']").click();
}else if(current_pos>four_pos && current_pos<five_pos){
$("ul li a[href='#four']").click();
}else if(current_pos>four_pos){
$("ul li a[href='#five']").click();
}
})
div{
height:200px;
padding:2%;
border:1px solid;
}
ul{
position:fixed;
list-style:none;
}
ul li.current i{
color:green;
}
ul li i{
color:#ddd;
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="current"><a href="#one"><i class="fa fa-circle"></i></a></li>
<li><a href="#two"><i class="fa fa-circle"></i></a></li>
<li><a href="#three"><i class="fa fa-circle"></i></a></li>
<li><a href="#four"><i class="fa fa-circle"></i></a></li>
<li><a href="#five"><i class="fa fa-circle"></i></a></li>
</ul>
<div id="one">one</div>
<div id="two">two</div>
<div id="three">three</div>
<div id="four">four</div>
<div id="five">five</div>
<div>rest</div>
This is what i initially thought too but then the OP said he doesn't have anyclick
function so i think the dots will be colored on scroll not on click
– Mojo Allmighty
Nov 15 '18 at 9:21
Yeah this works great but i need this to work the same , but on scroll
– Bojan Kolano
Nov 15 '18 at 9:24
@BojanKolano updated code with on scroll trigger. I hope this will helps you.
– Dilip Belgumpi
Nov 15 '18 at 9:39
@DilipBelgumpi Thats even better ! This is a great solution but i need it to be not hard coded . This way if someone adds a new section, the nav wont work anymore :(
– Bojan Kolano
Nov 15 '18 at 9:43
add a comment |
OK so after some time and lots of help, this is the solution :
$(window).scroll(function(){
var cutoff = $(window).scrollTop();
panel.each(function(){
if($(this).offset().top + $(this).height() > cutoff){
panel.removeClass('current');
$(this).addClass('current');
return false;
}
})
var aChildren = $('.side ul').children();
var aArray = ;
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('section-id');
aArray.push(ahref);
}
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
if($('#'+theID).hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
}
Thanks to @Mojo Allmighty
add a comment |
What this does is "Loop through all a-elements, and color them if the panel has the class "current"".
You problem:
If the panel has class "current" color all a-elements green
What you want:
IF the panel has class current lookup the corresponding a-element (belonging to the panel) and addClass green.
So this does not check if the single a-element should get the class "greens" based on which panel is currently active ("current").
Without insight into the rest of the code you should/could do 1 of 2 things;
- Loop through the panels and add the class "Greens" to the corresponding a-element.
- Loop through the a-elements (as you're doing now), and check the corresponding panel element (the panel element that belongs to the a-element).
Depending on your code this might be as easy as:
$panel.find("a").addClass('greens');
---- edit ---
Although there's an accepted answer already you could do this with a somewhat easier to understand JS and HTML (without the loops and all).
On the panel you can add data-attributes with names that correspond to the a-elements you want to make green/active. When you are making the panel active you can also select the single a-element that corresponds to your panel (so panel with data-corresponding-link="hero" refers to the a-element with data-id="hero"), and just add the class green to it.
You can choose to first select all a-elements and remove the class green or select the single a-element which has the class green and remove that single one.
<panel class="hero current" data-corresponding-link="hero"></panel>
<panel class="other-panel" data-corresponding-link="other-panel"></panel>
<a data-id="hero" href="#hero" class="dot"><span class="show">dot</span></a>
<a data-id="other-panel" href="#three-split" class="dot"><span class="hide">dot</span></a>
js:
$(window).scroll(function(){
var cutoff = $(window).scrollTop();
panel.each(function(){
if($(this).offset().top + $(this).height() > cutoff){
panel.removeClass('current');
$(this).addClass('current');
$(".green").removeClass("green") //Select all the green classes and remove the class green (you could scope this further down to a.green or something).
$("data-id='"+panel.dataset.correspondingLink+"']").addClass("green") //based on the panels corresponding-link data-attribute select the element(s) that have this data-attribute data-id set to "hero" and addClass green
return false;
}
})
}
This is more of a comment, not an answer.
– Mojo Allmighty
Nov 15 '18 at 8:46
@sajoku This makes sense but my sidenav with dots is not in the sections . None of the sections have a . This sidenav is a separate thing on the page .
– Bojan Kolano
Nov 15 '18 at 8:50
@sajoku The 2nd thing , Looping through a-elements sounds very close to what I need . Could you write an example or something ?
– Bojan Kolano
Nov 15 '18 at 8:55
I added a way to do this without having to loop through all the a-elements.
– sajoku
Nov 16 '18 at 9:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315264%2fsidenav-with-dots-scrolled%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is selecting the sections. What you're doing right now is to take all elements with class panel
and try to see if it contains the class current
. Having if (panel.hasClass('current'))
will always return true because there will always be an element with class panel
who has that class therefor all the dots will be green. You need a new selector to match your a
with your sections.
<a section-id="hero" href="#hero" class="dot"><span class="hide">dot</span></a>
<a section-id="whole" href="#whole" class="dot"><span class="hide">dot</span></a>
<a section-id="split" href="#split" class="dot"><span class="hide">dot</span></a>
<a section-id="two-split" href="#two-split" class="dot"><span class="hide">dot</span></a>
<a section-id="three-split" href="#three-split" class="dot"><span class="hide">dot</span></a>
<a section-id="footer" href="#footer" class="dot"><span class="hide">dot</span></a>
As for your javascript code, you'll need to select the items with section-id
attribute and check if the associated ID from sections has class current
var aChildren = $('.side ul').children();
var aArray = ;
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('section-id');
aArray.push(ahref);
}
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
if($('#'+theID).hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
}
add a comment |
The problem is selecting the sections. What you're doing right now is to take all elements with class panel
and try to see if it contains the class current
. Having if (panel.hasClass('current'))
will always return true because there will always be an element with class panel
who has that class therefor all the dots will be green. You need a new selector to match your a
with your sections.
<a section-id="hero" href="#hero" class="dot"><span class="hide">dot</span></a>
<a section-id="whole" href="#whole" class="dot"><span class="hide">dot</span></a>
<a section-id="split" href="#split" class="dot"><span class="hide">dot</span></a>
<a section-id="two-split" href="#two-split" class="dot"><span class="hide">dot</span></a>
<a section-id="three-split" href="#three-split" class="dot"><span class="hide">dot</span></a>
<a section-id="footer" href="#footer" class="dot"><span class="hide">dot</span></a>
As for your javascript code, you'll need to select the items with section-id
attribute and check if the associated ID from sections has class current
var aChildren = $('.side ul').children();
var aArray = ;
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('section-id');
aArray.push(ahref);
}
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
if($('#'+theID).hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
}
add a comment |
The problem is selecting the sections. What you're doing right now is to take all elements with class panel
and try to see if it contains the class current
. Having if (panel.hasClass('current'))
will always return true because there will always be an element with class panel
who has that class therefor all the dots will be green. You need a new selector to match your a
with your sections.
<a section-id="hero" href="#hero" class="dot"><span class="hide">dot</span></a>
<a section-id="whole" href="#whole" class="dot"><span class="hide">dot</span></a>
<a section-id="split" href="#split" class="dot"><span class="hide">dot</span></a>
<a section-id="two-split" href="#two-split" class="dot"><span class="hide">dot</span></a>
<a section-id="three-split" href="#three-split" class="dot"><span class="hide">dot</span></a>
<a section-id="footer" href="#footer" class="dot"><span class="hide">dot</span></a>
As for your javascript code, you'll need to select the items with section-id
attribute and check if the associated ID from sections has class current
var aChildren = $('.side ul').children();
var aArray = ;
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('section-id');
aArray.push(ahref);
}
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
if($('#'+theID).hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
}
The problem is selecting the sections. What you're doing right now is to take all elements with class panel
and try to see if it contains the class current
. Having if (panel.hasClass('current'))
will always return true because there will always be an element with class panel
who has that class therefor all the dots will be green. You need a new selector to match your a
with your sections.
<a section-id="hero" href="#hero" class="dot"><span class="hide">dot</span></a>
<a section-id="whole" href="#whole" class="dot"><span class="hide">dot</span></a>
<a section-id="split" href="#split" class="dot"><span class="hide">dot</span></a>
<a section-id="two-split" href="#two-split" class="dot"><span class="hide">dot</span></a>
<a section-id="three-split" href="#three-split" class="dot"><span class="hide">dot</span></a>
<a section-id="footer" href="#footer" class="dot"><span class="hide">dot</span></a>
As for your javascript code, you'll need to select the items with section-id
attribute and check if the associated ID from sections has class current
var aChildren = $('.side ul').children();
var aArray = ;
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('section-id');
aArray.push(ahref);
}
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
if($('#'+theID).hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
}
answered Nov 15 '18 at 10:01
Mojo AllmightyMojo Allmighty
645316
645316
add a comment |
add a comment |
I don't know how is your html looks, here I am giving sample code as it work same as you want.
EDIT: Added on scroll too
$("ul li a").click(function(){
$current=$(this);
$("ul li").each(function(){
$(this).removeClass("current");
})
$current.parent().addClass("current");
});
var one_pos=$('#one').offset().top;
var two_pos=$('#two').offset().top;
var three_pos=$('#three').offset().top;
var four_pos=$('#four').offset().top;
var five_pos=$('#five').offset().top;
$(window).on('scroll', function() {
var current_pos = window.pageYOffset;
if(current_pos>one_pos && current_pos<two_pos){
$("ul li a[href='#one']").click();
}else if(current_pos>two_pos && current_pos<three_pos){
$("ul li a[href='#two']").click();
}else if(current_pos>three_pos && current_pos<four_pos){
$("ul li a[href='#three']").click();
}else if(current_pos>four_pos && current_pos<five_pos){
$("ul li a[href='#four']").click();
}else if(current_pos>four_pos){
$("ul li a[href='#five']").click();
}
})
div{
height:200px;
padding:2%;
border:1px solid;
}
ul{
position:fixed;
list-style:none;
}
ul li.current i{
color:green;
}
ul li i{
color:#ddd;
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="current"><a href="#one"><i class="fa fa-circle"></i></a></li>
<li><a href="#two"><i class="fa fa-circle"></i></a></li>
<li><a href="#three"><i class="fa fa-circle"></i></a></li>
<li><a href="#four"><i class="fa fa-circle"></i></a></li>
<li><a href="#five"><i class="fa fa-circle"></i></a></li>
</ul>
<div id="one">one</div>
<div id="two">two</div>
<div id="three">three</div>
<div id="four">four</div>
<div id="five">five</div>
<div>rest</div>
This is what i initially thought too but then the OP said he doesn't have anyclick
function so i think the dots will be colored on scroll not on click
– Mojo Allmighty
Nov 15 '18 at 9:21
Yeah this works great but i need this to work the same , but on scroll
– Bojan Kolano
Nov 15 '18 at 9:24
@BojanKolano updated code with on scroll trigger. I hope this will helps you.
– Dilip Belgumpi
Nov 15 '18 at 9:39
@DilipBelgumpi Thats even better ! This is a great solution but i need it to be not hard coded . This way if someone adds a new section, the nav wont work anymore :(
– Bojan Kolano
Nov 15 '18 at 9:43
add a comment |
I don't know how is your html looks, here I am giving sample code as it work same as you want.
EDIT: Added on scroll too
$("ul li a").click(function(){
$current=$(this);
$("ul li").each(function(){
$(this).removeClass("current");
})
$current.parent().addClass("current");
});
var one_pos=$('#one').offset().top;
var two_pos=$('#two').offset().top;
var three_pos=$('#three').offset().top;
var four_pos=$('#four').offset().top;
var five_pos=$('#five').offset().top;
$(window).on('scroll', function() {
var current_pos = window.pageYOffset;
if(current_pos>one_pos && current_pos<two_pos){
$("ul li a[href='#one']").click();
}else if(current_pos>two_pos && current_pos<three_pos){
$("ul li a[href='#two']").click();
}else if(current_pos>three_pos && current_pos<four_pos){
$("ul li a[href='#three']").click();
}else if(current_pos>four_pos && current_pos<five_pos){
$("ul li a[href='#four']").click();
}else if(current_pos>four_pos){
$("ul li a[href='#five']").click();
}
})
div{
height:200px;
padding:2%;
border:1px solid;
}
ul{
position:fixed;
list-style:none;
}
ul li.current i{
color:green;
}
ul li i{
color:#ddd;
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="current"><a href="#one"><i class="fa fa-circle"></i></a></li>
<li><a href="#two"><i class="fa fa-circle"></i></a></li>
<li><a href="#three"><i class="fa fa-circle"></i></a></li>
<li><a href="#four"><i class="fa fa-circle"></i></a></li>
<li><a href="#five"><i class="fa fa-circle"></i></a></li>
</ul>
<div id="one">one</div>
<div id="two">two</div>
<div id="three">three</div>
<div id="four">four</div>
<div id="five">five</div>
<div>rest</div>
This is what i initially thought too but then the OP said he doesn't have anyclick
function so i think the dots will be colored on scroll not on click
– Mojo Allmighty
Nov 15 '18 at 9:21
Yeah this works great but i need this to work the same , but on scroll
– Bojan Kolano
Nov 15 '18 at 9:24
@BojanKolano updated code with on scroll trigger. I hope this will helps you.
– Dilip Belgumpi
Nov 15 '18 at 9:39
@DilipBelgumpi Thats even better ! This is a great solution but i need it to be not hard coded . This way if someone adds a new section, the nav wont work anymore :(
– Bojan Kolano
Nov 15 '18 at 9:43
add a comment |
I don't know how is your html looks, here I am giving sample code as it work same as you want.
EDIT: Added on scroll too
$("ul li a").click(function(){
$current=$(this);
$("ul li").each(function(){
$(this).removeClass("current");
})
$current.parent().addClass("current");
});
var one_pos=$('#one').offset().top;
var two_pos=$('#two').offset().top;
var three_pos=$('#three').offset().top;
var four_pos=$('#four').offset().top;
var five_pos=$('#five').offset().top;
$(window).on('scroll', function() {
var current_pos = window.pageYOffset;
if(current_pos>one_pos && current_pos<two_pos){
$("ul li a[href='#one']").click();
}else if(current_pos>two_pos && current_pos<three_pos){
$("ul li a[href='#two']").click();
}else if(current_pos>three_pos && current_pos<four_pos){
$("ul li a[href='#three']").click();
}else if(current_pos>four_pos && current_pos<five_pos){
$("ul li a[href='#four']").click();
}else if(current_pos>four_pos){
$("ul li a[href='#five']").click();
}
})
div{
height:200px;
padding:2%;
border:1px solid;
}
ul{
position:fixed;
list-style:none;
}
ul li.current i{
color:green;
}
ul li i{
color:#ddd;
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="current"><a href="#one"><i class="fa fa-circle"></i></a></li>
<li><a href="#two"><i class="fa fa-circle"></i></a></li>
<li><a href="#three"><i class="fa fa-circle"></i></a></li>
<li><a href="#four"><i class="fa fa-circle"></i></a></li>
<li><a href="#five"><i class="fa fa-circle"></i></a></li>
</ul>
<div id="one">one</div>
<div id="two">two</div>
<div id="three">three</div>
<div id="four">four</div>
<div id="five">five</div>
<div>rest</div>
I don't know how is your html looks, here I am giving sample code as it work same as you want.
EDIT: Added on scroll too
$("ul li a").click(function(){
$current=$(this);
$("ul li").each(function(){
$(this).removeClass("current");
})
$current.parent().addClass("current");
});
var one_pos=$('#one').offset().top;
var two_pos=$('#two').offset().top;
var three_pos=$('#three').offset().top;
var four_pos=$('#four').offset().top;
var five_pos=$('#five').offset().top;
$(window).on('scroll', function() {
var current_pos = window.pageYOffset;
if(current_pos>one_pos && current_pos<two_pos){
$("ul li a[href='#one']").click();
}else if(current_pos>two_pos && current_pos<three_pos){
$("ul li a[href='#two']").click();
}else if(current_pos>three_pos && current_pos<four_pos){
$("ul li a[href='#three']").click();
}else if(current_pos>four_pos && current_pos<five_pos){
$("ul li a[href='#four']").click();
}else if(current_pos>four_pos){
$("ul li a[href='#five']").click();
}
})
div{
height:200px;
padding:2%;
border:1px solid;
}
ul{
position:fixed;
list-style:none;
}
ul li.current i{
color:green;
}
ul li i{
color:#ddd;
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="current"><a href="#one"><i class="fa fa-circle"></i></a></li>
<li><a href="#two"><i class="fa fa-circle"></i></a></li>
<li><a href="#three"><i class="fa fa-circle"></i></a></li>
<li><a href="#four"><i class="fa fa-circle"></i></a></li>
<li><a href="#five"><i class="fa fa-circle"></i></a></li>
</ul>
<div id="one">one</div>
<div id="two">two</div>
<div id="three">three</div>
<div id="four">four</div>
<div id="five">five</div>
<div>rest</div>
$("ul li a").click(function(){
$current=$(this);
$("ul li").each(function(){
$(this).removeClass("current");
})
$current.parent().addClass("current");
});
var one_pos=$('#one').offset().top;
var two_pos=$('#two').offset().top;
var three_pos=$('#three').offset().top;
var four_pos=$('#four').offset().top;
var five_pos=$('#five').offset().top;
$(window).on('scroll', function() {
var current_pos = window.pageYOffset;
if(current_pos>one_pos && current_pos<two_pos){
$("ul li a[href='#one']").click();
}else if(current_pos>two_pos && current_pos<three_pos){
$("ul li a[href='#two']").click();
}else if(current_pos>three_pos && current_pos<four_pos){
$("ul li a[href='#three']").click();
}else if(current_pos>four_pos && current_pos<five_pos){
$("ul li a[href='#four']").click();
}else if(current_pos>four_pos){
$("ul li a[href='#five']").click();
}
})
div{
height:200px;
padding:2%;
border:1px solid;
}
ul{
position:fixed;
list-style:none;
}
ul li.current i{
color:green;
}
ul li i{
color:#ddd;
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="current"><a href="#one"><i class="fa fa-circle"></i></a></li>
<li><a href="#two"><i class="fa fa-circle"></i></a></li>
<li><a href="#three"><i class="fa fa-circle"></i></a></li>
<li><a href="#four"><i class="fa fa-circle"></i></a></li>
<li><a href="#five"><i class="fa fa-circle"></i></a></li>
</ul>
<div id="one">one</div>
<div id="two">two</div>
<div id="three">three</div>
<div id="four">four</div>
<div id="five">five</div>
<div>rest</div>
$("ul li a").click(function(){
$current=$(this);
$("ul li").each(function(){
$(this).removeClass("current");
})
$current.parent().addClass("current");
});
var one_pos=$('#one').offset().top;
var two_pos=$('#two').offset().top;
var three_pos=$('#three').offset().top;
var four_pos=$('#four').offset().top;
var five_pos=$('#five').offset().top;
$(window).on('scroll', function() {
var current_pos = window.pageYOffset;
if(current_pos>one_pos && current_pos<two_pos){
$("ul li a[href='#one']").click();
}else if(current_pos>two_pos && current_pos<three_pos){
$("ul li a[href='#two']").click();
}else if(current_pos>three_pos && current_pos<four_pos){
$("ul li a[href='#three']").click();
}else if(current_pos>four_pos && current_pos<five_pos){
$("ul li a[href='#four']").click();
}else if(current_pos>four_pos){
$("ul li a[href='#five']").click();
}
})
div{
height:200px;
padding:2%;
border:1px solid;
}
ul{
position:fixed;
list-style:none;
}
ul li.current i{
color:green;
}
ul li i{
color:#ddd;
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="current"><a href="#one"><i class="fa fa-circle"></i></a></li>
<li><a href="#two"><i class="fa fa-circle"></i></a></li>
<li><a href="#three"><i class="fa fa-circle"></i></a></li>
<li><a href="#four"><i class="fa fa-circle"></i></a></li>
<li><a href="#five"><i class="fa fa-circle"></i></a></li>
</ul>
<div id="one">one</div>
<div id="two">two</div>
<div id="three">three</div>
<div id="four">four</div>
<div id="five">five</div>
<div>rest</div>
edited Nov 15 '18 at 9:39
answered Nov 15 '18 at 9:19
Dilip BelgumpiDilip Belgumpi
631413
631413
This is what i initially thought too but then the OP said he doesn't have anyclick
function so i think the dots will be colored on scroll not on click
– Mojo Allmighty
Nov 15 '18 at 9:21
Yeah this works great but i need this to work the same , but on scroll
– Bojan Kolano
Nov 15 '18 at 9:24
@BojanKolano updated code with on scroll trigger. I hope this will helps you.
– Dilip Belgumpi
Nov 15 '18 at 9:39
@DilipBelgumpi Thats even better ! This is a great solution but i need it to be not hard coded . This way if someone adds a new section, the nav wont work anymore :(
– Bojan Kolano
Nov 15 '18 at 9:43
add a comment |
This is what i initially thought too but then the OP said he doesn't have anyclick
function so i think the dots will be colored on scroll not on click
– Mojo Allmighty
Nov 15 '18 at 9:21
Yeah this works great but i need this to work the same , but on scroll
– Bojan Kolano
Nov 15 '18 at 9:24
@BojanKolano updated code with on scroll trigger. I hope this will helps you.
– Dilip Belgumpi
Nov 15 '18 at 9:39
@DilipBelgumpi Thats even better ! This is a great solution but i need it to be not hard coded . This way if someone adds a new section, the nav wont work anymore :(
– Bojan Kolano
Nov 15 '18 at 9:43
This is what i initially thought too but then the OP said he doesn't have any
click
function so i think the dots will be colored on scroll not on click– Mojo Allmighty
Nov 15 '18 at 9:21
This is what i initially thought too but then the OP said he doesn't have any
click
function so i think the dots will be colored on scroll not on click– Mojo Allmighty
Nov 15 '18 at 9:21
Yeah this works great but i need this to work the same , but on scroll
– Bojan Kolano
Nov 15 '18 at 9:24
Yeah this works great but i need this to work the same , but on scroll
– Bojan Kolano
Nov 15 '18 at 9:24
@BojanKolano updated code with on scroll trigger. I hope this will helps you.
– Dilip Belgumpi
Nov 15 '18 at 9:39
@BojanKolano updated code with on scroll trigger. I hope this will helps you.
– Dilip Belgumpi
Nov 15 '18 at 9:39
@DilipBelgumpi Thats even better ! This is a great solution but i need it to be not hard coded . This way if someone adds a new section, the nav wont work anymore :(
– Bojan Kolano
Nov 15 '18 at 9:43
@DilipBelgumpi Thats even better ! This is a great solution but i need it to be not hard coded . This way if someone adds a new section, the nav wont work anymore :(
– Bojan Kolano
Nov 15 '18 at 9:43
add a comment |
OK so after some time and lots of help, this is the solution :
$(window).scroll(function(){
var cutoff = $(window).scrollTop();
panel.each(function(){
if($(this).offset().top + $(this).height() > cutoff){
panel.removeClass('current');
$(this).addClass('current');
return false;
}
})
var aChildren = $('.side ul').children();
var aArray = ;
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('section-id');
aArray.push(ahref);
}
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
if($('#'+theID).hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
}
Thanks to @Mojo Allmighty
add a comment |
OK so after some time and lots of help, this is the solution :
$(window).scroll(function(){
var cutoff = $(window).scrollTop();
panel.each(function(){
if($(this).offset().top + $(this).height() > cutoff){
panel.removeClass('current');
$(this).addClass('current');
return false;
}
})
var aChildren = $('.side ul').children();
var aArray = ;
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('section-id');
aArray.push(ahref);
}
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
if($('#'+theID).hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
}
Thanks to @Mojo Allmighty
add a comment |
OK so after some time and lots of help, this is the solution :
$(window).scroll(function(){
var cutoff = $(window).scrollTop();
panel.each(function(){
if($(this).offset().top + $(this).height() > cutoff){
panel.removeClass('current');
$(this).addClass('current');
return false;
}
})
var aChildren = $('.side ul').children();
var aArray = ;
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('section-id');
aArray.push(ahref);
}
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
if($('#'+theID).hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
}
Thanks to @Mojo Allmighty
OK so after some time and lots of help, this is the solution :
$(window).scroll(function(){
var cutoff = $(window).scrollTop();
panel.each(function(){
if($(this).offset().top + $(this).height() > cutoff){
panel.removeClass('current');
$(this).addClass('current');
return false;
}
})
var aChildren = $('.side ul').children();
var aArray = ;
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('section-id');
aArray.push(ahref);
}
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
if($('#'+theID).hasClass('current')) {
$("a[href='#" + theID + "']").addClass('greens');
} else {
$("a[href='#" + theID + "']").removeClass('greens');
}
}
Thanks to @Mojo Allmighty
answered Nov 15 '18 at 9:55
Bojan KolanoBojan Kolano
14210
14210
add a comment |
add a comment |
What this does is "Loop through all a-elements, and color them if the panel has the class "current"".
You problem:
If the panel has class "current" color all a-elements green
What you want:
IF the panel has class current lookup the corresponding a-element (belonging to the panel) and addClass green.
So this does not check if the single a-element should get the class "greens" based on which panel is currently active ("current").
Without insight into the rest of the code you should/could do 1 of 2 things;
- Loop through the panels and add the class "Greens" to the corresponding a-element.
- Loop through the a-elements (as you're doing now), and check the corresponding panel element (the panel element that belongs to the a-element).
Depending on your code this might be as easy as:
$panel.find("a").addClass('greens');
---- edit ---
Although there's an accepted answer already you could do this with a somewhat easier to understand JS and HTML (without the loops and all).
On the panel you can add data-attributes with names that correspond to the a-elements you want to make green/active. When you are making the panel active you can also select the single a-element that corresponds to your panel (so panel with data-corresponding-link="hero" refers to the a-element with data-id="hero"), and just add the class green to it.
You can choose to first select all a-elements and remove the class green or select the single a-element which has the class green and remove that single one.
<panel class="hero current" data-corresponding-link="hero"></panel>
<panel class="other-panel" data-corresponding-link="other-panel"></panel>
<a data-id="hero" href="#hero" class="dot"><span class="show">dot</span></a>
<a data-id="other-panel" href="#three-split" class="dot"><span class="hide">dot</span></a>
js:
$(window).scroll(function(){
var cutoff = $(window).scrollTop();
panel.each(function(){
if($(this).offset().top + $(this).height() > cutoff){
panel.removeClass('current');
$(this).addClass('current');
$(".green").removeClass("green") //Select all the green classes and remove the class green (you could scope this further down to a.green or something).
$("data-id='"+panel.dataset.correspondingLink+"']").addClass("green") //based on the panels corresponding-link data-attribute select the element(s) that have this data-attribute data-id set to "hero" and addClass green
return false;
}
})
}
This is more of a comment, not an answer.
– Mojo Allmighty
Nov 15 '18 at 8:46
@sajoku This makes sense but my sidenav with dots is not in the sections . None of the sections have a . This sidenav is a separate thing on the page .
– Bojan Kolano
Nov 15 '18 at 8:50
@sajoku The 2nd thing , Looping through a-elements sounds very close to what I need . Could you write an example or something ?
– Bojan Kolano
Nov 15 '18 at 8:55
I added a way to do this without having to loop through all the a-elements.
– sajoku
Nov 16 '18 at 9:57
add a comment |
What this does is "Loop through all a-elements, and color them if the panel has the class "current"".
You problem:
If the panel has class "current" color all a-elements green
What you want:
IF the panel has class current lookup the corresponding a-element (belonging to the panel) and addClass green.
So this does not check if the single a-element should get the class "greens" based on which panel is currently active ("current").
Without insight into the rest of the code you should/could do 1 of 2 things;
- Loop through the panels and add the class "Greens" to the corresponding a-element.
- Loop through the a-elements (as you're doing now), and check the corresponding panel element (the panel element that belongs to the a-element).
Depending on your code this might be as easy as:
$panel.find("a").addClass('greens');
---- edit ---
Although there's an accepted answer already you could do this with a somewhat easier to understand JS and HTML (without the loops and all).
On the panel you can add data-attributes with names that correspond to the a-elements you want to make green/active. When you are making the panel active you can also select the single a-element that corresponds to your panel (so panel with data-corresponding-link="hero" refers to the a-element with data-id="hero"), and just add the class green to it.
You can choose to first select all a-elements and remove the class green or select the single a-element which has the class green and remove that single one.
<panel class="hero current" data-corresponding-link="hero"></panel>
<panel class="other-panel" data-corresponding-link="other-panel"></panel>
<a data-id="hero" href="#hero" class="dot"><span class="show">dot</span></a>
<a data-id="other-panel" href="#three-split" class="dot"><span class="hide">dot</span></a>
js:
$(window).scroll(function(){
var cutoff = $(window).scrollTop();
panel.each(function(){
if($(this).offset().top + $(this).height() > cutoff){
panel.removeClass('current');
$(this).addClass('current');
$(".green").removeClass("green") //Select all the green classes and remove the class green (you could scope this further down to a.green or something).
$("data-id='"+panel.dataset.correspondingLink+"']").addClass("green") //based on the panels corresponding-link data-attribute select the element(s) that have this data-attribute data-id set to "hero" and addClass green
return false;
}
})
}
This is more of a comment, not an answer.
– Mojo Allmighty
Nov 15 '18 at 8:46
@sajoku This makes sense but my sidenav with dots is not in the sections . None of the sections have a . This sidenav is a separate thing on the page .
– Bojan Kolano
Nov 15 '18 at 8:50
@sajoku The 2nd thing , Looping through a-elements sounds very close to what I need . Could you write an example or something ?
– Bojan Kolano
Nov 15 '18 at 8:55
I added a way to do this without having to loop through all the a-elements.
– sajoku
Nov 16 '18 at 9:57
add a comment |
What this does is "Loop through all a-elements, and color them if the panel has the class "current"".
You problem:
If the panel has class "current" color all a-elements green
What you want:
IF the panel has class current lookup the corresponding a-element (belonging to the panel) and addClass green.
So this does not check if the single a-element should get the class "greens" based on which panel is currently active ("current").
Without insight into the rest of the code you should/could do 1 of 2 things;
- Loop through the panels and add the class "Greens" to the corresponding a-element.
- Loop through the a-elements (as you're doing now), and check the corresponding panel element (the panel element that belongs to the a-element).
Depending on your code this might be as easy as:
$panel.find("a").addClass('greens');
---- edit ---
Although there's an accepted answer already you could do this with a somewhat easier to understand JS and HTML (without the loops and all).
On the panel you can add data-attributes with names that correspond to the a-elements you want to make green/active. When you are making the panel active you can also select the single a-element that corresponds to your panel (so panel with data-corresponding-link="hero" refers to the a-element with data-id="hero"), and just add the class green to it.
You can choose to first select all a-elements and remove the class green or select the single a-element which has the class green and remove that single one.
<panel class="hero current" data-corresponding-link="hero"></panel>
<panel class="other-panel" data-corresponding-link="other-panel"></panel>
<a data-id="hero" href="#hero" class="dot"><span class="show">dot</span></a>
<a data-id="other-panel" href="#three-split" class="dot"><span class="hide">dot</span></a>
js:
$(window).scroll(function(){
var cutoff = $(window).scrollTop();
panel.each(function(){
if($(this).offset().top + $(this).height() > cutoff){
panel.removeClass('current');
$(this).addClass('current');
$(".green").removeClass("green") //Select all the green classes and remove the class green (you could scope this further down to a.green or something).
$("data-id='"+panel.dataset.correspondingLink+"']").addClass("green") //based on the panels corresponding-link data-attribute select the element(s) that have this data-attribute data-id set to "hero" and addClass green
return false;
}
})
}
What this does is "Loop through all a-elements, and color them if the panel has the class "current"".
You problem:
If the panel has class "current" color all a-elements green
What you want:
IF the panel has class current lookup the corresponding a-element (belonging to the panel) and addClass green.
So this does not check if the single a-element should get the class "greens" based on which panel is currently active ("current").
Without insight into the rest of the code you should/could do 1 of 2 things;
- Loop through the panels and add the class "Greens" to the corresponding a-element.
- Loop through the a-elements (as you're doing now), and check the corresponding panel element (the panel element that belongs to the a-element).
Depending on your code this might be as easy as:
$panel.find("a").addClass('greens');
---- edit ---
Although there's an accepted answer already you could do this with a somewhat easier to understand JS and HTML (without the loops and all).
On the panel you can add data-attributes with names that correspond to the a-elements you want to make green/active. When you are making the panel active you can also select the single a-element that corresponds to your panel (so panel with data-corresponding-link="hero" refers to the a-element with data-id="hero"), and just add the class green to it.
You can choose to first select all a-elements and remove the class green or select the single a-element which has the class green and remove that single one.
<panel class="hero current" data-corresponding-link="hero"></panel>
<panel class="other-panel" data-corresponding-link="other-panel"></panel>
<a data-id="hero" href="#hero" class="dot"><span class="show">dot</span></a>
<a data-id="other-panel" href="#three-split" class="dot"><span class="hide">dot</span></a>
js:
$(window).scroll(function(){
var cutoff = $(window).scrollTop();
panel.each(function(){
if($(this).offset().top + $(this).height() > cutoff){
panel.removeClass('current');
$(this).addClass('current');
$(".green").removeClass("green") //Select all the green classes and remove the class green (you could scope this further down to a.green or something).
$("data-id='"+panel.dataset.correspondingLink+"']").addClass("green") //based on the panels corresponding-link data-attribute select the element(s) that have this data-attribute data-id set to "hero" and addClass green
return false;
}
})
}
edited Nov 16 '18 at 9:55
answered Nov 15 '18 at 8:45
sajokusajoku
164
164
This is more of a comment, not an answer.
– Mojo Allmighty
Nov 15 '18 at 8:46
@sajoku This makes sense but my sidenav with dots is not in the sections . None of the sections have a . This sidenav is a separate thing on the page .
– Bojan Kolano
Nov 15 '18 at 8:50
@sajoku The 2nd thing , Looping through a-elements sounds very close to what I need . Could you write an example or something ?
– Bojan Kolano
Nov 15 '18 at 8:55
I added a way to do this without having to loop through all the a-elements.
– sajoku
Nov 16 '18 at 9:57
add a comment |
This is more of a comment, not an answer.
– Mojo Allmighty
Nov 15 '18 at 8:46
@sajoku This makes sense but my sidenav with dots is not in the sections . None of the sections have a . This sidenav is a separate thing on the page .
– Bojan Kolano
Nov 15 '18 at 8:50
@sajoku The 2nd thing , Looping through a-elements sounds very close to what I need . Could you write an example or something ?
– Bojan Kolano
Nov 15 '18 at 8:55
I added a way to do this without having to loop through all the a-elements.
– sajoku
Nov 16 '18 at 9:57
This is more of a comment, not an answer.
– Mojo Allmighty
Nov 15 '18 at 8:46
This is more of a comment, not an answer.
– Mojo Allmighty
Nov 15 '18 at 8:46
@sajoku This makes sense but my sidenav with dots is not in the sections . None of the sections have a . This sidenav is a separate thing on the page .
– Bojan Kolano
Nov 15 '18 at 8:50
@sajoku This makes sense but my sidenav with dots is not in the sections . None of the sections have a . This sidenav is a separate thing on the page .
– Bojan Kolano
Nov 15 '18 at 8:50
@sajoku The 2nd thing , Looping through a-elements sounds very close to what I need . Could you write an example or something ?
– Bojan Kolano
Nov 15 '18 at 8:55
@sajoku The 2nd thing , Looping through a-elements sounds very close to what I need . Could you write an example or something ?
– Bojan Kolano
Nov 15 '18 at 8:55
I added a way to do this without having to loop through all the a-elements.
– sajoku
Nov 16 '18 at 9:57
I added a way to do this without having to loop through all the a-elements.
– sajoku
Nov 16 '18 at 9:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315264%2fsidenav-with-dots-scrolled%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I believe it has something to do with the dot click function. Also keep in mind that
panel
variable stores all elements with class..panels
so you might reconsider this ifpanel.hasClass('current')
– Mojo Allmighty
Nov 15 '18 at 8:44
@Mojo Allmighty I don't think I have any click event , if that's what you mean. But for this Variable , you think I shouldn't store it like that ? I don't think I understand clearly :(
– Bojan Kolano
Nov 15 '18 at 8:48
1
You can use
if($('#'+theID).hasClass('current'))
– Mojo Allmighty
Nov 15 '18 at 8:57
1
The id must be unique, you are using it in multiple places.
– Mojo Allmighty
Nov 15 '18 at 9:04
1
Yeah, or use an attribute for that, something like
section-id="YourSectionID"
and select it in your first for. You'll need to modifyvar ahref = $(aChild).attr('id');
intovar ahref = $(aChild).attr('section-id');
– Mojo Allmighty
Nov 15 '18 at 9:16