And I want it to completely disappear with a fadeOut when the mouse is idle, except for when it is at the top of the page (so no scroll has been made).
You’re probably right. I have there jQuery, but I call it at the end of the document. The problem is that I tried adding that new script code at the beginning (just to test it out, then I would have moved it down).
Ok, I moved it to the end but it still doesn’t work.
Can you please tell me how did you find out about that error? I tried opening Tools → Dev Tools → Console in Firefox but I can’t find it anywhere. Maybe the Console is not te right place I should look at?
Ok, found! I needed to open the “other” console with Shift + J!
Ok, then I moved the script at the end. In the script code I have var header = $("#fademe"); and in the HTML I have <div id="fademe">[content I want faded]</div> but I still get that error (ReferenceError: $ is not defined). Why?
Thank you for your replies, PaulOB. Strange. I’m using CloudFlare, but the jQuery is loaded correctly (if it wasn’t there I would had some problems with eg. the dropdown menus at the top). This is the code that I have right now at the end of my HTML code, before the jQuery library is called:
<script type="text/javascript">
$(window).on('scroll', function() {
var header = $("#fademe");
if ($(this).scrollTop() > 50) {
if (!header.data('faded')) header.data('faded', 1).stop(true).fadeTo(300, 0);
} else if (header.data('faded')) {
header.data('faded', 0).stop(true).fadeTo(300, 1);
}
});
</script>
and the navbar I want to hide/fadeout is enclosed in this code:
<div id="fademe">[nav code]</div>
I sincerely don’t know what else I could try here.
Can’t you just jquery normally and see what happens. I’m assuming you are delaying the loading of the scripts which means that the inline script runs straight away and causes the error.
How about packaging that new inline script in the same way that you have done for the others?
Thank you a lot, Paul!
It seems to be a little bit buggy. When you scroll, sometimes it is shown and sometimes it is not, plus it doesn’t work when you just move the mouse…
Anyway, thank you A LOT, again.
Yes it was pretty simplistic All it does is set the sticky header to visible if you scroll and then sets a timeout to fade it out which assumes you have stopped scrolling by then.
You will need a much more involved script to achieve what you want so hopefully one of the JS experts will jump in with a better method
Hi Norman, the reason for this is that there’s no “mousemove” event handler. ;-) BTW, you don’t need to define that fadeHeader function each time you scroll – one time outside that callback is enough. What you need to do each time however is to clear the timeout and set it anew. Try this code at the bottom of your page:
Ough, I just deleted my message, so I fortunately saved a copy:
Thank you a lot to both of you, guys. @m3g4p0p, your script is just what I needed! Thanks a billion.
The problem for me now is how to apply the same to that navigation bar that is appended on scroll. It doesn’t enter in the #fademe div.
I thought that I could be able to just add that #fademe also to that element… but the problem is that I only have a <section> there and I add a class to it still via jQuery in order to make it
stickyed. Umhhh… right now I’m not coming with an idea for it…
I also tried removing first “scroll” and then “mousemove”… this because, for usability, probably would be best to have it appear only when you scroll the page, BUT stay active when you mouseover it. What do you think?
Nooooo! Every ID is supposed to be unique! You might use a dedicated class for this behaviour, and which is independent of the sticky-behaviour of the navigation bar. (Sorry I can’t have a closer look at your page right now…)
Yes, certainly! :-) But removing one of those event listeners won’t help, since neither of them has anything to do with the mouse hovering the element. The .hover() event would be responsible for this; however in this case you might use the :hover selector together with jQuery’s .is() filter (which we’ve been using above as well), like this:
Oh, true! You’re completely right! I just didn’t thought it was an ID and not a class!
Thank you again, m3g4p0p! I’m gonna try it…
All this stuff that you post me here/teach me here will surely help me a lot in the future when I’ll have some good time to invest in order to completely learn jQuery! So thanks a lot.
I implemented it and it works great! Thank you again.
I used another ID for the navbar and then now I have this code (which probably could be “merged” somehow as it does have some repetition. I don’t know!):
Anyway, maybe I dreamt too much. I mean, this is a cool effect, indeed… but seems to be still a little bit forced in my case. What do you think, as a “user” who browse the website? I’m undecided.