I want to show a hidden div when scrolling down after 800px from the top of the page. By now I have this example, but I guess it needs modification in order to achive what I am looking for.
EDIT:
[And when scrollUp and the height is less the 800px, this div should hide]
HTML:
<div class="bottomMenu">
<!-- content -->
</div>
css:
.bottomMenu {
width: 100%;
height: 60px;
border-top: 1px solid #000;
position: fixed;
bottom: 0px;
z-index: 100;
opacity: 0;
}
jQuery:
$(document).ready(function() {
$(window).scroll( function(){
$('.bottomMenu').each( function(i){
var bottom_of_object = $(this).position().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
if( bottom_of_window > bottom_of_object ){
$(this).animate({'opacity':'1'},500);
}
});
});
});
Here is a Fiddle of my current code.
If you want to show a div after scrolling a number of pixels:
Working Example
Its simple, but effective.
Documentation for .scroll()
Documentation for .scrollTop()
If you want to show a div after scrolling a number of pixels,
without jQuery:
Working Example
Documentation for .scrollY
Documentation for .className
Documentation for .addEventListener
If you want to show an element after scrolling to it:
Working Example
Note that you can't get the offset of elements set to
display: none;
, grab the offset of the element's parent instead.Documentation for .each()
Documentation for .parent()
Documentation for .offset()
If you want to have a nav or div stick or dock to the top of the page once you scroll to it and unstick/undock when you scroll back up:
Working Example
SCROLLBARS &
$(window).scrollTop()
What I have discovered is that calling such functionality as in the solution thankfully provided above, (there are many more examples of this throughout this forum - which all work well) is only successful when scrollbars are actually visible and operating.
If (as I have maybe foolishly tried), you wish to implement such functionality, and you also wish to, shall we say, implement a minimalist "clean screen" free of scrollbars, such as at this discussion, then
$(window).scrollTop()
will not work.It may be a programming fundamental, but thought I'd offer the heads up to any fellow newbies, as I spent a long time figuring this out.
If anyone could offer some advice as to how to overcome this or a little more insight, feel free to reply, as I had to resort to show/hide onmouseover/mouseleave instead here
Live long and program, CollyG.
You can also, do this.
You've got a few things going on there. One, why a class? Do you actually have multiple of these on the page? The CSS suggests you can't. If not you should use an ID - it's faster to select both in CSS and jQuery:
Second you've got a few crazy things going on in that CSS - in particular the z-index is supposed to just be a number, not measured in pixels. It specifies what layer this tag is on, where each higher number is closer to the user (or put another way, on top of/occluding tags with lower z-indexes).
The animation you're trying to do is basically .fadeIn(), so just set the div to display: none; initially and use .fadeIn() to animate it:
.fadeIn() works by first doing display: (whatever the proper display property is for the tag), opacity: 0, then gradually ratcheting up the opacity.
Full working example:
http://jsfiddle.net/b9chris/sMyfT/
CSS:
JS: