我正在嘗試建立一個(gè)簡(jiǎn)單的「主要是 css」圖像輪播,它使用滾動(dòng)捕捉,但也有導(dǎo)航按鈕來(lái)控制當(dāng)前查看的幻燈片。我找到的唯一解決方案是在目標(biāo)元素上使用scrollIntoView(),它可以滿(mǎn)足我的要求,但它也總是將目標(biāo)元素滾動(dòng)到頁(yè)面頂部。我希望目標(biāo)輪播?%B
嘗試將 block: 'nearest'
加入到你的scrollIntoView選項(xiàng)中,如下所示:
document.querySelectorAll('button').forEach(button => { button.addEventListener('click', event => { console.log('event',event); slides[event.target.dataset.index].scrollIntoView({ behavior: 'smooth', inline: 'center', block: 'nearest', }); }); });