简体   繁体   English

AMP:切换CSS类

[英]AMP: Toggle CSS Class

I am generating an amp template and I have a button that toggles the visibility of an amp-sidebar . 我正在生成一个amp模板,并且有一个按钮可以切换amp-sidebar的可见性。 The code is as follows: 代码如下:

<button class="button .closed icon" on='tap:sidebar1.toggle'></button>

The side-bar toggles as expected. 侧栏按预期切换。 However, I also want to the icon displayed to be changed depending on the state (open or closed). 但是,我也希望根据状态(打开或关闭)更改显示的图标。

I have the two classes for doing so: 为此,我有两个课程:

  • button .closed (hamburger menu icon) .closed按钮(汉堡菜单图标)

  • button .open (cross) 按钮。打开(十字)

The shared class "icon" should remain the same between the two. 共享类“ icon”在两者之间应保持相同。 I also want to keep the 'tap:sidebar1.toggle' so the menu will still toggle. 我还想保留“ tap:sidebar1.toggle”,以便菜单仍然可以切换。

One way of achieving this would be by using an amp-state to keep track of whether the sidebar is open or closed. 实现此目的的一种方法是使用放大器状态来跟踪侧边栏是打开还是关闭。 Then you can bind the button's class using this state to style it accordingly. 然后,您可以使用此状态绑定按钮的类以相应地设置其样式。

Initialise an amp-state like follows : 初始化如下所示的放大器状态:

<amp-state id = "sbOpen">
    <script type = "application/json">
        false
    </script>
</amp-state>

and now change your sidebar code as follows : 然后按如下所示更改边栏代码:

<amp-sidebar

 on="sidebarOpen:AMP.setState({sbOpen});sidebarClose:AMP.setState({sbOpen})" 

and finally your button tag should be something like this : 最后,您的button标签应该是这样的:

<button class="button closed icon" on='tap:sidebar1.toggle'
        [class]=" 'button icon ' + (sbOpen?'open':'closed) "></button>

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

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