iOS 11 Safari bootstrap modal text area outside of cursor

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

    Those solutions using position: fixed and position correction based on scrollTop work really well, but some people (including me) got another issue: keyboard caret/cursor not showing when inputs are focused.

    I observed that caret/cursor works only when we DON'T use position: fixed on body. So after trying several things, I gave up on using this approach and decided to use position: relative on body and use scrollTop to correct modal's top position instead.

    See code below:

    var iosScrollPosition = 0;
    
    function isIOS() {
       // use any implementation to return true if device is iOS
    }
    
    function initModalFixIOS() {
        if (isIOS()) {
            // Bootstrap's fade animation does not work with this approach
            // iOS users won't benefit from animation but everything else should work
            jQuery('#myModal').removeClass('fade');
        }
    }
    
    function onShowModalFixIOS() {
        if (isIOS()) {
            iosScrollPosition = jQuery(window).scrollTop();
            jQuery('body').css({
                'position': 'relative', // body is now relative
                'top': 0
            });
            jQuery('#myModal').css({
                'position': 'absolute', // modal is now absolute
                'height': '100%',
                'top': iosScrollPosition // modal position correction
            });
            jQuery('html, body').css('overflow', 'hidden'); // prevent page scroll
        }
    }
    
    function onHideModalFixIOS() {
        // Restore everything
        if (isIOS()) {
            jQuery('body').css({
                'position': '',
                'top': ''
            });
            jQuery('html, body').scrollTop(iosScrollPosition);
            jQuery('html, body').css('overflow', '');
        }
    }
    
    jQuery(document).ready(function() {
        initModalFixIOS();
        jQuery('#myModal')
            .on('show.bs.modal', onShowModalFixIOS)
            .on('hide.bs.modal', onHideModalFixIOS);
    });
    

提交回复
热议问题