简体   繁体   English

如何直接使用javascript在AMP页面中打开侧边栏? 有功能吗?

[英]How do I open the sidebar in a AMP page with javascript directly? Is there a function?

I do not really comprehend the way to debug this the right way so I cannot solve the problem. 我不太了解正确的调试方法,因此无法解决问题。 I have an AMP page, take this as example: 我有一个AMP页面,以此为例:

    <!doctype html>
    <html AMP lang="en">
    <head>
        <meta charset="utf-8">
        <title>title</title>
        <link rel="canonical" href="//link_to_self" />
        <meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,initial-scale=1">
        <script async  src="https://cdn.ampproject.org/v0.js"></script>
        <script async custom-element=amp-sidebar src="https://cdn.ampproject.org/v0/amp-sidebar-0.1.js"></script>
        <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
        <style amp-custom></style>
    </head>
    <body>
        <amp-sidebar id="sidebar" layout="nodisplay">Sidebar</amp-sidebar>
        <button on="tap:sidebar.open">Open sidebar</button>
    </body>
    </html>

What I want to do is a pure javascript call that does the same as the button does. 我想做的是一个纯JavaScript调用,其功能与按钮相同。 This doesnt work: 这不起作用:

    <script>
    document.getElementById("sidebar").open();
    </script>

The reason I need this is that I have a swipe library that when swipe is detected needs to call the function to open the menu. 我需要这个的原因是我有一个滑动库,当检测到滑动时需要调用该函数以打开菜单。 Anyonw know how I would call the sidebar function? Anyonw知道如何调用侧边栏功能吗?

The documentation has three states that are possible from HTML, how do I call these from javascript? 该文档具有HTML可能提供的三种状态,如何从javascript调用这些状态? (and if possible, how do I find the name of function with the chrome debugger or is it possible, I have now clue but I would think the answer is infront of me as I can read the javascript code. (并且,如果可能的话,如何通过chrome调试器找到函数的名称,或者是否有可能,我现在有所提示,但是我认为答案就在我眼前,因为我可以阅读javascript代码。

    <button on="tap:sidebar.toggle">Toggle sidebar</button>
    <button on="tap:sidebar.open">Open sidebar</button>
    <button on="tap:sidebar.close">Close sidebar</button>

From what I know, you can't unfortunately: 据我所知,不幸的是你不能:

One thing we realized early on is that many performance issues are caused by the integration of multiple JavaScript libraries, tools, embeds, etc. into a page. 我们早就意识到的一件事是,许多性能问题是由将多个JavaScript库,工具,嵌入等集成到页面中引起的。 This isn't saying that JavaScript immediately leads to bad performance, but once arbitrary JavaScript is in play, most bets are off because anything could happen at any time and it is hard to make any type of performance guarantee. 这并不是说JavaScript会立即导致性能下降,但是一旦使用了任意JavaScript,大多数赌注就会消失,因为任何时间都可能发生任何事情,并且很难保证任何类型的性能。 With this in mind we made the tough decision that AMP HTML documents would not include any author-written JavaScript, nor any third-party scripts. 考虑到这一点,我们做出了艰难的决定,即AMP HTML文档将不包含任何作者编写的JavaScript或任何第三方脚本。

From: AMP Project | 来自: AMP项目| How it works 这个怎么运作

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

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