gpt4 book ai didi

javascript - 当键盘像 whatsapp 一样弹出时滚动整个 html/body

转载 作者:行者123 更新时间:2023-11-28 00:02:44 24 4
gpt4 key购买 nike

我正在使用 html5/javascript/jQuery/css 开发移动应用程序。 HTML body 包含 Header , footer 和中间的一个 div 。页脚包含一个文本框,用户可以在其中输入内容。每当用户触摸文本框时,键盘就会弹出。问题是每当弹出键盘时,html 正文都不会向上移动。如何实现相同?它应该与所有移动设备(Android、ios、windows)兼容。

这是我的html代码

<body onload="hidebtn()">
<header class="topBar">

<ul class="nav nav-tabs row" id="myTab" role="tablist">
<li class="nav-item col">
<a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true" style="padding-top: 20px; font-size: 14px; cursor: pointer;" onclick="chat()">Chat</a>
</li>
<li class="nav-item col">
<a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false" style="padding-top: 20px; font-size: 14px; cursor: pointer;" onclick="ticketStatus()">Incident</a>
</li>
<li class="nav-item col">
<a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false" style="padding-top: 20px; font-size: 14px; cursor: pointer;" onclick="ticketStatus()">Service Request</a>
</li>
</ul>

</header><br><br>
<div class="tab-content" id="myTabContent" style="overflow:hidden;">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<div class="container">
<div class="row h-100">
<div id="topButtons" class="card-body" style="padding: 0; margin-top: 12px; height: 80px;"></div>
<div id="chatBody" class="card-body anyClass">
<p class="WC_message_fl"><img class="con_im" src="images\chatrobo.png"></p>
<p class="WC_message_f"> Type your Questions & Start chatting </p>
</br></br>
</div>

</div>
</div>

</div>

<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab" >
</br></br> <p class="WC_message_fl" id='msg_table'></p>
<div class="container" style="overflow-x:hidden;overflow-y:auto;height:84vh;"><div class="row" id="table"></div>
</div>

</div>
<div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab" >
</br></br> <p class="WC_message_fl" id='msg_sr_table'></p>
<div class="container" style="overflow-x:hidden;overflow-y:auto;height:84vh;"><div class="row" id="sr_table"></div></div>
</div>

</div>




<footer id="footerChtbt" style="position:fixed;bottom:0;right:0;left:0;height: 45px;line-height: 20px;background-color: #ECEFF1;font-size: 0.6rem;border-top: 1px solid #aaa;box-shadow: 0 1px 5px 0 #9B9B9B;">
<span id="Resp" style="color:blue; position: absolute; z-index: 1000; left: 8vw;"></span>
<div class="container">
<div class="row">
<div class="col-10">
<input id="query" type="text" class="form-control" placeholder = 'Ask sth...'" onkeyup="pressedkey(event)" style="outline:0;box-shadow:none;font-size:14px;" required />
</div>
<div class="col-2" style="padding: 0; margin-top: 2px;">
<img src="images/send_icon.svg" name="submitbtn" onClick="sendbtn()" style="cursor: pointer; width: 40px;">
</div>
</div>
</div>
</footer>

最佳答案

无法检测虚拟键盘是否存在,但您不需要这样做。

我在你的 html 中找不到你的输入框,所以我假设它的 ID 是“textInput”

let textInput = $("#textInput")
//When the input box gets focused you just have to scroll it into view
textInput.focusin((event) => {
var elem = event.currentTarget;
//scrollIntoViewIfNeeded is non standard but supported in many browsers
if(elem.scrollIntoViewIfNeeded !== undefined) {
elem.scrollIntoViewIfNeeded()
} else if(elem.scrollIntoView !== undefined){
elem.scrollIntoView()
} else {
//Fallback if scrollIntoView is not supported
var offset = textInput.offset()
$('html, body').animate({
scrollTop: offset.top,
scrollLeft: offset.left
})
}
})
#textInput {
position: fixed;
bottom: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="textInput" type="text">

关于javascript - 当键盘像 whatsapp 一样弹出时滚动整个 html/body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55907832/

24 4 0
文章推荐: javascript - 线性渐变在 Chrome 上没有响应,但在 FireFox 上有响应,为什么?
文章推荐: html - 如何在