【问题标题】:Centre Lottie SVG to a Header element将 Lottie SVG 居中到 Header 元素
【发布时间】:2019-08-18 08:37:33
【问题描述】:

我有一个来自 Lottiefiles 的动画(JSON 格式),然后由 Lottie 框​​架将其转换为动画 SVG 文档。但是,我似乎无法使用标题标签定位 SVG 文档。我希望它在文本旁边。

我试图在现有线程中搜索类似的东西,但这些都不起作用,除了一个(某种)。这包括将 SVG 添加到标题本身内部的 div 中。但是,当我尝试此操作时,SVG 文档已固定到位,因此虽然它适用于较短的文本(少于 6 个字符),但如果文本较长,SVG 将出现在下方,而不是移动到文本的末尾.

我还必须在超时时通过 Javascript 手动将样式分配给 SVG 文件,因为 SVG 文档最初不存在。

这是实际的标头代码(在 PugJS 中)。

 h1(class="channel-header" style="margin-bottom: 36px; width: 96px; margin: auto;") #{channel}
            if premium
                div(id="bodyanim" class="badge baseline")

这是标题和内部 div 标签的 SASS:

.badge
  display: inline-flex
  align-self: center
  height: 70%

.badge svg, .badge img
  height: 1em
  width: 1em
  fill: currentColor
  z-index: -1
  position: absolute
  left: 0
  top: 0


.badge.baseline svg, .badge img
  top: .125em
  position: relative

.channel-header
  margin: 0 0 16px 0
  padding: 0
  line-height: 1
  font-weight: normal
  position: relative
  height: 45px

这是设置 SVG 对象的 JS,并在超时后设置其 CSS。

var animData = {
    wrapper: document.getElementById('bodyanim'),
    animType: 'svg',
    loop: true,
    prerender: true,
    autoplay: true,
    path: '/anims/4768-trophy.json'
};

var anim = bodymovin.loadAnimation(animData);

setTimeout(function () {
    var svg = animData.wrapper.getElementsByTagName("svg")[0];
    svg.style.position = "absolute";
    svg.style.left = "0";
    svg.style.top = "0";
    svg.style.zIndex = -1;
    svg.style.marginLeft = "65%";

}, 100);

当我使用此代码运行站点时,标题适用于任何短于 7 个字符的文本,但如果有更多,标题会尝试将自身“推”到 SVG 文档上方,而 SVG 会保持在它后面的位置与文本一起移动。 您可以在此站点上看到一个示例(您可以编辑端点,即 /channel/anythinghere 或编辑标签客户端):

http://themadgamers.co.uk:3000/channel/ItsMike

【问题讨论】:

  • 包含一个使用生成的源代码的工作示例以清楚地概述问题。
  • @AndyHoffman 抱歉,我编辑了问题以包含指向工作和不工作的屏幕截图的链接。
  • 问题是我们没有简单的方法来测试它。这只是屏幕上的一堆代码。一个工作示例的链接会很棒,这样我就可以测试可能的解决方案。试试CodeSandbox。
  • 更好的是,编辑您的问题以将编译后的 html/css 包含为 MCVE。 (另外,我认为h1 元素中不允许使用div 元素——您可能想使用header 而不是h1)
  • @AndyHoffman 抱歉,这更有意义。我无法让 CodeSandbox 处理所需的 JS 文件,但我已将它扔到我的服务器上,您可以在其中实时查看。

标签: javascript html css pug lottie


【解决方案1】:

为什么要为h1 设置固定宽度?

 h1(class="channel-header" style="margin-bottom: 36px; width: 96px; margin: auto;")

如果您删除 96px 宽度限制,较长的字符串将不再将奖杯推到用户名下方。

至于手册需要通过JavaScript设置SVG的样式...

setTimeout(function () {
  var svg = animData.wrapper.getElementsByTagName("svg")[0];
  svg.style.position = "absolute";
  svg.style.left = "0";
  svg.style.top = "0";
  svg.style.zIndex = -1;
  svg.style.marginLeft = "65%";
}, 100);

考虑将新的class 添加到您的CSS。

.mySvg {
  position: absolute;
  left: 0;
  top: 0;
  z-index: -1;
  margin-left: 65%;
}

那么您应该可以将JavaScript 简化为:

setTimeout(function () {
  var svg = animData.wrapper.getElementsByTagName("svg")[0];
  svg.className = "mySvg";
}, 100);

【讨论】:

  • 感谢您的帮助!我删除了标题上的宽度,但奖杯消失了,大概在屏幕外的某个地方。如果我删除或更改margin-left,它会重新出现,但仍会在文本后面。您更改了 position 标签有什么问题吗?
  • 编辑:我发现 width 被 Lottie 框​​架自动设置为 100%,这将标题推到一边。现在唯一的问题是设置 className 的 JavaScript 似乎不起作用,我假设这又是由于 Lottie 覆盖它,因为没有引发错误。看来只要我覆盖width 就可以了
猜你喜欢
  • 1970-01-01
  • 2017-05-10
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
  • 2022-10-30
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
相关资源
最近更新 更多