我似乎不知道如何設計一個像下圖這樣的可滾動的div。我找到的教程和例子都把div淡出到背景色,但是我找不到這樣的,有人有解決方法嗎?不需要有內部div和標題如下,只需要保持白盒不褪色,謝謝!
您可以在支持CSS mask-image的瀏覽器中使用它(這在當今相當普遍)。根據您的需要,可能需要JS(例如,如果您希望根據用戶是否已經滾動到元素的頂部/底部來有條件地切換淡化)。
創建一個& quot底部淡出& quot效果,您可以使用以下CSS規則:
mask-image: linear-gradient(to bottom, black calc(100% - 48px), transparent 100%);
它基本上說:
從頂部到底部創建一個線性漸變 當我們離底部48px的時候,開始把顏色從黑色(蒙版時透明)變成透明(蒙版時不透明) 然而,這僅產生底部梯度。要同時遮罩頂部和底部,您需要將兩個漸變合并為一個:
mask-image: linear-gradient(to bottom, transparent 0%, black 48px, black calc(100% - 48px), transparent 100%);
現在你會問:我們如何隱藏面具?簡單:如果色標間隔為0px,蒙版將全部為黑色,這將使蒙版完全透明。
JS部分只是使用類來切換這些顏色停止。我們使用CSS自定義屬性,可以通過添加/刪除類來覆蓋這些屬性。JS用于嗅出滾動位置并切換這些類。請參見下面的概念驗證:
function setClasses(el) {
const isScrollable = el.scrollHeight > el.clientHeight;
// GUARD: If element is not scrollable, remove all classes
if (!isScrollable) {
el.classList.remove('is-bottom-overflowing', 'is-top-overflowing');
return;
}
// Otherwise, the element is overflowing!
// Now we just need to find out which direction it is overflowing to (can be both)
const isScrolledToBottom = el.scrollHeight <= el.clientHeight + el.scrollTop;
const isScrolledToTop = isScrolledToBottom ? false : el.scrollTop === 0;
el.classList.toggle('is-bottom-overflowing', !isScrolledToBottom);
el.classList.toggle('is-top-overflowing', !isScrolledToTop);
}
document.querySelector('#content').addEventListener('scroll', (e) => {
const el = e.currentTarget;
setClasses(el);
});
setClasses(document.querySelector('#content'));
* {
box-sizing: border-box;
}
body {
padding: 0;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background-color: #2a9d8f;
font-family: Arial, sans-serif;
}
.box {
width: 50vw;
height: 90vh;
background-color: #fff;
border-radius: 16px;
padding: 32px;
display: flex;
flex-direction: column;
}
.box h1 {
margin: 0;
}
#content {
overflow-y: auto;
-webkit-mask-image: linear-gradient(to bottom, transparent 0, black var(--top-mask-size, 0), black calc(100% - var(--bottom-mask-size, 0)), transparent 100%);
mask-image: linear-gradient(to bottom, transparent 0, black var(--top-mask-size, 0), black calc(100% - var(--bottom-mask-size, 0)), transparent 100%);
--top-mask-size: 0px;
--bottom-mask-size: 0px;
}
#content.is-top-overflowing {
--top-mask-size: 48px !important;
}
#content.is-bottom-overflowing {
--bottom-mask-size: 48px !important;
}
<article class="box">
<h1>Lorem ipsum</h1>
<section id="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque vel metus at tellus consectetur rhoncus. Aenean euismod eget mauris cursus tincidunt. Cras lectus dolor, suscipit nec porttitor a, tincidunt vel arcu. Etiam facilisis faucibus lectus
vitae pharetra. Fusce euismod lacus sit amet consequat mattis. Aliquam suscipit metus a nulla suscipit varius. In tempor suscipit pretium.</p>
<p>Vivamus aliquam eros eu orci finibus, id efficitur lorem placerat. Sed congue ipsum quis accumsan feugiat. Nunc imperdiet faucibus tellus, nec tincidunt ipsum dictum non. Quisque dui lacus, bibendum vitae lectus vel, vulputate tempus quam. Praesent
ullamcorper ultricies felis, at fermentum massa gravida quis. Pellentesque mollis, urna sed vehicula elementum, dolor lorem congue ipsum, eget ullamcorper ex arcu nec mi. Suspendisse potenti. Morbi pharetra eu nisi quis laoreet. Donec ut quam id
justo pulvinar volutpat eget ut magna.</p>
<p>Etiam aliquam eleifend dignissim. Donec sagittis tincidunt quam, eget venenatis mi sollicitudin et. Vestibulum id lectus mi. Aenean enim sem, viverra at velit ut, posuere dapibus tellus. Ut accumsan mi eu lectus sollicitudin ornare. Morbi eu semper
lacus lacus. Aenean id erat at nulla ornare consequat a at leo. In risus risus, blandit sit amet tortor sed, accumsan porttitor velit. Quisque interdum id ipsum quis convallis. Suspendisse vel pretium augue. Sed tincidunt, felis ut porta consectetur,
mauris urna hendrerit diam, nec aliquet augue ante quis metus.</p>
</section>
</article>