【发布时间】:2021-01-13 10:21:44
【问题描述】:
我有一些特定时间线的代码,问题是: 滚动(顶部和底部)时,时间轴中的背景项目不会在正确的时间显示
我需要在相关项目垂直居中时显示背景,例如在最后一个和倒数第二个背景中有一个显示偏移。
请看一下GIF:https://gfycat.com/possiblesadcowbird
JS 代码(我取自:https://codepen.io/knyttneve/pen/bgvmma?editors=0010 并根据我的项目进行了编辑):
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 代码:
<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>
【问题讨论】:
-
背景附件有什么问题:已修复;像这样:w3schools.com/howto/tryhow_css_parallax_demo.htm
-
嗨,谢谢你的评论,但它不适合我所做的......我必须更改所有的css和js,我没有时间:(
-
您还需要添加图像预加载。 stackoverflow.com/questions/3646036/…
标签: javascript html jquery timeline