I’m building a local website on the Bitnami WordPress Stack with the Arras theme, if that’s important.
I’m making a fixed menu that I want to show after I have scrolled 190 pixels down on the page. The problem is that anything works, no matter which JQuery or JavaScript code I try. I have searched and searched here on StackOverflow, and I know that this question have been asked numerous times here before – but I have tried every code I could find, and none works. This is my JavaScript/JQuery/HTML/PHP code for my menu, placed in the header.php file:
<div class="medfolg" id="medfolg">
<script type="text/javascript">
$(document).ready(function(){
$(window).bind('scroll', function(){
if($(window).scrollTop() > 190){
$('#medfolg').show();
} else {
$('#medfolg').hide();
};
});
});
</script>
<?php
if ( function_exists('wp_nav_menu') ) {
wp_nav_menu( array(
'menu' => 'medfolg',
'menu_class' => 'sf-menu'
) );
}
?>
</div>
And this is the CSS code I have placed in my default.css file:
#medfolg.medfolg {position:fixed;}
#medfolg { text-transform: lowercase; position: absolute; top: 0; width: 100%; background: #f5f5f5; z-index:5000; display: none;}
#medfolg .menu-medfolg-container { width: 980px; margin: 0 auto; }
#medfolg .sf-menu { position: relative; top:3px !important; }
#medfolg .sf-menu a { font-size: 22px; color: #444; margin-right: 15px;}
I desperately need some help – please!
EDIT: I’ve made a jsFiddle here with only small modifications (WordPress .php menu cannot be read on other places than WordPress): http://jsfiddle.net/wHMjr/
For completeness, I will post my code:
First, the code is wrapped with a self-executing function to prevent conflict between libraries:
Then, I create the handler:
which will be attached to the window’s scroll event and change the menu’s visibility according to the scroll status.
The attachment is done by binding the function to the event:
The last line in the above section sets the initial state of the menu, as it is possible that the initial scroll value would require it to be visible (e.g, a link to a lower subsection of the page).
The entire process is initiated when the document’s markup is ready.
See demo here.
Try using vanilla js instaead of $
(window).scrollTop()
trywindow.scrollY > 190
for the complete function I would use
toggle
too so:jQuery
HTML
Demo