css - 当高度超过最大高度时使 ionic 文本区域可滚动

标签 css ionic-framework ionic2 textarea ionic3

我正在尝试在我的应用中实现聊天功能。

我写了一个自动调整大小的 ion-textarea,如下所示: 查看

<ion-footer [keyboardAttach]="content">
  <ion-toolbar class="text-input">
    <ion-item no-lines>
      <ion-textarea autosize
        rows="1"
        placeholder="Votre message..."
        autocomplete="on"
        autocorrect="on"
        [(ngModel)]="message"
        >
      </ion-textarea>
      <button ion-button small icon-only round item-end color="secondary" (tap)="onSend()">
        <ion-icon name="send"></ion-icon>
      </button>
    </ion-item>
  </ion-toolbar>
</ion-footer>

CSS

ion-footer {
    ion-toolbar.toolbar.toolbar-ios {
      &.toolbar:last-child {
        padding-bottom: 0px;
        height: auto;
      }
    }
    ion-item.item-textarea {
      background-color: transparent;
    }
    .text-input {
      height: auto;
      div.input-wrapper {
        background-color: #fff;
        border-radius: 20px;
        textarea.text-input {
          margin-left: 8px;
          max-height: 100px;
        }
      }
    }
  }

指令

import {ElementRef, HostListener, Directive, OnInit, ViewChild} from '@angular/core';
import {Content} from 'ionic-angular';

@Directive({
  selector: 'ion-textarea[autosize]'
})

export class AutosizeDirective implements OnInit {
  @HostListener('input', ['$event.target'])
  onInput(textArea:HTMLTextAreaElement):void {
    this.adjust();
  }

  constructor(public element:ElementRef) {
  }

  ngOnInit():void {
    setTimeout(() => this.adjust(), 0);
  }

  adjust():void {
    let textArea = this.element.nativeElement.getElementsByTagName('textarea')[0];
    textArea.style.overflow = 'hidden';
    textArea.style.height = 'auto';
    textArea.style.height = textArea.scrollHeight + "px";
  }
}

一切正常,但滚动。

当我超过 100px 最大高度时,我无法在 ion-textarea 中滚动...所以如果我想更正文本的隐藏部分,这是不可能的。此外,当我尝试滚动时,它实际上会滚动聊天( ionic 内容)...

知道如何解决这个问题吗?

最佳答案

我是这样解决的: .SCSS

.text-input {
      height: auto;
      div.input-wrapper {
        background-color: #fff;
        border-radius: 20px;
        textarea.text-input {
          margin-left: 8px;
          //overflow-y: auto;
          //max-height: 100px;
        }
      }
    }

并使用包含调整函数的自定义 Autosize 指令:

adjust():void {
    let textArea = this.element.nativeElement.getElementsByTagName('textarea')[0];
    textArea.style.height = 'auto';
    if (textArea.scrollHeight < 100) {
      textArea.style.height = textArea.scrollHeight + "px";
      textArea.style.overflowY = 'hidden';
    } else {
      textArea.style.height = "100px";
      textArea.style.overflowY = 'auto';
    }

  }

希望这对其他人有帮助。

关于css - 当高度超过最大高度时使 ionic 文本区域可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46953893/

相关文章:

twitter-bootstrap - 将一个 div 元素置于另一个 div 元素的中心

html - CSS - 可以用背景图像覆盖图像吗?

javascript - 我想使用 for 循环从现有的 div 和 css 中增加多个 div

visual-studio - Cordova Visual Studio 构建错误

cordova - ionic 2 : Add URL Schemes automatically to the Whitelist for ios from config. xml

angular - 如何将项目数据传递给 angular 2 的单击事件?

html - 设置多个备用表格行的样式

ionic-framework - Ionicons:如何添加自定义图标?

angular - 如何在不丢失准备好的 css 的情况下在 Ionic 2/3 中编写自定义组件?

java - 使用 Java 和 Angular2 进行 Spring Security 登录