javascript - 当可编辑的 div 改变其高度时附加一个类

标签 javascript jquery css scroll contenteditable

我有一个可编辑的 div,溢出设置为隐藏,我希望 div 在用户键入时扩展(如果需要)。每次高度变化时,我都使用 jquery 来更改 div 的类。 我似乎根本无法开始工作,它改变了类,但什么也没发生

Fiddle

HTML:

<div class="contanier">
  <div class="1" id="msgWriteArea" contenteditable="true"></div>
</div>

CSS:

#msgWriteArea{
    outline: 0;
  border: none;
    resize: none;
    width: 100%;
  height:20px;
  line-height:20px;
  font-size:18px;
  border:solid 1px #000;
  background:#FFF;
}
.contanier .1{
  height:20px;
  overflow:hidden;
}
.contanier .2{
  height:40px;
  overflow:hidden;
}
.contanier .3{
  height:60px;
  overflow:hidden;
}
.contanier .4{
  height:80px;
  overflow:hidden;
}
.contanier .5{
  height:80px;
  overflow-y:scroll;
}

Javascript:

var chatBoxSize = {
    oldHeight : 0,
    scrollHeight : 0,
    lastClass : 1,
    maxClass : 5
};
function updateChatSize() {
    var id = '#msgWriteArea';
    var element = document.querySelector(id);
    if((element.offsetHeight < element.scrollHeight) || (element.offsetWidth < element.scrollWidth)){
        if(chatBoxSize.lastClass == null){
            //add size 1
            console.log('ADD SIZE 1');
            $(id).addClass('1');
            chatBoxSize.lastClass = '1';
        } else if(chatBoxSize.oldHeight != $(id)[0].scrollHeight){
            //get the correct size to add
            if(parseInt(current) >= parseInt(chatBoxSize.maxClass)){
                var current = chatBoxSize.maxClass,
                last = parseInt(chatBoxSize.maxClass) - 1;
                chatBoxSize.lastClass = current;
                console.log('IS AT MAX SIZE');
            } else if(chatBoxSize.scrollHeight < $(id)[0].scrollHeight){
                var current = parseInt(chatBoxSize.lastClass) + 1,
                last = parseInt(chatBoxSize.lastClass);
                chatBoxSize.lastClass = current;
            } else if(chatBoxSize.scrollHeight > $(id)[0].scrollHeight) {
                var current = parseInt(chatBoxSize.lastClass) - 1,
                last = parseInt(chatBoxSize.lastClass);
                chatBoxSize.lastClass = current;
            } else {
                //console.log('No Change in height');
            }
      if(last != undefined){
        console.log('Add', current, 'Remove', last);
        $('#msgWriteArea').addClass(current + "");
                $('#msgWriteArea').removeClass(last + "");
        $('#display').val('Add ' + current + ' Remove' + last);
      }
        }
        chatBoxSize.oldHeight = element.offsetHeight;
        chatBoxSize.scrollHeight = $(id)[0].scrollHeight;
        chatBoxSize.oldHeight = element.offsetHeight;
    }
};
$(function (){
    $('#msgWriteArea').bind('change keydown input', function () {
        if(event.type == 'keydown'){
            updateChatSize();
        }
    });
});

这是一个更大页面的一部分。因此需要类对其他元素进行更改,但现在只需更改一个元素即可了解其工作原理

最佳答案

您可以通过添加此 CSS 来实现:

#msgWriteArea{
    height: auto !important;
    max-height: 80px; // Max Chat Box Size.. 
    overflow-y: scroll;
}

参见 JsFiddle

关于javascript - 当可编辑的 div 改变其高度时附加一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36792035/

相关文章:

php - datepicker 回调在 IE 中不起作用

javascript - 拖动图像时底层对象上的 jQuery 鼠标事件

javascript - 如何在新版fancybox中默认隐藏标题

html - Form Spacing justify-content-between 一个 <div> 到左边和一个 <span> 到右边

jquery - 具有不同 css 的多个 div/同一类

javascript - Polymer 1.0 - 通过模板绑定(bind)上的嵌套数组递增。

javascript - jQuery 选择器,DIV 内的所有跨度

jquery - 背景图像过渡不起作用

javascript - 即使单击返回顶部按钮也会关闭弹出窗口

javascript - 命名 Vue 3 js 模块时以 'use' 开头的文件名是惯例吗?