添加水平滚动阴影效果

mfr*_*ale 7 css mobile bootstrap-4

我想添加一个插入阴影以指示有更多内容要滚动。我能解释它的最好方法是用一张图:

在此处输入图片说明

我使用 Bootstrap 作为框架,这正是我正在做的工作:

下面是代码:

<div class="table-responsive">
  <table class="table">
    <thead>
      <tr>
        <th scope="col">#</th>
        <th scope="col">Heading</th>
        <th scope="col">Heading</th>
        <th scope="col">Heading</th>
        <th scope="col">Heading</th>
        <th scope="col">Heading</th>
        <th scope="col">Heading</th>
        <th scope="col">Heading</th>
        <th scope="col">Heading</th>
        <th scope="col">Heading</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">1</th>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
      </tr>
      <tr>
        <th scope="row">2</th>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
      </tr>
      <tr>
        <th scope="row">3</th>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
      </tr>
    </tbody>
  </table>
</div>
Run Code Online (Sandbox Code Playgroud)

HDP*_*HDP 8

您可以使用以下背景 css 来制作水平滚动阴影效果。

.table-responsive{
  background-image: linear-gradient(to right, white, white), linear-gradient(to right, white, white), linear-gradient(to right, rgba(0, 0, 20, .50), rgba(255, 255, 255, 0)), linear-gradient(to left, rgba(0, 0, 20, .50), rgba(255, 255, 255, 0));
  /* Shadows */
  /* Shadow covers */
  background-position: left center, right center, left center, right center;
  background-repeat: no-repeat;
  background-color: white;
  background-size: 20px 100%, 20px 100%, 10px 100%, 10px 100%;
  background-attachment: local, local, scroll, scroll;
  }
Run Code Online (Sandbox Code Playgroud)

演示:https : //jsfiddle.net/3oxq1ju6/1/

  • 此解决方案不适用于嵌套 `&lt;tr&gt;` 元素具有背景的情况,例如 .`tr {background: red;}`。它不起作用,因为滚动阴影效果附加在行后面,并且行的背景颜色覆盖了它。如何解决这个问题? (4认同)
  • @HDP 在装有最新版 Chrome 的 Mac 笔记本电脑上的 Chrome 上效果不佳。当我使用外接显示器时它可以工作,但使用内置的笔记本电脑显示器时它不起作用...... (3认同)
  • 这在当前版本的 Chrome、Safari 和 Firefox 上运行良好。但是,直到您在 Edge 上滚动后,它才会显示阴影。 (2认同)