当内容超出可用宽度时,我需要启用滚动阴影.这我想用纯css(没有JS)来实现.我遇到了很多文章,可以用css多背景和背景附件来实现.如果内容是文本类型,则使用以下jsfilldle代码正常工作,在按钮情况下背景位于按钮后面.所以在视觉上不起作用.
预期行为:
场景A:应该只在右侧启用阴影,因为滚动条位于最左侧
场景B:应该启用阴影权限和左侧作为滚动条位于中间,从左侧移动一部分
场景C:应该仅在左侧启用阴影,因为滚动条位于最右侧.
例如:
/**
* Scrolling shadows by @kizmarh and @leaverou
* Only works in browsers supporting background-attachment: local; & CSS gradients
* Degrades gracefully
*/
html {
background: white;
font: 120% sans-serif;
}
.scrollbox {
overflow: auto;
width: 200px;
max-height: 200px;
margin: 50px auto;
background:
/* Shadow covers */
linear-gradient(white 30%, rgba(255, 255, 255, 0)), linear-gradient(rgba(255, 255, 255, 0), white 70%) 0 100%,
/* Shadows */
radial-gradient(50% 0, farthest-side, rgba(0, 0, 0, .2), rgba(0, 0, 0, 0)), radial-gradient(50% 100%, farthest-side, rgba(0, 0, 0, .2), rgba(0, 0, 0, 0)) 0 100%;
background:
/* Shadow covers */
linear-gradient(white 30%, rgba(255, 255, 255, 0)), linear-gradient(rgba(255, 255, 255, 0), white 70%) 0 100%,
/* Shadows */
radial-gradient(farthest-side at 50% 0, rgba(0, 0, 0, .2), rgba(0, 0, 0, 0)), radial-gradient(farthest-side at 50% 100%, rgba(0, 0, 0, .2), rgba(0, 0, 0, 0)) 0 100%;
background-repeat: no-repeat;
background-color: white;
background-size: 100% 40px, 100% 40px, 100% 14px, 100% 14px;
/* Opera doesn't support this in the shorthand */
background-attachment: local, local, scroll, scroll;
}
<h1>CSS-only shadow-scrolling effect.</h1>
<div class="scrollbox">
<ul>
<li>Ah! Scroll below!</li>
<li><button>Button</button></li>
<li><button>Button</button></li>
<li><button>Button</button></li>
<li><button>Button</button></li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>The end!</li>
<li>No shadow there.</li>
</ul>
</div>
最佳答案 对于文本也会发生这种情况,但您无法看到它,因为它们具有相同的颜色.一个解决方案是:儿童的z-index低于父母的z-index.为此,您需要另外两件事:
>孩子和父母的静态位置
> .scrollbox的透明背景
解
.scrollbox li * {
z-index:0;
position:relative;
}
.scrollbox {
z-index:1;
position:relative;
background-color:transparent;
}
/**
* Scrolling shadows by @kizmarh and @leaverou
* Only works in browsers supporting background-attachment: local; & CSS gradients
* Degrades gracefully
*/
html {
background: white;
font: 120% sans-serif;
}
.scrollbox {
overflow: auto;
width: 200px;
max-height: 200px;
margin: 50px auto;
background:
/* Shadow covers */
linear-gradient(white 30%, rgba(255, 255, 255, 0)), linear-gradient(rgba(255, 255, 255, 0), white 70%) 0 100%,
/* Shadows */
radial-gradient(50% 0, farthest-side, rgba(0, 0, 0, .2), rgba(0, 0, 0, 0)), radial-gradient(50% 100%, farthest-side, rgba(0, 0, 0, .2), rgba(0, 0, 0, 0)) 0 100%;
background:
/* Shadow covers */
linear-gradient(white 30%, rgba(255, 255, 255, 0)), linear-gradient(rgba(255, 255, 255, 0), white 70%) 0 100%,
/* Shadows */
radial-gradient(farthest-side at 50% 0, rgba(0, 0, 0, .2), rgba(0, 0, 0, 0)), radial-gradient(farthest-side at 50% 100%, rgba(0, 0, 0, .2), rgba(0, 0, 0, 0)) 0 100%;
background-repeat: no-repeat;
background-color: transparent;
background-size: 100% 40px, 100% 40px, 100% 14px, 100% 14px;
/* Opera doesn't support this in the shorthand */
background-attachment: local, local, scroll, scroll;
z-index:1;
position:relative;
}
.scrollbox li * {
z-index:0;
position:relative;
}
<h1>CSS-only shadow-scrolling effect.</h1>
<div class="scrollbox">
<ul>
<li>Ah! Scroll below!</li>
<li><button>Button</button></li>
<li><button>Button</button></li>
<li><button>Button</button></li>
<li><button>Button</button></li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>The end!</li>
<li>No shadow there.</li>
</ul>
</div>
剩下的唯一问题是你需要一个坚实的背景颜色才能工作.
编辑:
子项的z-index需要低于父项的z-index,但如果你使用负z-index,就像我建议的那样,点击和悬停等事件就会停止工作.我正确地更改了代码.