0

I have a navigation with an empty div in each anchor tag that I am styling on hover.

html

<li><a href="#events">SPECIAL EVENTS <div></div></a></li>

css

a:hover div, a:active div{
    color: $black;
    height: 1px;
    background-color: $black;
    width: 100%;
    margin-top: 5px;
}

I also have a active class that I am attaching on click with some js. This is currently working correctly.

var currentDiv;

function addSelected(){

if(currentDiv !== undefined){
    $(currentDiv).removeClass("active");    
}

currentDiv = $(this).find('div');
$(currentDiv).addClass("active");
}

$(".menu a").click(addSelected);

What I am trying to do is attached that same active class based on the user scroll. I get most of it working, but I can't seem to figure how how to attached the class to the div, and not the anchor itself. Here is the js I'm working with.

js

    // Cache selectors
var lastId,
topMenu = $("#desktop-nav"),
topMenuHeight = topMenu.outerHeight()+15,
// All list items
menuItems = topMenu.find("a"),
// Anchors corresponding to menu items
scrollItems = menuItems.map(function(){
  var item = $($(this).attr("href"));
  if (item.length) { return item; }
});

// Bind to scroll
$(window).scroll(function(){
// Get container scroll position
var fromTop = $(this).scrollTop()+topMenuHeight;

// Get id of current scroll item
var cur = scrollItems.map(function(){
 if ($(this).offset().top < fromTop){
   return this;
 }  
});
// Get the id of the current element
cur = cur[cur.length-1];
var id = cur && cur.length ? cur[0].id : "";

if (lastId !== id) {
    lastId = id;
    // Set/remove active class
    menuItems

    //.parent().removeClass("active")
    //.end().filter("[href='#"+id+"']").parent().addClass("active");

    .removeClass("active");
    end().filter($("[href='#"+id+"']")).find('div').addClass("active");

    console.log(id);
}                   
});

I think the part that I am trying to change is this

"[href='#"+id+"']").parent()

but everything I try is either not working or giving me errors.

EDIT

Here is the fiddle that I am trying to modify.

fiddle link

4
  • What is your current status. Can you provide a fiddle ? Commented Jul 1, 2017 at 2:44
  • it's attaching the class to the parent of the anchor tag instead of the div tag that is inside of the anchor Commented Jul 1, 2017 at 2:48
  • Please provide the complete code where you are stuck. Commented Jul 1, 2017 at 2:50
  • @dan, I updated the js to include all the code that I am using, Does that help? Commented Jul 1, 2017 at 3:09

3 Answers 3

1

Use children() instead of parent() or find()

Example:

 if (lastId !== id) {
       lastId = id;
       // add/remove active class on div
       menuItems
         .children().removeClass("active")
         .end().filter("[href='#"+id+"']").children().addClass("active");
   } 
Sign up to request clarification or add additional context in comments.

Comments

1

Try changing it to use find() like in your click handler:

"[href='#"+id+"']").find('div')

1 Comment

This seems to find the first one, but still adds the class to the parent, even if I update the code to this .end().filter("[href='#"+id+"']").find('div').addClass("active");
0

I assume you've tried...

"[href='#"+id+"']").parent().find('div')

and/or...

"[href='#"+id+"'].div"

...if so, are you able to add a class or id to the div and target it that way?

1 Comment

Yes I can add a class to the div, but I'm still not sure how to target the class with the above code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.