当前位置:网站首页>NG Textarea-auto-resize
NG Textarea-auto-resize
2022-07-03 05:29:00 【SeriousLose】
<textarea (ngModelChange)="onChange()" [(ngModel)]="val" (focus)="onChange()" (blur)="onChange()" #text class="autosize" maxlength={
{maxlength}} rows="1" placeholder={
{placeholder}}></textarea>
<!--text1为影子textarea 用于获取scrollHeight,并将值付给主textarea的height 实现auto resize -->
<textarea class="autosize hidden" rows="1" [value]="val" #text1 ></textarea>
:host {
display: flex;
justify-content: flex-start;
.autosize {
vertical-align: top;
padding: 7px 8px;
font-size: 12px;
width: 200px;
border-radius: 3px;
line-height: 14px;
position: relative;
outline: none;
resize: none;
box-sizing: border-box;
height: 32px;
transition: height 0.2s;
}
.hidden {
position: absolute;
left: -100px;
visibility: hidden;
overflow: hidden;
}
}
import { AfterViewInit, Component, ElementRef, EventEmitter, Input, OnInit, Output, Renderer2, ViewChild } from '@angular/core';
@Component({
selector: 'textarea-auto-resize',
templateUrl: './textarea-auto-resize.component.html',
styleUrls: ['./textarea-auto-resize.component.less'],
})
export class TextareaAutoResizeComponent implements OnInit, AfterViewInit {
@Input('max-height') maxHeight = 100; // 最大高度 默认 100
@Input('maxlength') maxlength = 500; // 最大字数 默认500
@Input('placeholder') placeholder: any; // placeholder 提示语
@Input('g') g: any;
@Input('el') el: any;
@Output('valChange') valChange = new EventEmitter();
@ViewChild('text') text: ElementRef;
@ViewChild('text1') text1: ElementRef;
@Input('value') val = '';
constructor(private rd2: Renderer2) {}
ngOnInit() {}
ngAfterViewInit() {
this.reset();
}
onChange() {
this.reset();
setTimeout(() => {
this.valChange.emit({
value: this.val,
g: this.g,
el: this.el,
});
this.reset();
}, 0);
}
/**
* @description: 改变textarea的高度
* @param {type}
* @return:
*/
reset() {
this.text1.nativeElement.style.width = this.text.nativeElement.scrollWidth + 2 + 'px';
if (this.text1.nativeElement.scrollHeight < this.maxHeight) {
this.text1.nativeElement.style.width = this.text.nativeElement.scrollWidth + 19 + 'px';
this.text.nativeElement.style.height = this.text1.nativeElement.scrollHeight + 2 + 'px';
} else {
this.text.nativeElement.style.height = this.maxHeight - 0 + 2 + 'px';
}
}
}
边栏推荐
- 【实战项目】自主web服务器
- Best practices for setting up altaro VM backups
- Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
- Altaro virtual machine replication failed: "unsupported file type vmgs"
- Introduction to deep learning (II) -- univariate linear regression
- Deploy crawl detection network using tensorrt (I)
- Yolov5 input (II) | CSDN creative punch in
- Differences among bio, NiO and AIO
- es7创建索引容易犯的错误
- Source insight automatic installation and licensing
猜你喜欢
Communication - how to be a good listener?
Export the altaro event log to a text file
es7创建索引容易犯的错误
About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller
今天很多 CTO 都是被干掉的,因为他没有成就业务
联想R7000显卡的拆卸与安装
乾元通多卡聚合路由器的技术解析
Deep embedding and alignment of Google | protein sequences
Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)
随机推荐
Communication - how to be a good listener?
Shallow and first code
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
Best practices for setting up altaro VM backups
Notepad++ wrap by specified character
Webapidom get page elements
Bluebridge cup real topic 2020 palindrome date simulation construction provincial competition
EMD distance - example of use
Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
Pessimistic lock and optimistic lock of multithreading
Differences among bio, NiO and AIO
Introduction to rust Foundation (basic type)
Redis 入門和數據類型講解
Explanation of several points needing attention in final (tested by the author)
ninja: build stopped: subcommand failed.
Intégration profonde et alignement des séquences de protéines Google
Obtenir et surveiller les journaux du serveur distant
Redis使用Lua脚本简介
ROS Compilation Principle
大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南