div element should scroll down till it gets in contact with another element - javascript

I've an div element #1 which stick on top the screen while scrolling. Thats working so far. Now further down on the page there is another element #2 and the element #1 should stop scrolling when it gets in contact with #2. To illustrate the scenario I made a sick picture with my paint skills.
Do somebody have an idea how to do it?

$el1.offset().top + $el1.outerHeight();
this will give you the bottom position of first div. Now When you scroll check if taht bottom position of first div is less than top position of second div. if it is less its ok but if it is greater than you can do whatever you want with first div. You can hide it on make its position absolute.
$(window).scroll(function(){
var bott = $el1.offset().top + $el1.outerHeight();
if(bott > $el2.offset().top){
$el1.css('display','none');
// OR
$el1.css('position','absolute');
}
});
hope this helps

Related

How to stay in same scroll position when adding items to the top of a div?

I want when the user scrolls from the bottom to the top of a div to load more items into that div to the top part of the div and I want to maintain the scroll position (or rather make sure the top item before adding more items is still visible after adding the items)
For example if I have item 100 on the top and 100 more items has been loaded into the div then I want item 100 to still be the focus of the scroll position, currently when I add items to the div it jumps to item 200...
I'd love to get help with this issue, thanks in advance :)
I found this answer useful, I just created the same logic but in React
jquery - keep window from changing scroll position while prepending items to a list?

jQuery scrolling out of view issue

I am working on building a schedule. So far it's pretty straight-forward. There is one bit of functionality I am having issues with.
I have an ul that has a fixed height to allow scrolling. There are "Labels" (li.dayLabel) withing the ul that separate the hours. What I am trying to do is to have it so that when a label is scrolled out of view it will change the text in the placeholder to it's text. Then once that works, I need it to work in reverse. So when they label scrolls back into view it updates the placeholder. Basically, I am trying to make the placeholder be a title for the available items until another label is then scrolled out of view. This is for scrolling from the top. So as you scroll down the list the placeholder is meant to be a title for the section you are viewing until you reach another section and it takes its place. Then when you scroll back down I need it to replace the text with the previous li.dayLabel so the sections stay organized. I hope this makes sense.
You can see what I am trying to do by looking at the original that I am basing this off of. Notice how the placeholder changes as you scroll down the list and changes back when you scroll back up.
Demo: jsFiddle // Note: line 54 is the part that is in question
I originally used:
$(".snlf-schedule-list li.dayLabel:visible:first").text();
as the :first selector is suppose to only match a single element.
I later tried:
$(".snlf-schedule-list li.dayLabel:visible").filter(":eq(0)")
as this is suppose to be the same thing.
It seems that when an element is out of view it still is considered :visible I believe this is my issue.
Am I doing this completely wrong? I was under the impression that when you scroll an element like this it should no longer be :visible. After reading the documentation I have learned that this is not the correct selector to use.
It would appear that scrollTop is how I should be doing this. Now I have used scrollTop for scrolling down pages to disable animations when not in view but I am not clear on how to untilize this for a ul with scrollbars.
I tried:
var _first = $('li.dayLabel:first'); // next element to scroll out of view?
if( $(this).scrollTop() > (_first.offset().top+_first.height())) {
// Out of view
console.log("out");
} else {
// in view
console.log("in");
}
Updated Demo: jsFiddle
But it seems to be redundant as it's already calculating the first element so I am not sure how to get the correct element (the next one that's about to scroll out of view.) Then I need this to work when they scroll back up...
Any insight on this is much appreciated. Hopefully it's something simple I am just over complicating or missing completely.
Thanks,
Jeremy
The solution for my case was:
// Set placeholder text on scroll
var _scrollCagePositionTop = $(".snlf-schedule-list").offset().top;
var _first = $('li.dayLabel:first'); // first dayLabel element
$(".snlf-schedule-list").scroll(function(){
var _lastOffText = $(_first).text();
$("li.dayLabel").each(function() {
if ($(this).offset().top < _scrollCagePositionTop) {
_lastOffText = $(this).text();
}
});
$("#schedule-placeholder").text(_lastOffText);
});
What I did was set the known position of the top of the scroll cage (_scrollCagePositionTop)
When the user scrolls I set a variable _lastOffText that keeps track of the last item text content when scrolled out of view (less offset top than the palceholder). I then set this value to the placeholder.
This method allows me to have the proper text in my placeholder when the user scrolls up or down.
To fix the issue of an empty placeholder when the user scrolls back to the top I just set the default of _lastOffText to be the text of the first label ($(_first).text())
Hope others find this useful.
Here is the working demo: jsFiddle Final

Div appear after scroll past a div

I know there are a lot of tutorials to let a div appear after scroll down a certain amount of pixels.
But what I want is to let a div appear when the user scrolls past another div. And when they scroll back up it has to disappear again.
Why not after an amount of pixels? I want to use this for my menubar to appear on the top of my page after the user scrolls past the banner image. BUT when you scale down the browser (mobile, tablet, small screen...) the banner image will scale down to! The image will not be the same height as before. The menubar would appear to late or to earlier;) that's why I want the menubar to appear after that image (div).
Hope you guys can help!
Now that we are talking in abstract terms, I will try to explain to the best of my ability.
Can't you get the current position of the targetDiv (the div, after which scrolling down would show the menubar), and then use the same code of making the menuDiv appear after scroll down a certain amount of pixels.
This answer might help you in getting the current position of the targetDiv.
So, do the following steps and let me know how it went:
1) Get jquery.appear
2) Apply the disappear event on your selector:
$('someselector').on('disappear', function(event, $all_disappeared_elements) {
// this element is now outside browser viewport
});
3) On that event just make the other div dissapear.
$('theStuffToHide').toggle()
4) Final result
$('someselector').on('disappear', function(event, $all_disappeared_elements) {
$('theStuffToHide').toggle()
});
This should be the only thing you need.
Cheers, hope it helps.
You can check for the pageoffset to cross the amount (top offset of the target + height of the target element ) and show the div.
And when the pageoffset comes less that the calculated amount, make the div hide

Transitioning and autoscrolling divs

I have created a HTML page with different sections. Each section is contained within a DIV. My requirement is to make transition between each divs. For Eg, after couple of seconds, section1 blurs out and section2 blurs in and so on. Now within a particular section I want the section contents to autoscroll until the end of the section is reached, from bottom to top. I am able to create toggle between different sections but am not able to scroll the contents of each section, so that for eg, the first section autoscrolls and when the end of section is reached, it only then goes to another section and then scrolls that. Can anybody please help me with this? I have paste my code at below link,
http://pastebin.com/rE8h5NK0
Also, I have given fixed position to each section heading but when I minimize the page the heading doesnt align correctly. And if I do not have it fixed, it is not properly visible on the page.
I would suggest running JQuery scrollTop in a loop, looking for when the scrolling reaches the bottom by matching the div's height minus the container's height with the amount scrolled:
function scroll() {
if ($('#div').scrollTop() == $('#div').height() - $('#container').height()) {
scrollInterval.clearInterval();
// Transition to next section
} else {
$('#div').scrollTop($('#div').scrollTop() + 1);
}
}
var scrollInterval = setInterval(scroll,20 /* Scroll speed */);

Auto scoll a page when an image is at the top of the window

Ok I have a page with stacked div's and anchors which use a jquery slide slide down. When the bottom of a stacked div reaches the top of the screen I want the jquery to automatically scroll to the next div, which may be 1000px south.
How can i do this in javascript?
my idea is to put an image the bottom of the div and somehow check the screen to see if the image is in the top half of the monitor which would call a function to scroll to the next anchor..
Is this possible?
var wHeight =$(window).height()/2;
$(window).scroll(function(){
var imgOff = $('#two').offset().top-$(window).scrollTop();
if(imgOff<wHeight){
alert('Kill BILL!');
}
});
havent tried this out but it should work
Edit: this works try it here
http://jsfiddle.net/BG2Cz/
P.S. Be warned it will spam alerts once u pass the middle

Categories