I have some code for a specific timeline, the problem is : the background item in the timeline does not show up at the right time when scroll (top and bottom)
I need te display the bg when the concerned item is centred vertically, here for example, there is a display offset in the last and penultimate background.
Please take a look to the GIF : https://gfycat.com/possiblesadcowbird
JS code (i took it from : https://codepen.io/knyttneve/pen/bgvmma?editors=0010 and edited it according to my project) :
var fn_timeline = function() {
$.fn.timeline = function() {
var selectors = {
id: $(this),
item: $(this).find(".timeline-item"),
activeClass: "timeline-item--active",
img: ".o-timeline__img > img"
};
selectors.item.eq(0).addClass(selectors.activeClass);
selectors.id.css("background-image", "url(" + selectors.item.first().find(selectors.img).attr("src") + ")");
var itemLength = selectors.item.length;
$(window).scroll(function() {
var max, min;
var pos = $(this).scrollTop();
selectors.item.each(function(i) {
min = $(this).offset().top;
max = ($(this).height() + $(this).offset().top);
var that = $(this)
if (i == itemLength - 2 && pos > min + $(this).height() / 2) {
selectors.item.removeClass(selectors.activeClass);
selectors.id.css("background-image", "url(" + selectors.item.last().find(selectors.img).attr('src') + ")");
selectors.item.last().addClass(selectors.activeClass)
} else if (pos <= max - 40 && pos >= min) {
selectors.id.css("background-image", "url(" + $(this).find(selectors.img).attr('src') + ")");
selectors.item.removeClass(selectors.activeClass);
$(this).addClass(selectors.activeClass);
}
});
});
}
$("#timeline-1").timeline();
}
HTML code:
<div class="timeline-wrapper container-content container-content--smaller container-content--fake-col timeline1col ">
<div class="timeline-container" id="timeline-1" style="background-image: url("img/tl-3.jpg");">
<div class="timeline">
<div id="fullpage">
<div class="section timeline-item-wrapper timeline-item">
<div class="timeline-marker"></div>
<div class="timeline__content">
<div class="timeline-minature">
<div class="o-timeline__img timeline-img">
<img src="img/tl-1.jpg" style="width: 350px">
</div>
</div>
<h2 class="timeline__content-title">Le premier ordinateur</h2>
<div class="wysiwyg">
He was born in 1881 (probably in the spring) in Salonica, then an Ottoman city, now inGreece. His father Ali Riza, a customs official turned lumber merchant, died when Mustafawas still a boy. His mother Zubeyde, adevout and strong-willed woman, raised him and his sister.
</div>
<a href="#" class="a-link--border-effect timeline-link">
* Qu’est-ce que la géodésie ?
</a>
</div>
</div>
<div class="section timeline-item-wrapper timeline-item">
<div class="timeline-marker"></div>
<div class="timeline__content">
<div class="timeline-minature">
<div class="o-timeline__img timeline-img">
<img src="img/tl-2.jpg" style="width: 350px">
</div>
</div>
<p class="timeline__content-date">1991</p>
<div class="wysiwyg">
He was born in 1881 (probably in the spring) in Salonica, then an Ottoman city, now inGreece. His father Ali Riza, a customs official turned lumber merchant, died when Mustafawas still a boy. His mother Zubeyde, adevout and strong-willed woman, raised him and his sister.
</div>
</div>
</div>
<div class="section timeline-item-wrapper timeline-item timeline-item--active">
<div class="timeline-marker"></div>
<div class="timeline__content">
<div class="timeline-minature">
<div class="o-timeline__img timeline-img">
<img src="img/tl-3.jpg" style="width: 350px">
</div>
</div>
<h2 class="timeline__content-title">Le premier ordinateur</h2>
<div class="wysiwyg">
He was born in 1881 (probably in the spring) in Salonica, then an Ottoman city, now inGreece. His father Ali Riza, a customs official turned lumber merchant, died when Mustafawas still a boy. His mother Zubeyde, adevout and strong-willed woman, raised him and his sister.
</div>
<a href="#" class="a-link--border-effect timeline-link">
* Qu’est-ce que la géodésie ?
</a>
</div>
</div>
<div class="section timeline-item-wrapper timeline-item">
<div class="timeline-marker"></div>
<div class="timeline__content">
<div class="o-timeline__img timeline-bg">
<img src="img/tl-4.jpg" alt="">
</div>
<h2 class="timeline__content-title">just bg</h2>
<div class="wysiwyg">
He was born in 1881 (probably in the spring) in Salonica, then an Ottoman city, now inGreece. His father Ali Riza, a customs official turned lumber merchant, died when Mustafawas still a boy. His mother Zubeyde, adevout and strong-willed woman, raised him and his sister.
</div>
</div>
</div>
</div>
</div>
</div>
</div>