Shr*_*wal 13 css responsive-design angular-material2 angular
我正在研究Angular 2中md-grid-list的基本示例.
HTML代码:
<md-grid-list cols="4" rowHeight="100px">
<md-grid-tile *ngFor="let tile of tiles"
[colspan]="tile.cols"
[rowspan]="tile.rows"
[style.background]="tile.color">
{{tile.text}}
</md-grid-tile>
</md-grid-list>
Run Code Online (Sandbox Code Playgroud)
TS代码:
export class GridListDynamicExample {
tiles = [
{text: 'One', cols: 3, rows: 1, color: 'lightblue'},
{text: 'Two', cols: 1, rows: 2, color: 'lightgreen'},
{text: 'Three', cols: 1, rows: 1, color: 'lightpink'},
{text: 'Four', cols: 2, rows: 1, color: '#DDBDF1'},
];
}
Run Code Online (Sandbox Code Playgroud)
上面的代码导致了这个:
如何使用某些HTML指令或CSS将布局设置为"列""列",以便在较小屏幕尺寸下的行(一和四)下方?
Angular 1中的Angular Material可以选择通过指定"md-cols-xs ="1"md-cols-sm ="2"md-cols-md ="4"md-cols-gt-md ="6" ".
小智 11
这是最简单的方法,你可以实现:)
<md-card class="sub-category-grid" style="padding:0px;" (window:resize)="onResize($event)">
<md-grid-list cols="{{test}}" rowHeight="1:1">
<md-grid-tile *ngFor="let item of Items">
{{item.title}}
</md-grid-tile>
</md-grid-list>
</md-card>
Run Code Online (Sandbox Code Playgroud)
// init this var with the default number of columns
test: any = 2;
Items: any = [
{title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
{title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
{title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
{title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
{title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
{title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
{title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
{title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
{title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" }
]
constructor() { }
ngOnInit() {
}
onResize(event) {
const element = event.target.innerWidth;
console.log(element);
if (element < 950) {
this.test = 2;
}
if (element > 950) {
this.test = 3;
}
if (element < 750) {
this.test = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将指令添加到组件中,然后像这样在指令中进行工作;
import { Directive, ElementRef, Input, HostListener, Output } from '@angular/core';
import * as _ from "lodash";
@Directive({ selector: '[myResponsive]' })
export class TestDirective {
@Input() tiles;
@HostListener('window:resize', ['$event'])
onResize(event) {
if (event.target.innerWidth < 980) {
_.each(this.tiles, tile => {
tile.cols = 2;
tile.rows = 1;
});
} else {
this.tiles = [
{text: 'One', cols: 3, rows: 1, color: 'lightblue'},
{text: 'Two', cols: 1, rows: 2, color: 'lightgreen'},
{text: 'Three', cols: 1, rows: 1, color: 'lightpink'},
{text: 'Four', cols: 2, rows: 1, color: '#DDBDF1'}
];
}
}
constructor(el: ElementRef) {
}
}
Run Code Online (Sandbox Code Playgroud)
您还需要将指令添加到 app.module.ts
import { TestDirective } from "./test.directive";
@NgModule({
imports: [
...
],
declarations: [
TestDirective
]
Run Code Online (Sandbox Code Playgroud)
你的 HTML 应该是这样的
<md-grid-list cols="4" rowHeight="100px">
<md-grid-tile myResponsive [(tiles)]="tiles" *ngFor="let tile of tiles"
[colspan]="tile.cols"
[rowspan]="tile.rows"
[style.background]="tile.color">
{{tile.text}}
</md-grid-tile>
</md-grid-list>
Run Code Online (Sandbox Code Playgroud)