【问题标题】:CSS fixed position causes soft keypad appearing over the textbox in androidCSS固定位置导致软键盘出现在android中的文本框上
【发布时间】:2019-11-03 23:18:36
【问题描述】:

我有一个聊天机器人窗口,必须将其放置在固定的右下角。我已将 CSS 设置放在下面,用于固定定位和视口设置。

CSS 中的聊天窗口固定在右下角,底部有一个文本框。

在 Android 设备中,当该文本框处于焦点位置时,软键盘将出现在文本框上方,而上方的 movnig 网页和定位文本框位于键盘上方则没有正常行为。所以用户看不到他们正在输入的内容。

截图:https://ibb.co/G0mNQV8

当我将聊天窗口的 CSS 位置更改为绝对位置时,焦点键盘正常工作。但是,如果滚动没有焦点,整个聊天窗口会向上移动,而不是固定在右下角。

我需要修复这两种情况,以获得焦点的软键盘的正常行为以及固定定位。

请参考以下代码sn-ps,如果能帮忙解决就好了。

@viewport {
  min-width: device-width ;
  zoom: 1.0 ;
  initial-scale:1.0;
  maximum-scale:1.0;
  user-scalable:1.0;
} 
@-ms-viewport {
  min-width: device-width ;
  zoom: 1.0 ;
  initial-scale:1.0;
  maximum-scale:1.0;
  user-scalable:1.0;
} 
#live-chat {
    display:none;
    bottom: 0;
    right: 0;
    position: fixed;  
    width: 99% !important;
    max-width:350px;
    max-height:100vh;
    background: #e9e9e9;
    color: #eae2e2;
    font: 100%/1.5em "Droid Sans", sans-serif;
    margin: 0;
    z-index: 10000;    box-shadow: 0px 0px 20px 4px #ccc;
}
<div id="live-chat-container">
    <div id="live-chat">



        <header class="clearfix">



            <a href="javascript:void(0);"  onclick="hidechat();" class="chat-close">x</a>



            <h4> <img src="https://www.xxxx.com/chat/cf-icon.png" width="32px" style="display:inline;vertical-align:middle;"> Chat with Us</h4>



            <span class="chat-message-counter">3</span>



        </header>



        <div class="chat">



            <div class="chat-history">


            </div> <!-- end chat-history -->



            <p class="chat-feedback" style="display:none;">Your partner is typing?</p>



            <form action="#" method="post" onsubmit="return chatsend();">

                <fieldset class="cbtgroup">

                    <input type="text" placeholder="Type your message?" id="cbt"  autocomplete="off"><input type="submit" value="&raquo;" class="cbt" id="cbtbtn">

                </fieldset>

            </form>
            <div style="font-size:xx-small;text-align:center;"><a href="https://www.xxxx.com" target="_blank">Powered by xxx.com</a></div>

        </div>



    </div> <!-- end live-chat -->

【问题讨论】:

    标签: android css android-keypad


    【解决方案1】:

    我找到了该问题的解决方案并将其发布在此处,因为它对遇到相同问题的任何人都有帮助。

    1. 我确实用一个额外的 div 包裹了聊天窗口的外部 div,给它固定的位置和顶部、底部 0。

    2. 然后将聊天窗口 div 的位置更改为 'relative 并具有其容器的 93.9% 的高度。

    3. 以上两个帮助解决了滚动问题(保持聊天窗口固定在右下角)和避免软键盘覆盖文本框

    4. 在 android 中,在此之后注意到另一个问题,即使文本框失去焦点,软键盘也会出现,因此使用取自 here 的以下 js 函数。

    #live-chat-wrapper{
        bottom: 0;
        right: 0px;
        position: fixed;
        width: 99% !important;
        max-width:351px;
        max-height:484px;
        margin: 0;
        z-index: 100000;
        -webkit-transform: translate3d(0,0,0);
    }
    
    #sigiriyalive-chat {
        display:none;
        bottom: 0;
        right: 0px;
        position: relative;
        width: 99% !important;
        max-width:350px;
        height: 93.9%;
        transition: height 1s ease;
        background: #e9e9e9;
        color: #eae2e2;
        font: 100%/1.5em "Droid Sans", sans-serif;
        margin: 0;
        z-index: 1000001;
        box-shadow: 0px 0px 20px 4px #ccc;
    }
    
    <div id="live-chat-wrapper">
        <div id="live-chat">
            <header class="clearfix">
                <a href="javascript:void(0);"  onclick="hidechat();" class="chat-close">x</a>
                   <h4> <img src="https://www.wm.xxx.com/chat/xx-icon.png" width="32px" style="display:inline;vertical-align:middle;"> Chat with Us</h4>
                <span class="chat-message-counter">3</span>
            </header>
            <div class="chat">
                <div class="chat-history">
                </div> <!-- end chat-history -->
                <p class="chat-feedback" style="display:none;">Your partner is typing?</p>
                <form action="#" method="post" onsubmit="return chatsend();">
                    <fieldset class="cbtgroup">
                        <input type="text" placeholder="Type your message?" id="cbt"  autocomplete="off"><input type="submit" value="&raquo;" class="cbt" id="cbtbtn">
                    </fieldset>
                </form>
                <div style="font-size:xx-small;text-align:center;"><a href="https://www.xx.com" target="_blank">Powered by xx.com</a></div>
            </div> <!-- end chat --> 
        </div> <!-- end sigiriyalive-chat -->
    </div>
    
    function hidekeyboard() {
      //this set timeout needed for case when hideKeyborad
      //is called inside of 'onfocus' event handler
      setTimeout(function() {
    
        //creating temp field
        var field = document.createElement('input');
        field.setAttribute('type', 'text');
        //hiding temp field from peoples eyes
        //-webkit-user-modify is nessesary for Android 4.x
        field.setAttribute('style', 'position:absolute; top: 0px; opacity: 0; -webkit-user-modify: read-write-plaintext-only; left:0px;');
        document.body.appendChild(field);
    
        //adding onfocus event handler for out temp field
        field.onfocus = function(){
          //this timeout of 200ms is nessasary for Android 2.3.x
          setTimeout(function() {
    
            field.setAttribute('style', 'display:none;');
            setTimeout(function() {
              document.body.removeChild(field);
              document.body.focus();
            }, 14);
    
          }, 200);
        };
        //focusing it
        field.focus();
    
      }, 50);
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 2015-06-29
      • 2012-09-12
      • 2019-11-01
      • 2010-11-02
      相关资源
      最近更新 更多