gpt4 book ai didi

html - 纯 css 滚动阴影

转载 作者:行者123 更新时间:2023-12-04 21:35:51 25 4
gpt4 key购买 nike

当内容超过可用宽度时,我需要启用滚动阴影。这是我试图用纯 css(没有 JS)来实现的。我遇到了很多文章,可以使用 css 多背景和背景附件来实现。如果内容是文本类型,则可以使用下面的 jsfilldle 代码正常工作,在按钮情况下,背景位于按钮后面。所以从视觉上看是行不通的。

预期行为:

enter image description here

场景 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对 child 比对 parent 。为此,您还需要另外两件事:

  • 一个 position除了 static给 child 和家长
  • .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 child 的需要低于 parent 的一个,但如果你使用负数 z-index ,就像我建议的那样,点击和悬停等事件停止工作。我适本地更改了代码。

    关于html - 纯 css 滚动阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38542163/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com