gpt4 book ai didi

jquery - 文本区域自动调整内容高度 jQuery

转载 作者:行者123 更新时间:2023-12-01 03:42:37 25 4
gpt4 key购买 nike

问题是,当文本区域高度高于窗口时,滚动条会粘在文本区域的顶部,而此时它应该将焦点放在插入符号所在的位置。

这是我的代码

function adaptiveheight(a) {
$(a).height(0);
var scrollval = $(a)[0].scrollHeight;
$(a).height(scrollval);
}

这是一个fiddle

尝试让文本区域变长,你就会发现问题。

我尽可能不想要第三方插件。谢谢

最佳答案

将此代码添加到 adaptiveheight(a) 函数的末尾

if (parseInt(a.style.height) > $(window).height() - 30) {
$(document).scrollTop(parseInt(a.style.height));
}

工作演示 http://jsfiddle.net/cse_tushar/ve4rL/3/

新的Js代码

$("#description").keyup(function (e) {
adaptiveheight(this);
});
i=0;
j=0;
function adaptiveheight(a) {
$(a).height(0);
var scrollval = $(a)[0].scrollHeight;
$(a).height(scrollval);
if (parseInt(a.style.height) > $(window).height()) {
if(j==0){
max=a.selectionEnd;
}
j++;
var i =a.selectionEnd;
console.log(i);
if(i >=max){
$(document).scrollTop(parseInt(a.style.height));
}else{
$(document).scrollTop(0);
}
}
}

工作演示 http://jsfiddle.net/cse_tushar/ve4rL/5/

关于jquery - 文本区域自动调整内容高度 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17802866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com