Hi, i have read a lot of forum threads but i can't figure out how to make the CSS Active to stay on the selected TopNavi item. I have put in (ul id="topnav") and i works fine but i dont know where to put (li class="active") i have used the standard Razor code from Umbraco, see below:
Thanks for the quick reply, i have tried to make the changes above, but the class (active) still jumps back to the first menu item and does not stay on the selected page / menu item. The only thing the class (active) does is to highlight the selectet page / item in the TopNavi menu, so when you click the "About Us" it should stay highlighted, but it jumps to the first item in the TopNavi. I hope this explains in more detail what my problem is.
I think the problem comes from the fact that the herf for the "about us" page is #, which means that you actually do not change the page your are on.
So if you are on the home page, the url will be /index.html, and if you then click on "about us", the url will become /index.html#, so you are actually still on index.html and it is therefore that page that gets highlighted.
So the easiest solution, I think, would be to put an actual url on the "about us" page. Is this possible, or is there a specific reason why you have put it to "#"?
Sorry about the HTML shows that "about us" is #, but that was just an example. I really want the menu to use Razor and be dynamic for learning reasons, because im new to Razor. The menu works dynamically but as earlier mentioned the only thing that does not work is the "active" CSS. I am testing local, so i cant show you the site im working on. But the local url's are at the moment. "http://localhost:26193/om-refica.aspx" + "http://localhost:26193/produkter.aspx" which are createt dynamically from the content in Umbraco.
Now, I don't think it will help, but to make sure, ther are two more changes you can make to the razor script:
for the parent : change @Model.AncestorOrSelf by Model.AncestorOrSelf
for the li : remove the Html.Raw call
@inherits umbraco.MacroEngines.DynamicNodeContext @{ var level =String.IsNullOrEmpty(Parameter.Level)?1:int.Parse(Parameter.Level); var ulClass =String.IsNullOrEmpty(Parameter.UlClass)?" id=topnav":String.Format(" class=\"{0}\"",Parameter.UlClass); var parent = Model.AncestorOrSelf(level); if(parent !=null){ <ul@Html.Raw(ulClass)> @foreach(var item inparent.Children.Where("Visible")){ var selected =Array.IndexOf(Model.Path.Split(','),item.Id.ToString())>=0?" class=\"active\"":""; <li @selected> <a href="@item.Url">@item.Name</a> </li> } </ul> } }
If this still does not work, maybe in the loop you can output the value of Model.Path and of item.Id. At least we can see if those values seem logical.
TopNavi Current / Active CSS class
Hi, i have read a lot of forum threads but i can't figure out how to make the CSS Active to stay on the selected TopNavi item. I have put in (ul id="topnav") and i works fine but i dont know where to put (li class="active") i have used the standard Razor code from Umbraco, see below:
HTML:
Razor:
Thanks in advance. :-)
Hi René,
I'm not sure what your problem exactly is, but id what you are trying to do is get the above HTML in razor, I think this script will work better:
Hope this helps.
Cheers,
Michael.
Hi Michael
Thanks for the quick reply, i have tried to make the changes above, but the class (active) still jumps back to the first menu item and does not stay on the selected page / menu item. The only thing the class (active) does is to highlight the selectet page / item in the TopNavi menu, so when you click the "About Us" it should stay highlighted, but it jumps to the first item in the TopNavi. I hope this explains in more detail what my problem is.
Cheers,
René
Hi René,
I think the problem comes from the fact that the herf for the "about us" page is #, which means that you actually do not change the page your are on.
So if you are on the home page, the url will be /index.html, and if you then click on "about us", the url will become /index.html#, so you are actually still on index.html and it is therefore that page that gets highlighted.
So the easiest solution, I think, would be to put an actual url on the "about us" page. Is this possible, or is there a specific reason why you have put it to "#"?
Cheers,
Michael.
Hi Michael
Sorry about the HTML shows that "about us" is #, but that was just an example. I really want the menu to use Razor and be dynamic for learning reasons, because im new to Razor. The menu works dynamically but as earlier mentioned the only thing that does not work is the "active" CSS. I am testing local, so i cant show you the site im working on. But the local url's are at the moment. "http://localhost:26193/om-refica.aspx" + "http://localhost:26193/produkter.aspx" which are createt dynamically from the content in Umbraco.
Cheers,
René
Hi René,
Strange indeed, because the razor looks OK. Can you maybe post the HTML that is generated on your local site? Maybe this can help finding the issue.
Cheers,
Michael.
Hi Michael
Below you can see the HTML which is generated:
And i should look somthing like this, if "om-refica.aspx" is selected in the TopNavi:
I am very grateful that you are spending so much time on my problem. :-)
Hi René,
Very strang indeed.
Now, I don't think it will help, but to make sure, ther are two more changes you can make to the razor script:
for the parent : change @Model.AncestorOrSelf by Model.AncestorOrSelf
for the li : remove the Html.Raw call
If this still does not work, maybe in the loop you can output the value of Model.Path and of item.Id. At least we can see if those values seem logical.
Cheers,
Michael.
Hi Michael
Yeeaaahh, now it works. :-) It has now generated the HTML perfectly with "active" included:
Thank you very much, i really appreciate your help.
Cheers,
René
Hi René,
Great news!
Can you maybe mark my last post as answer, so that others may find it more easily?
Thanks and good day further!
Cheers,
Michael.
Hi Michael
Your last post is hereby marked as answer. Again...Thank you very much!
Cheers,
René
Hi René,
You're very welcome :-)
Cheers,
Michael.
is working on a reply...