I am using wordpress, bootstrap and fontawesome to create a navigation. It is fixed to the top of the screen and when the user scrolls a class shrink
gets added to the header class with different css values.
When the class gets added, I want the text links in the header to turn to icons (fontawesome) tried using show/hide css but I can’t hardcode the links directly since they are generated by wordpress.
HTML – navigation
<div class="navbar-collapse collapse navbar-right">
<?php /* Primary navigation */
wp_nav_menu( array(
'menu' => 'top_menu',
'depth' => 2,
'container' => false,
'menu_class' => 'nav navbar-nav',
//Process nav menu using our custom nav walker
'walker' => new wp_bootstrap_navwalker())
);
?>
</div>
css
.navbar-custom .nav>li>a {
font-size: 1.15em;
font-weight: 400;
etc...
}
EDIT – As mentioned in the question I can’t hardcode the links directly cause it’s handled by wordpress
head html – applying the ‘shrink’ class
$(function(){
var shrinkHeader = 100;
$(window).scroll(function() {
var scroll = getCurrentScroll();
if ( scroll >= shrinkHeader ) {
$('header').addClass('shrink');
}
else {
$('header').removeClass('shrink');
}
});
Edit
This would probably be a better way of doing it, since you would just be changing display of CSS instead of html… less loops too.
JQuery
Original Post
This may be a bit of a stretch since I haven’t used WordPress much, but I believe (according to this source) that each li will have its own class. Using JQuery, you can apply a check when the user scrolls like this:
JQuery
This is really rough and I have no way of testing it at the moment, but hopefully it can help you in some way. I highly doubt it’s a 100% solution to your issue, it’ll probably need some tweaking even after you put in the right classes and ids. If you provide more information I can try to help more.
You can do that with just CSS. What you need are style rules that will be processed based on the existence of an element with the
shrink
class. If it exists in the header then the styles will set the linksfont-size
property to zero to ‘hide’ the text and will use a::before
selector to set the icons in thecontent
property and thefont-size
of the icons so they will not ‘hide’ with the previous set zero size.As you will have multiple links, you may need to use
:nth-child
to properly set each icon.See the example (here is the jsfiddle):
CSS
This is a HTML sample that works well:
HTML
* * * EDIT * * *
As the
nth-child
is a CSS3 selector and some old – but still active – browsers may not support it properly, you can check whether you are able to set the icon as a data attribute in each menu link so we can replace this selector with the following css rule:CSS
HTML