gpt4 book ai didi

html - 显示/隐藏多个 Div 取决于屏幕 Angular 2

转载 作者:太空狗 更新时间:2023-10-29 18:20:44 25 4
gpt4 key购买 nike

我想使用一些按钮来显示/隐藏使用 angular2 的多个 div

该页面最初将显示所有 div。当小屏幕时,我隐藏所有 div,然后单独的按钮显示特定的 div,同时隐藏其余部分。

如有任何帮助,我们将不胜感激。

<div class="buttons">
<a class="button" id="showdiv1">Div 1</a>
<a class="button" id="showdiv2">Div 2</a>
<a class="button" id="showdiv3">Div 3</a>
</div>


<div class="row">
<div class="col-xlg-4 col-xl-5 col-lg-12 col-sm-12 col-xs-12 hidden-sm-down">
</div>

<div class="col-xlg-4 col-xl-5 col-lg-12 col-sm-12 col-xs-12 hidden-sm-down">
</div>

<div class="col-xlg-4 col-xl-5 col-lg-12 col-sm-12 col-xs-12 hidden-sm-down">
</div>
</div>

最佳答案

使用 @HostListenerresize 事件来确定视口(viewport)的宽度。然后在您的组件中设置一个具有此值的属性,然后您可以将其提供给 HTML 模板中的 ngIf*

import { Component, HostListener, AfterViewInit } from '@angular/core';

@Component()

export class AppComponent {

windowWidth: number = window.innerWidth;

//initial values, The window object may still be undefined during this hook, let me know if that's the case and we'll figure out a better hook for the initial value
ngAfterViewInit() {
this.windowWidth = window.innerWidth;
}

//if screen size changes it'll update
@HostListener('window:resize', ['$event'])
resize(event) {
this.windowWidth = window.innerWidth;
}

}
<!-- Then any HTML element you wish to toggle apply the *ngIf directive as so: -->

<div *ngIf*="windowWidth > 800">content</div>

关于html - 显示/隐藏多个 Div 取决于屏幕 Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41745158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com