当前位置:网站首页>Using directive in angualr2 to realize that the picture size changes with the window size
Using directive in angualr2 to realize that the picture size changes with the window size
2022-07-05 10:09:00 【cppphp】
Front end programming , It is often required that pictures maintain aspect ratio , The picture zooms automatically as the window zooms , stay angular2 We can use instructions Directive To achieve this function .
First, in the existing angular2 Enter the directory to use this function in the project , Then input... At the terminal :
ng generate directive item-resize
Two files will be generated in the current directory item-resize.directive.ts and item-resize.directive.spec.ts,item-resize.directive.spec.ts It is a unit test file, which we will not discuss in this article .
Open it with an editor item-resize.directive.ts, I use it VS Code, The others are OK , Enter the following code :
import { Directive, ElementRef, HostListener, Renderer, Input, HostBinding } from '@angular/core';
@Directive({
selector: '[appItemResize]'
})
export class ItemResizeDirective {
private el: HTMLElement;
constructor(el: ElementRef, public renderer: Renderer) {
this.el = el.nativeElement;
}
@HostBinding('style.height.px')
elHeight: number;
@HostListener('window:resize', ['$event.target'])
onResize() {
this.resizeWorks();
}
// tslint:disable-next-line:use-life-cycle-interface
ngAfterViewInit() {
this.resizeWorks();
}
private resizeWorks(): void {
const elImage = this.el.getElementsByTagName('img');
const img = new Image();
const that = this;
img.src = elImage[0].src;
img.onload = function(e) {
that.elHeight = +that.el.clientWidth * img.height / img.width;
};
}
}
Save after confirmation . Then open... In your current directory html file , My is introduction.component.html, Enter the following in the relevant location :
…
<div id='technology' appItemResize>
<img src="../../../../assets/images/N4Si3Patten.jpg" width="98%" style="z-index:-100;position:absolute;left:10;top:200">
</div>
…
among appItemResize Are we in item-resize.directive Defined in instruction . For different pictures, you can use instructions in multiple places in the document .
src It's a picture file , The content can be adjusted according to their respective directory structure and image files .
style The image style in should also be adjusted according to their own needs .
Of course, the instructions we define should be added to the corresponding module file , Such as goods.module.ts, The sample code is as follows :
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ItemResizeDirective } from './product/introduction/item-resize.directive';
@NgModule({
declarations: [
…
ItemResizeDirective,
],
imports: [
CommonModule
],
providers: [
],
exports: [
CommonModule,
]
})
export class GoodsModule { }
边栏推荐
- 程序员搞开源,读什么书最合适?
- Officially launched! Tdengine plug-in enters the official website of grafana
- Android SQLite database encryption
- 苹果 5G 芯片研发失败?想要摆脱高通为时过早
- 高级 OpenCV:BGR 像素强度图
- Jupiter notebook shortcut key
- To bring Euler's innovation to the world, SUSE should be the guide
- Kotlin Compose 多个条目滚动
- ArcGIS Pro 创建要素
- MySQL character type learning notes
猜你喜欢
How Windows bat script automatically executes sqlcipher command
Oracle combines multiple rows of data into one row of data
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
程序员如何活成自己喜欢的模样?
TDengine × Intel edge insight software package accelerates the digital transformation of traditional industries
正式上架!TDengine 插件入驻 Grafana 官网
The king of pirated Dall · e? 50000 images per day, crowded hugging face server, and openai ordered to change its name
[technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode
双容水箱液位模糊PID控制系统设计与仿真(Matlab/Simulink)
一种用于干式脑电图的高密度256通道电极帽
随机推荐
卷起來,突破35歲焦慮,動畫演示CPU記錄函數調用過程
Pagoda panel MySQL cannot be started
How Windows bat script automatically executes sqlcipher command
RMS to EAP is simply implemented through mqtt
Kotlin Compose 与原生 嵌套使用
剪掉ImageNet 20%数据量,模型性能不下降!Meta斯坦福等提出新方法,用知识蒸馏给数据集瘦身...
卷起来,突破35岁焦虑,动画演示CPU记录函数调用过程
TDengine 已经支持工业英特尔 边缘洞见软件包
TDengine 离线升级流程
基于单片机步进电机控制器设计(正转反转指示灯挡位)
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下
写入速度提升数十倍,TDengine 在拓斯达智能工厂解决方案上的应用
搞数据库是不是越老越吃香?
The popularity of B2B2C continues to rise. What are the benefits of enterprises doing multi-user mall system?
Tongweb set gzip
小程序启动性能优化实践
Coffeescript Chinese character to pinyin code
90%的人都不懂的泛型,泛型的缺陷和应用场景
善用兵者,藏于无形,90 分钟深度讲解最佳推广价值作品
How do enterprises choose the appropriate three-level distribution system?