【问题标题】:Event that would "setState" when div becomes visible in amp-html当 div 在 amp-html 中可见时将“setState”的事件
【发布时间】:2018-01-23 00:21:29
【问题描述】:

首先,我了解如何使用 amp-bind 在 amp-html 中 setState,如下所示:

<amp-selector on="select:AMP.setState({ guess: event.targetOption })">
    <div class="button" option="a">Answer 1</div>
    <div class="button" option="b">Answer 2</div>
    <div class="button" option="c">Answer 3</div>
</amp-selector>

现在假设我选择按钮Answer 2,那么printState() 上的状态将看起来像{"guess":"b"},并且该div 将不再被隐藏:

<div hidden [hidden]="!guess">
    <h4>You have the correct answer!</h4>
</div>

我的问题是:一旦隐藏的 div 变得可见,我如何设置另一个键/值来声明状态?换句话说,我怎样才能让它工作?

<div hidden [hidden]="!guess" on="WhenThisIsVisible:AMP.setState({"text":"hi!"})"></div>

我可以使用tap: 代替我编造的事件,但我希望用户不必点击setState 的div。

所以一旦 div 可见,printState() 将返回 {"guess":"b","text":"hi!"}

【问题讨论】:

    标签: html amp-html


    【解决方案1】:

    您是否可以在 div 中使用 span,然后使用类似:

    <span [text]="text"></span>
    

    这是完整的示例:

    <!doctype html>
    <html ⚡>
    <head>
      <meta charset="utf-8">
      <title>My AMP Page</title>
      <link rel="canonical" href="self.html" />
      <meta name="viewport" content="width=device-width,minimum-scale=1">
      <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>
      <script async src="https://cdn.ampproject.org/v0.js"></script>
      <script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
      <script async custom-element="amp-selector" src="https://cdn.ampproject.org/v0/amp-selector-0.1.js"></script>
      <style amp-custom>
        h1 {
          margin: 1rem;
        }
      </style>
    </head>
    <body>
      <amp-selector on="select:AMP.setState({ guess: event.targetOption, text: 'hi' })">
        <div class="button" option="a">Answer 1</div>
        <div class="button" option="b">Answer 2</div>
        <div class="button" option="c">Answer 3</div>
    </amp-selector>
      <div hidden [hidden]="!guess">
        <span [text]="text"></span>
        <h4>You have the correct answer!</h4>
    </div>
    </body>
    </html>
    

    【讨论】:

    • 抱歉,我不确定这与设置放大器状态有何关系
    猜你喜欢
    • 2017-01-03
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    相关资源
    最近更新 更多