html 纯CSS滚动阴影

vfhzx4xs  于 2022-12-09  发布在  其他
关注(0)|答案(2)|浏览(176)

I want to enable scroll shadows when content exceeds the available width. This I am trying to achieve with pure CSS (no JS). Using a technique I found in various articles, I can achieve my goal with multiple CSS backgrounds and background-attachment .
The below code is working fine if the content is text only. However, in the case of buttons, the shadow backgrounds get displayed behind the buttons. How can I display these shadows above the buttons?

Expected behavior:

Scenario A: enable shadow only at right side, as scroll bar is at extreme left
Scenario B: enable shadows on both right & left side, as scroll bar is somewhere in the middle
Scenario C: enable shadow only at left side, as scroll bar is at extreme right

Example:

/**
 * 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: 160px;
  margin: 0 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;
}
<div class="scrollbox">
  <ul>
    <li>Ah! Scroll below!</li>
    <li><button>Button</button></li>
    <li><button>Button</button></li>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li><button>Button</button></li>
    <li><button>Button</button></li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
    <li>10</li>
    <li>The end!</li>
    <li>No shadow there.</li>
  </ul>
</div>
eoigrqb6

eoigrqb61#

对于文本也会发生这种情况,但是您看不到它,因为它们的颜色相同。一个解决方案是对子对象使用比父对象更小的z-index。要实现这一点,您还需要另外两个条件:

  1. position,而不是子项和父项的static
  2. .scrollbox的透明背景

溶液

.scrollbox li * {
  z-index:0;
  position:relative;
}
.scrollbox {
  z-index:1;
  position:relative;
  background-color:transparent;
}

第一次
剩下的唯一问题是,您需要一个坚实的背景颜色,这才能工作。

编辑:

子节点的z-index必须小于父节点的z-index,但是如果像我建议的那样使用负的z-index,像单击和悬停这样的事件将停止工作。

km0tfn4u

km0tfn4u2#

.scrollGradient {
  background: 
    linear-gradient(#f429b7 33%, rgba(244,41,183,0)),
    linear-gradient(rgba(244,41,183, 0), #f429b7 66%) 0 100%,
    radial-gradient(farthest-side at 50% 0, rgba(34,34,34, 0.5), rgba(0,0,0,0)),
    radial-gradient(farthest-side at 50% 100%, rgba(34,34,34,0.5), rgba(0,0,0,0)) 0 100%;
  background-color: #f429b7;
  background-repeat: no-repeat;
  background-attachment: local, local, scroll, scroll;
  background-size: 100% 45px, 100% 45px, 100% 15px, 100% 15px;
}

相关问题