<div id="scroller" class="scroller">
<ul>
<li><img src="avatar1.jpg"><h4>用戶 1</h4><p>內容...</p></li>
<li><img src="avatar2.jpg"><h4>用戶 2</h4><p>內容...</p></li>
<li>...</li>
</ul>
</div>
.scroller { width: 380px; height: 300px; border: 1px solid #ddd; overflow: hidden; position: relative; }
.scroller ul { position: absolute; left: 0; top: 0; margin: 0; padding: 10px; list-style: none; }
.scroller li { margin-bottom: 16px; opacity: 1; transition: opacity .3s, transform .3s; }
.scroller li.hidden { opacity: 0; transform: translateY(-20px); pointer-events: none; }
class VerticalScroller {
constructor(containerSel, options = {}) {
this.$container = $(containerSel);
this.$ul = this.$container.find('ul');
this.$items = this.$ul.children('li');
this.interval = options.interval || 3000;
this.animDur = options.animDur || 600;
this.timer = null;
this._bindHover();
this._initItems();
this.start();
}
_initItems() {
this.$items = this.$ul.children('li');
this.itemCount = this.$items.length;
if (this.itemCount < 2) return; // 單項無需滾動
}
_bindHover() {
this.$container.hover(
() => this.pause(),
() => this.resume()
);
}
_getFirstHeight() {
const $first = this.$items.first();
return $first.outerHeight(true); // 含 margin
}
step() {
if (this.itemCount < 2) return;
const $first = this.$items.first();
const h = $first.outerHeight(true);
// 動畫:先向上移出
this.$ul.animate({ marginTop: -h }, this.animDur, () => {
// 移動到隊尾
$first.appendTo(this.$ul);
this.$ul.css('marginTop', 0);
this._initItems(); // 更新引用
// 新項目淡入
this.$items.last().addClass('hidden').removeClass('hidden'); // 觸發 transition
});
}
start() {
this.pause();
this.timer = setInterval(() => this.step(), this.interval);
}
pause() { clearInterval(this.timer); this.timer = null; }
resume() { if (!this.timer) this.start(); }
// 公開 API:動態新增
addItem(html) {
const $li = $(html).hide();
this.$ul.append($li);
this._initItems();
$li.slideDown();
}
removeItem(index) {
this.$items.eq(index).slideUp(() => { this._initItems(); });
}
}
// 初始化
const scroller = new VerticalScroller('#scroller', { interval: 3000, animDur: 600 });
// 動態新增示範
setInterval(() => scroller.addItem(`新消息 ${Date.now()}
動態插入
`), 15000);
<style>
@keyframes scrollUp {
from { transform: translateY(0); }
to { transform: translateY(-100%); } /* 需配合 clone 技巧 */
}
.scroller-native { overflow: hidden; position: relative; height: 300px; }
.scroller-native ul { display: flex; flex-direction: column; animation: scrollUp 20s linear infinite; }
.scroller-native:hover ul { animation-play-state: paused; }
/* 無縫循環技巧:JS 克隆首項到尾部,或 CSS duplicate 内容 */
const observer = new IntersectionObserver(entries => {
entries.forEach(en => {
if (en.isIntersecting) trackView(en.target.dataset.id);
});
}, { root: document.querySelector('.scroller'), threshold: 0.5 });
scroller.$items.each((i, el) => observer.observe(el));
垂直無縫滾動的核心是**「動畫位移 → DOM 移動 → 重置位置」**三步曲。關鍵細節:`outerHeight(true)` 含 margin、動畫佇列管理 (`stop(true,true)`)、動態增刪後重新計算、減少動畫偏好尊重。現代專案優先考慮 **CSS Animation + `animation-play-state`** 零 JS 方案,複雜互動才上 JS Class 封裝。