iOS 11 Safari bootstrap modal text area outside of cursor

前端 未结 14 926
暖寄归人
暖寄归人 2020-11-27 11:22

With iOS 11 safari, input textbox cursor are outside of input textbox. We did not get why it is having this problem. As you can see my focused text box is email text input b

14条回答
  •  半阙折子戏
    2020-11-27 11:59

    Personally, position: fixed scroll to top automatically. Quite annoying !

    To avoid penalizing other devices and versions I apply this fix only to the appropriate versions of iOS.


    **VERSION 1 - All modals fix**

    For the javascript/jQuery

    $(document).ready(function() {
    
        // Detect ios 11_x_x affected  
        // NEED TO BE UPDATED if new versions are affected
        var ua = navigator.userAgent,
        iOS = /iPad|iPhone|iPod/.test(ua),
        iOS11 = /OS 11_0|OS 11_1|OS 11_2/.test(ua);
    
        // ios 11 bug caret position
        if ( iOS && iOS11 ) {
    
            // Add CSS class to body
            $("body").addClass("iosBugFixCaret");
    
        }
    
    });
    

    For the CSS

    /* Apply CSS to iOS affected versions only */
    body.iosBugFixCaret.modal-open { position: fixed; width: 100%; }
    

    **VERSION 2 - Selected modals only**

    I modified the function to fire only for selected modals with a class .inputModal

    Only the modals with inputs should be impacted to avoid the scroll to top.

    For the javascript/jQuery

    $(document).ready(function() {
    
        // Detect ios 11_x_x affected
        // NEED TO BE UPDATED if new versions are affected 
        (function iOS_CaretBug() {
    
            var ua = navigator.userAgent,
            scrollTopPosition,
            iOS = /iPad|iPhone|iPod/.test(ua),
            iOS11 = /OS 11_0|OS 11_1|OS 11_2/.test(ua);
    
            // ios 11 bug caret position
            if ( iOS && iOS11 ) {
    
                $(document.body).on('show.bs.modal', function(e) {
                    if ( $(e.target).hasClass('inputModal') ) {
                        // Get scroll position before moving top
                        scrollTopPosition = $(document).scrollTop();
    
                        // Add CSS to body "position: fixed"
                        $("body").addClass("iosBugFixCaret");
                    }
                });
    
                $(document.body).on('hide.bs.modal', function(e) {
                    if ( $(e.target).hasClass('inputModal') ) {         
                        // Remove CSS to body "position: fixed"
                        $("body").removeClass("iosBugFixCaret");
    
                        //Go back to initial position in document
                        $(document).scrollTop(scrollTopPosition);
                    }
                });
    
            }
        })();
    });
    

    For the CSS

    /* Apply CSS to iOS affected versions only */
    body.iosBugFixCaret.modal-open { position: fixed; width: 100%; }
    

    For the HTML Add the class inputModal to the modal

    
    

    Nota bene The javascript function is now self-invoking


    **UPDATE iOS 11.3 - Bug corrected

提交回复
热议问题