【问题标题】:Position Sticky While Offsetting Inline Elements在偏移内联元素时保持粘性
【发布时间】:2020-10-09 07:27:57
【问题描述】:

我正在尝试做的是增量偏移内联元素,同时在它们上使用粘性位置,以便在用户滚动元素时锁定到位,形成一个连贯的结构。例如,我有一个父元素div,其中包含一个p 子元素,而子元素又包含多个span 子元素。

<div>
  <p>
    <span>w</span>
    <span>o</span>
    <span>r</span>
    <span>d</span>
  </p>
</div>

我所做的是使用translateY 递增地偏移每个字母(因为sticky 显然使用top 等来确定症结点),所以第一个字母被偏移了,比如说,100% ,200% 的第二个字母,以此类推。这个想法是每个字母将在用户滚动时锁定到位,最终在用户完成滚动时形成一个单词。

我知道问题在于,一旦启用sticky,span 元素就会定位到父级relative,这意味着元素总是相对于父级偏移。我只是想不出一个纯 CSS 解决方案。

我在写这篇文章时确实有一个想法:也许诀窍是使用top 来抵消,而bottom 用于坚持?

Here是下面嵌入代码的codepen。

html,
body { width: 100%; height: 100%; margin: 0; }

div  { border: solid red 2px; box-sizing: border-box; }

p    { font-family: arial; font-size: 104px; border: solid blue 2px; box-sizing: border-box;
  overflow-y: scroll;
  margin: 0;
}

span { border: solid black 2px; box-sizing: border-box;
  /* position: sticky; */
  top: 0;
  display: inline-block;
}

span:nth-child(1) { transform: translateY(100%); }
span:nth-child(2) { transform: translateY(200%); }
span:nth-child(3) { transform: translateY(300%); }
span:nth-child(4) { transform: translateY(400%); }
<div>
  <p>
    <span>w</span>
    <span>o</span>
    <span>r</span>
    <span>d</span>
  </p>
</div>

【问题讨论】:

    标签: html css sticky


    【解决方案1】:

    您可以在每个字母上使用负百分比来抵消变换:

    html,
    body {
      width: 100%;
      height: 100%;
      margin: 0;
    }
    
    div {
      border: solid red 2px;
      box-sizing: border-box;
    }
    
    p {
      font-family: arial;
      font-size: 104px;
      border: solid blue 2px;
      box-sizing: border-box;
      overflow-y: scroll;
      margin: 0;
    }
    
    span {
      border: solid black 2px;
      box-sizing: border-box;
      position: sticky;
      display: inline-block;
    }
    
    span:nth-child(1) {
      transform: translateY(100%);
      top: -100%;
    }
    
    span:nth-child(2) {
      transform: translateY(200%);
      top: -200%;
    }
    
    span:nth-child(3) {
      transform: translateY(300%);
      top: -300%;
    }
    
    span:nth-child(4) {
      transform: translateY(400%);
      top: -400%;
    }
    <div>
      <p>
        <span>w</span>
        <span>o</span>
        <span>r</span>
        <span>d</span>
      </p>
    </div>

    【讨论】:

    • 自从我上次编码以来已经有一段时间了,所以我的编码大脑并没有全力以赴,但这仍然是雄辩的。
    【解决方案2】:

    这是另一个更适合等宽字体和通用的解决方案的想法(您不需要使用nth-child设置样式)

    div {
      border: solid red 2px;
      box-sizing: border-box;
    }
    
    p {
      font-family: monospace;
      font-size:90px;
      border: solid blue 2px;
      box-sizing: border-box;
      overflow-y: scroll;
      margin: 0;
      line-height: 1.2em;
      height: 1.2em; /* equal to line-height */
    }
    
    span {
      position: sticky;
      top: 0;
      display: block;
    }
    
    span:first-child {
      margin-top: 1.2em; /* equal to line-height */
    }
    
    span:not(:first-child)::before {
      content: "";
      float: left;
      width: 1ch;
      height: 1.3em; /* a bit bigger than the line-height */
    }
    <div>
      <p>
        <span>H</span>
        <span>e</span>
        <span>l</span>
        <span>l</span>
        <span>o</span>
        <span> </span>
        <span>w</span>
        <span>o</span>
        <span>r</span>
        <span>d</span>
      </p>
    </div>

    【讨论】:

    • 有人打败了你。还是很感激
    • 邪恶!!等宽字体是宝藏。希望他们适合我正在建立的品牌:(
    • 这是通过在每个字母之前插入一个伪元素来实现的,假设 margin-top 来自 span:first-child?偏移量是这样增加的吗?
    • @oldboy 它的工作原理是浮动的伪元素将字母推到每一行。这是一个更好的说明:stackoverflow.com/a/53611964/8620333
    猜你喜欢
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 2016-05-04
    • 2017-10-07
    • 2022-01-02
    • 2019-04-27
    • 2019-12-22
    相关资源
    最近更新 更多