Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select element cause a scroll to top of the page on iOS devices

I have a Bootstrap website with a <select> element inside a modal.

My problem is that in iOS (tried on iPhone 5) when I try to open the select to choose an option the background content (behind modal) automatically scrolls up to the top of the page.

I get this error on Safari and Google Search, instead no error on Chrome and Mercury Browser.

Does anyone know the reason and the solution of this problem? Thanks

like image 769
Gio Bact Avatar asked Feb 04 '15 11:02

Gio Bact


People also ask

How do I scroll to the top in IOS?

iPhone has had this feature for a long time, but many users aren't aware of it. You can tap the clock in the status bar to scroll to top in any app on your iPhone. If you have a iPhone X or newer devices, the ones with a “notch”, you can tap on either sides of the notch to scroll to the top.

How do I scroll to the bottom of the page in Safari?

If you use an external keyboard, ⌘ cmd ↓ will scroll to the bottom of the page, and ⌥ opt ↓ will scroll one page down.

How do I make my iPhone notes scroll automatically?

Enable automatic scrolling 1) Enter Presenter Mode. 2) Tap the Aa button from the top right. 3) Move the slider next to Auto Scroll which should turn orange. 4) Optionally move the slider beneath that to adjust the speed of the scroll.


1 Answers

I have the same issue and found solution that really solves this problem:

if( navigator.userAgent.match(/iPhone|iPad|iPod/i) ) {

    $('.modal').on('show.bs.modal', function() {

        // Position modal absolute and bump it down to the scrollPosition
        $(this)
            .css({
                position: 'absolute',
                marginTop: $(window).scrollTop() + 'px',
                bottom: 'auto'
            });

        // Position backdrop absolute and make it span the entire page
        //
        // Also dirty, but we need to tap into the backdrop after Boostrap 
        // positions it but before transitions finish.
        //
        setTimeout( function() {
            $('.modal-backdrop').css({
                position: 'absolute', 
                top: 0, 
                left: 0,
                width: '100%',
                height: Math.max(
                    document.body.scrollHeight, document.documentElement.scrollHeight,
                    document.body.offsetHeight, document.documentElement.offsetHeight,
                    document.body.clientHeight, document.documentElement.clientHeight
                ) + 'px'
            });
        }, 0);
    });
}

Hope this would be helpful for others who will have same problem.

like image 190
Ani Avatar answered Sep 27 '22 19:09

Ani