angular8 滚动多个图片加文字组件

这篇博客介绍了如何在Angular8中创建一个滚动展示多个图片和文字的组件。包括子组件的HTML、CSS和TS代码,以及父组件的HTML结构。完整代码可从GitHub仓库https://github.com/yyk316507/angualr8-demo获取。

子组件html

<div class="container" 
[ngStyle]="{
    'grid-template-rows': templateRows,
    'grid-template-columns': templateColumns}"
    (scroll) ="handleScroll($event)"
    >
    <ng-content select="[appGridItem]"></ng-content>
</div>
<div class="underline" *ngIf="scrollable">
    <div class="highlight" [ngStyle]="{'margin': sliderMargin}"></div>
</div>

子组件css

.container{
    background-color: #fff;
    padding: 5px 0;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    display: grid;
    place-content: stretch;
    place-items: center;
    grid-gap: 0.4rem;
    grid-auto-flow: row;
}
.underline {
    width: 20%;
    height: 2px;
    background-color: #f5f5f5;
    margin: 0 auto;
  }
.underline > .highlight {
    width: 50%;
    height: 2px;
    background-color: #e02f29;
    padding: 0;
}
.container::-webkit-scrollbar {
    display: none !important;
}

子组件ts

import { Component, OnInit,Output,Input} from '@angular/core';
export interface Channel {
  id: number;
  icon: string ;
  title: string;
  link: string;
}
@Component({
  selector: 'app-horizontal-grid',
  templateUrl: './horizontal-grid.component.html',
  styleUrls: ['./horizontal-grid.component.css']
})
export class HorizontalGridComponent implements OnInit {
  constructor() { }
  @Output() cols =6;
  @Input() displayCols =5;
  sliderMargin = '0'
  ngOnInit() {
  }
  
  public get scrollable() : boolean {
    return this.cols > this.displayCols
  }
  
  public get templateRows() : string {
    return `minmax(auto,max-content)`
  }
 
 public get templateColumns() : string {
   return `repeat(${this.cols},calc((100vw - ${this.displayCols*0.4}rem)/${this.displayCols}))`
 }
 handleScroll(ev){
  this.sliderMargin = `0 ${100*ev.target.scrollLeft /ev.target.scrollWidth}%`
 }
}

父组件html

<app-horizontal-grid >
    <div appGridItem *ngFor="let item of channels">
        <img [src]="item.icon" alt="" appGridItemImage="2rem" fitMode="none" (click)="handleClick(item.id)" >
        <span appGridItemTitle="0.6rem">{{ item.title }}</span>
    </div>
</app-horizontal-grid> 

可以到git下载https://github.com/yyk316507/angualr8-demo

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值