iOS 11 Safari bootstrap modal text area outside of cursor

前端 未结 14 756
暖寄归人
暖寄归人 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

    Incase anyone is looking for a fix in vanilla js that works on IOS >11.2 and doesnt require any additional CSS:

    (function() {
        if (!/(iPhone|iPad|iPod).*(OS 11_[0-2]_[0-5])/.test(navigator.userAgent)) return
    
        document.addEventListener('focusin', function(e) {
            if (!e.target.tagName == 'INPUT' && !e.target.tagName != 'TEXTAREA') return
            var container = getFixedContainer(e.target)
            if (!container) return
            var org_styles = {};
            ['position', 'top', 'height'].forEach(function(key) {
                org_styles[key] = container.style[key]
            })
            toAbsolute(container)
            e.target.addEventListener('blur', function(v) {
                restoreStyles(container, org_styles)
                v.target.removeEventListener(v.type, arguments.callee)
            })
        })
    
        function toAbsolute(modal) {
            var rect = modal.getBoundingClientRect()
            modal.style.position = 'absolute'
            modal.style.top = (document.body.scrollTop + rect.y) + 'px'
            modal.style.height = (rect.height) + 'px'
        }
    
        function restoreStyles(modal, styles) {
            for (var key in styles) {
                modal.style[key] = styles[key]
            }
        }
    
        function getFixedContainer(elem) {
            for (; elem && elem !== document; elem = elem.parentNode) {
                if (window.getComputedStyle(elem).getPropertyValue('position') === 'fixed') return elem
            }
            return null
        }
    })()
    

    What this does is:

    1. Check if the browser is Safari on iOS 11.0.0 - 11.2.5
    2. Listen for any focusin events on the page
    3. If the focused element is an input or a textarea and is contained in an element with fixed position, change the container position to absolute while regarding scrollTop and the containers original dimensions.
    4. On blur, restore the container's position to fixed.
    0 讨论(0)
  • 2020-11-27 11:59

    This solution worked for me and its working well across all browsers on iOS.

    .safari-nav-force{
    /* Allows content to fill the viewport and go beyond the bottom */
    height: 100%;
    overflow-y: scroll;
    /* To smooth any scrolling behavior */
    -webkit-overflow-scrolling: touch;
    }
    

    JavsScript

    var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
    $('.modal').on('shown.bs.modal', function () {
        if (iOS && $('.modal').hasClass('in')){
            $('html,body').addClass('safari-nav-force');
        }
    });
    $('.modal').on('hidden.bs.modal', function () {
        if (iOS && !$('.modal').hasClass('in')){
            $('html,body').removeClass('safari-nav-force');
        }
    });
    
    0 讨论(0)
提交回复
热议问题