当前位置:网站首页>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-resizeTwo 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 { }
边栏推荐
- Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft
- 如何獲取GC(垃圾回收器)的STW(暫停)時間?
- Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)
- 分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
- QT timer realizes dynamic display of pictures
- Oracle combines multiple rows of data into one row of data
- 宝塔面板MySQL无法启动
- oracle和mysql批量Merge对比
- From "chemist" to developer, from Oracle to tdengine, two important choices in my life
- 程序员搞开源,读什么书最合适?
猜你喜欢

Baidu app's continuous integration practice based on pipeline as code
![[200 opencv routines] 219 Add digital watermark (blind watermark)](/img/ac/f6d16e74538bd589a1f6faee08d59a.png)
[200 opencv routines] 219 Add digital watermark (blind watermark)

基于单片机步进电机控制器设计(正转反转指示灯挡位)

Roll up, break 35 - year - old Anxiety, animation Demonstration CPU recording Function call Process

How to correctly evaluate video image quality

Six simple cases of QT

The king of pirated Dall · e? 50000 images per day, crowded hugging face server, and openai ordered to change its name

宝塔面板MySQL无法启动

如何獲取GC(垃圾回收器)的STW(暫停)時間?

单片机原理与接口技术(ESP8266/ESP32)机器人类草稿
随机推荐
天龙八部TLBB系列 - 关于包裹掉落的物品
历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
Why don't you recommend using products like mongodb to replace time series databases?
TDengine ×英特尔边缘洞见软件包 加速传统行业的数字化转型
cent7安装Oracle数据库报错
天龙八部TLBB系列 - 单体技能群伤
Cross process communication Aidl
Data visualization platform based on template configuration
How to use sqlcipher tool to decrypt encrypted database under Windows system
Common fault analysis and Countermeasures of using MySQL in go language
mysql80服务不启动
Pagoda panel MySQL cannot be started
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上
百度智能小程序巡檢調度方案演進之路
RMS to EAP is simply implemented through mqtt
善用兵者,藏于无形,90 分钟深度讲解最佳推广价值作品
Tutorial on building a framework for middle office business system
Comment obtenir le temps STW du GC (collecteur d'ordures)?
The king of pirated Dall · e? 50000 images per day, crowded hugging face server, and openai ordered to change its name
Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft