• 解决聊天窗口的输入框高度变化,引起中间滚动内容的跳动问题


    场景:iphone  类似聊天窗口、有中间可滚动内容divB,输入框高度可以变化

    问题:当输入框输入文字高度变化后,如输入了两三行的文字,则中间可滚动内容divB会跳动回到之前一行时的位置。

    解决代码如下:通过async  和 await 解决

    1. <script>
    2. export default {
    3. data () {
    4. return {
    5. iosPlaceholderHeight: '100px',
    6. inputText: '',
    7. };
    8. },
    9. computed: {
    10. },
    11. watch: {
    12. inputText: {
    13. handler(v) {
    14. if (v.length > 0) {
    15. this.chatBoxScrollToBottom(false);
    16. }
    17. },
    18. immediate: true
    19. }
    20. },
    21. activated() {
    22. },
    23. methods: {
    24. fieldFocus() {
    25. this.iosPlaceholderHeight = '400px';
    26. setTimeout(() => {
    27. this.chatBoxScrollToBottom(false);
    28. }, 100);
    29. },
    30. fieldBlur() {
    31. this.iosPlaceholderHeight = '100px';
    32. },
    33. // 设置滚动条到最底部
    34. async chatBoxScrollToBottom(smooth = true) {
    35. console.log('chatBoxScrollToBottom scrollHeight:', this.$refs.container?.scrollHeight);
    36. if (!smooth) {
    37. this.$nextTick(async () => {
    38. const chatBox = await this.$refs.container;
    39. chatBox?.scrollTo({ top: chatBox.scrollHeight });
    40. });
    41. } else {
    42. this.$nextTick(async () => {
    43. const chatBox = await this.$refs.container;
    44. chatBox?.scrollTo({ top: chatBox.scrollHeight, behavior: 'smooth' });
    45. });
    46. }
    47. },
    48. },
    49. };
    50. script>
    51. <style lang="less" scoped>
    52. .main{
    53. padding-top: 88px;
    54. display: flex;
    55. flex-direction: column;
    56. height: 100%;
    57. .title{
    58. height: 100px;
    59. }
    60. .container{
    61. overflow: scroll;
    62. flex: 1;
    63. .item{
    64. height: 60px;
    65. }
    66. }
    67. .bottom{
    68. padding-top: 20px;
    69. background: #F7F8FA;
    70. }
    71. .input{
    72. width: 510px;
    73. line-height: 50px;
    74. font-size: 40px;
    75. margin-left: 20px;
    76. margin-right: 20px;
    77. margin-bottom: 6px;
    78. padding-top: 15px;
    79. }
    80. }
    81. style>

  • 相关阅读:
    利用Python制作全网通用的点赞器,绝绝子!
    cool-admin框架后端使用-node版本,配置多数据源
    分分钟让你学会栈和队列
    Python 实现行为驱动开发 (BDD) 自动化测试详解
    sqli-labs(less-11)
    C++ NULL 与nullptr 区别
    golang入门笔记——gRPC
    C++面向对象——类与对象
    网关中间件-Nginx(二)
    C语言进阶第八课 --------通讯录的实现
  • 原文地址:https://blog.csdn.net/yyhjifeng/article/details/134295066