【问题标题】:How to mimic Amazon's Word Wise anotation feature如何模仿亚马逊的 Word Wise 注释功能
【发布时间】:2019-06-27 12:13:18
【问题描述】:

启用后,eInk Kindle 和一些 Kindle 应用会在不常用的单词上方显示定义。例如:

我试图用 标签来模拟这个功能,但是当定义比定义的单词长时它就不起作用了。此外,如果您仔细查看屏幕截图,您会注意到每个与其长度匹配的单词上方还有一个竖括号(?)。

仅使用 HTML5 和 CSS 来模拟这种布局的最佳方法是什么?

【问题讨论】:

    标签: css annotations definition epub kindle


    【解决方案1】:

    所以这里是我将如何做到这一点的基本版本,虽然我可以看到在并列词的解释重叠的特定场景中的一些问题。

    首先,我将每个单词包装在 span 元素中,并添加了带有单词解释的 data-tooltip 属性。我使用伪元素进行样式设置,以便解释绝对位于每个单词的顶部,并在要解释的单词上添加一个带有另一个伪元素的边框顶部以伪造箭头提示。

    CSS:

    body {
      font-size:22px;
    }
    [data-tooltip] {
      position:relative;
      display:inline-block;
      margin-top:20px;
      border-top:1px solid red;
    }
    [data-tooltip]:before {
      content:attr(data-tooltip);
      font-size:12px;
      position:absolute;
      display:inline-block;
      white-space:nowrap;
      /* left:0; */
      margin-top:-20px;
    }
    [data-tooltip]:after {
      content:"";
      display:block;
      width:6px;
      height:6px;
      border-top:1px solid red;
      border-right:1px solid red;
      transform:rotate(-45deg);
      position:absolute;
      left:50%;
      margin-left:-3px;
      top:-4px;
      background:#fff;
    }
    [data-tooltip].left:before {
      left: 0 !important;
    }
    [data-tooltip].right:before {
      right: 0 !important;  
    }
    [data-tooltip].center:before {
      left: 50% !important;
      transform:translateX(-50%);
    }
    

    此外,使用Javascript,我获取了每个paragraph 的长度(假设都具有相同的width)并为以下3 个场景创建了一个if 语句: - 待解释的词在段落长度的前1/3之前 - 待解释词在段落长度的2/3之后 - 待解释的词在上述两个陈述之间 ...并通过相应地应用 leftrightcenter 类来相应地调整解释。

    Javascript

    $(window).on('load resize', function() {
        pw = $('p').first().width();
      $('[data-tooltip]').each(function() {
        pos = $(this).offset().left;
        if (pos < pw/3) {
        console.log('run');
            $(this).removeClass().addClass('left');
        } else if (pos > pw*2/3) {
            $(this).removeClass().addClass('right');
        } else if ((pos > pw/3) && (pos < pw*2/3)) {
            $(this).removeClass().addClass('center');
        }
      })
    })
    

    这是一个有效的小提琴: https://jsfiddle.net/fgnp07ty/1/

    如果并排的两个单词的解释太长且重叠,您将不得不将解释宽度限制为单词长度并在末尾添加省略号 (...) - 我会去使用此解决方案,或计算解释长度并进行相应调整 - 这太多了。

    【讨论】:

      【解决方案2】:

      也许这对你有用:

      p{
      line-height:2em;
      }
      span{
      position:relative;
      }
      span:after{
      content: "";
          width: 0.3rem;
          height: 0.3rem;
          display: block;
          position: absolute;
          border-bottom: solid 1px coral;
          border-left: solid 1px coral;
          background: white;
          top: -0.1rem;
          left: 10%;
          transform: rotate(-45deg);
      }
      span:before{
      content:"the definition of penatibus";
      font-size:.7em;
      line-height:1rem;
      color:coral;
      position:absolute;
      top:-1rem;
      white-space:nowrap;
      border-bottom:solid 1px coral;
      width:100%;
      }
      &lt;p&gt;Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque &lt;span&gt;penatibus&lt;/span&gt; et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.&lt;/p&gt;

      【讨论】:

      • 感谢您的贡献。它有效,但我更喜欢 scooterlord 的解决方案。
      猜你喜欢
      • 2011-01-20
      • 2017-11-21
      • 2019-02-04
      • 1970-01-01
      • 2016-05-08
      • 2016-11-26
      • 2012-12-08
      • 1970-01-01
      • 2022-12-03
      相关资源
      最近更新 更多