简体   繁体   English

Amp-sidebar:它绝对必须是身体的孩子吗? 为什么?

[英]Amp-sidebar: Does it absolutely have to be the child of the body? Why?

Why does <amp-sidebar> HAVE to be the direct child of the body? 为什么<amp-sidebar>必须成为身体的直接孩子?

I am in a situation where, in terms of code structure, it would be much easier for me to include <amp-sidebar> within a <div> . 我处于这样一种情况:在代码结构方面,我更容易在<div>包含<amp-sidebar> <div> I want it to behave exactly like <amp-sidebar> , but I simply don't have any easy way to place an element as the direct child of a <body> . 我希望它的行为与<amp-sidebar>完全相同,但我没有任何简单的方法将元素放置为<body>的直接子元素。

I was especially confused when I saw that this amp-by-example page does not pass the AMP validator. 当我看到这个逐个示例的页面没有通过AMP验证器时,我特别困惑。

Could someone please illuminate me? 有人可以照亮我吗? I thought about opening an issue on github, but I'm not sure if there's a bug. 我想在github上打开一个问题,但我不确定是否有错误。

According to a lead dev of the AMP project, the requirement that <amp-sidebar> is child of <body> is due to a Safari bug with position:fixed . 根据AMP项目的主要开发者, <amp-sidebar><body>子项的要求是由于position:fixed的Safari错误position:fixed Link to tweet . 链接到推文

If you're working with server side rendering, one possible workaround is to parse the rendered html string and place the sidebar tag as a direct child of body. 如果您正在使用服务器端呈现,一种可能的解决方法是解析呈现的html字符串并将侧边栏标记放置为正文的直接子项。 I have not tried this at scale yet: 我还没有大规模尝试这个:

function fixAmpSidebars(strHtml) {
    let sidebarBegin = 0, sidebarEnd = 0
    const sidebars = []
    do {
        sidebarBegin = strHtml.indexOf('<amp-sidebar', sidebarBegin)
        if (sidebarBegin !== -1) {
            sidebarEnd = strHtml.indexOf('</amp-sidebar>', sidebarBegin) + 14
            sidebars.push(strHtml.slice(sidebarBegin, sidebarEnd))
            strHtml = strHtml.slice(0, sidebarBegin) + strHtml.slice(sidebarEnd)
        }
    } while (sidebarBegin !== -1)
    if (sidebars.length) {
        strHtml = strHtml + sidebars.join('')
    }

    return strHtml
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM