I am building navigation that I would like to change the style of the link on the current page to highlight the page the user is on in the navigation. I have what I think should work, but the script won't load in the page. Any suggestions?
Isn't because the context in your subnavigation is not right? it says Model.Id == page.id...but in your subnavigation you're using item? Shouldn't it read Model.id == item.id? (In the context of the second foreach loop of course).
It needs to only display the children if the current page has children, otherwise it would just display all the top level nodes. Sorry if I was not clear. Thanks for all your help.
Yes, I tried it. It will always display all the children wheather or not the current page has children because if there are children the count is allways greater than 0.
Thanks Fuji for all your help with this! I've got exactly what I needed with this code, but I am a little confused as to why my second foreach "childActive" had to check for item.IsAncestorOrSelf(Model) and not item.IsDecendantOrSelf(Model), but it works.
One more question. If on my main page, I don't want any of the tabs which are children of @Model (current page), but once I get to one of the child pages I do want the children of @Model (current page) to have the "active" class. As it is now, the landing page has all the tabs or children as "active", untill you go to one of their pages. Look here: https://edit-wwwprep.rose-hulman.edu/student-life.aspx
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var home = Model.AncestorOrSelf(2);
var nav = home.Children.Where("Visible");
<ul>
@foreach(var item in nav){
var style = item.IsDescendantOrSelf(Model) ? "class=active" : "";
<li @style ><a href="@item.Url"><span>@item.Name</span></a>
@* test to see if there are child nodes and if so make the dropdown list *@
@if(item.Children.Count() > 0){
<ul>
@foreach(var subNav in item.Children){
var first = subNav.IsFirst() ? "class=first" : "";
<li @first ><a href="@subNav.Url"><span>@subNav.Name</span></a></li>
}
</ul>
}
</li>
}
</ul>
@* test to see if property useAlternateNavigation is checked if not load the image below the tabs *@
if(Model.Parent.Where("!useAlternateNavigation") || Model.Where("!useAlternateNavigation")){
<img src="/static/phase2/tabNavBorderBottom.jpg" class="tabBorder" />
}
}
Change CSS Style on Current Page?
I am building navigation that I would like to change the style of the link on the current page to highlight the page the user is on in the navigation. I have what I think should work, but the script won't load in the page. Any suggestions?
Hi Steve
Isn't because the context in your subnavigation is not right? it says Model.Id == page.id...but in your subnavigation you're using item? Shouldn't it read Model.id == item.id? (In the context of the second foreach loop of course).
/Jan
Hi Steve,
Try something like this
Hope it helps
Fuji, That works perfectly! Thanks!
One more question. How could I hide the children unless the user is on the page that contains the children?
You can do this
This doesn't seem to work, and I don't see how it would:
Are you building a Navigation here ? You should be able to get this working with some css as well
What about this ?
It needs to only display the children if the current page has children, otherwise it would just display all the top level nodes. Sorry if I was not clear. Thanks for all your help.
No prob but have you tried the code i posted ? Just change this bit then
Yes, I tried it. It will always display all the children wheather or not the current page has children because if there are children the count is allways greater than 0.
Oh i see then you should try
sorry i dont know what happened with the previous post
anyways this should work
Well, I couldn't get your code to work, but I have it sort of working. Except it is applying the @style to every child under the current page as well.
You need to change that part since you only want the sub page to be highlighted.
Thanks Fuji for all your help with this! I've got exactly what I needed with this code, but I am a little confused as to why my second foreach "childActive" had to check for item.IsAncestorOrSelf(Model) and not item.IsDecendantOrSelf(Model), but it works.
Glad i could help
Fuji,
One more question. If on my main page, I don't want any of the tabs which are children of @Model (current page), but once I get to one of the child pages I do want the children of @Model (current page) to have the "active" class. As it is now, the landing page has all the tabs or children as "active", untill you go to one of their pages. Look here: https://edit-wwwprep.rose-hulman.edu/student-life.aspx
Got it!
var style = item.IsAncestorOrSelf(Model) ? "class=active" : "";
is working on a reply...