I am using a modal to display large res versions of images when viewing on mobile. This way the user can choose whether to load the larger file as well as examine the image in more detail.
The 1st issue is that my image is larger than the modal container as well as the viewport. I want it that way with the scroll bars so the user can just use their finger to move around and examine the full size image, no need to pinch/zoom.
Because the image is larger though, I am having trouble centering it. No matter what CSS centering tricks I try, it always loads at the top left of the image.
The 2nd problem is focus not being properly trapped in the modal.
When I am scrolling through the high res image, sometimes there is a glitch where the user interacts with the area behind the modal? I notice it when I quickly scroll downwards. Sometimes the fixed footer menu will appear.
I used this tutorial
and I was utilizing the overlay which I thought would prevent interaction with elements behind the modal, but like I said I still encounter the issue of the fixed footer appearing sometimes.
Any advice regarding this would be greatly appreciated!
You can centre an image that is smaller than the containing block but you canât centre an image that is larger than its containing block because it will just sit the top left of the block as you found out.
At the point the image becomes larger than the containing block a scrollbar will appear horizontally or vertically in order for you to see the rest of the image which is sticking out the right and bottom of your div.
Although we could force the image to be centred there is no scroll mechanism to let you see images that are shifted out to the left or above the top. For example if you forced the image to be central by absolutely placing its centre in the centre (top:50%;left:50%;transform:translate:(-50%)). That would mean the image would be centred but you could not scroll to see the parts sticking out the left and top.
It seems to me that the only solution is to use some JS when the modal is called and automatically set the scrollbar position to the 50% mark both horizontally and vertically. In that way the image would appear centred when loaded and the user could scroll in any direction.
Thank you for taking the time to explain that to me I really appreciate it!
It makes me feel better knowing its simply not possible with CSS, I have been trying so many different variations trying to make it work!
I will ask about positioning the scrollbars using javascript in the javascript part of the forums as well as do some research on the web to see if someone has figured that out.
Just view source as all the code is in the page. The yellow square on the image is the exact center of the image so if its working it should show in the centre when loaded. Of course if you resize the page you would need to call the script again but I doubt that will be an issue on mobile. You can test by resizing the window and then refreshing and calling the modal again.
I also added a class to the html element when the modal is active to hide the scrollbars on html and body so the rest of the page doesnât scroll accidentally.
It needs thorough testing with different sized images to make sure its working. You would also need to run the code when the modal is called and detect the image that was being used but as proof of concept it seems feasible.
It seems like although you fixed the scrolling the content behind the open modal issue, a related bug appeared.
When I use google chrome and scroll around the image, it sometimes will still show the body content holding the fixed navbar that is behind the modal. It appears right where the google address bar is. When you scroll up and down, that address bar disappears to show more of the image and when that happens it will briefly show content from behind the modal?
This does not seem to happen in firefox, but the address bar does not disappear in firefox, it is in a fixed position.
So I am not exactly sure why this is happening or how to prevent itâŚBut it seems like it has to do with google chrome having their disappearing address bar on scroll.
Also, do you think it would be possible to call the script again if I change the view from portrait to landscape on the mobile device? I ask because when I think about how users will interact, they will probably be holding their phone in portrait, click on an image, the modal opens and is centered still in portrait mode, then the user will probably position their phone to landscape mode for better viewing. When that happens it would be great if the script was activated again, because otherwise all that centering effort is not really appreciated when the image changes to landscape view.
I think you are talking about the standard bounce effect that ios allows you to scroll further than the height of the element. It happens at both the top and bottom and is meant to show you youâve reached the end of the scroll as it bounces back. The downside is that for a fixed element you get a glimpse of the content underneath.
There is an article here that talks about the issue. However I think the simplest thing to do is to wrap your page excluding the modal in a wrapper and when the modal is active set the opacity of the rest of the content to zero so that you canât see at all even if it does bounce.
Yes you could tie the script into the orientation change but it may be better to tie it into the resize event so everyone gets the centred page and not just some devices.
Iâve added the script to my above demo although it does complicate things a little as there is also a throttle script to prevent triggering the resize too many times.
Hey Paul. I really like the way the mobile view functions but the friend I am making the website for would rather have the image not be initially zoomed in, instead relying on the browsers native pinch zoom capabilities.
So I tried keeping your script but reverting to the earlier version that does not have the resize event throttle script as that seemed unnecessary now.
I changed the .img properties to max-width:100% and that seemed to make things work as I wanted when I viewed it on google chrome on my mobile. The issue is when I viewed it on firefox on my mobile it was broken, it seems like the fixed elements of the modal are preventing the pinch zoom from working.
That page seems to be blank or at least no way to trigger a modal to test.
Youâd need to set the image height to auto and also remove all the resizing from the script as it seems you just want a standard modal now?
However if the image is taller than it is wider you will run into the issue you had before where the centre of the image will not be the center of the viewport. That means you would need to check if the image was taller than it is wider and then use height:100% and width:auto instead of width:100% and height:auto. (You may actually just be better off getting a modal script âoff-the-shelfâ as there are loads of fully featured free ones around. :))
I donât have Firefox on a mobile but it may be something to do with the touch-action:none that was added so you could try removing that and see if it has an effect.
I am out most of today so wonât have time to play around but I think you need to consider exactly what you want to happen as the example is set up for the way you originally wanted. Just changing one aspect for mobile is not that simple as youâd need to change the script based on viewport size which would mean using matchMedia I would think. It may be better just to have a standard modal image that scales to viewport for all devices.
Sorry that page had the code on it but was set to only appear on mobile.
You are right, the example you had made works perfectly for what I originally wanted.
It does sound like what I am wanting now is just a standard modal, I just want to make sure I still utilize the overflow and wrapper techniques you showed me.
I tried out your standard modal code and it seemed to be working great on mobile with chrome.
With firefox on mobile I did try to change the touch-action and it did allow me to pinch zoom but the issue where the fixed elements are all broken and oversized is still happening.
I actually spent a really long time troubleshooting this bug but the only way I got around that was by making the modal element positioning no longer fixed. That solved the zooming issue on firefox which is awesome but it seemed to cause another issue.
Now when I close the modal, the scroll bar jumps to the top of the page. This is problematic because of the stack of modal trigger links I am utilizing, it makes the user have to always scroll back down to the next image.
You can see it happening on this page where I changed the position from fixed to absolute for the modal, modal-content and close classes.
Is there a solution perhaps with javascript I could apply to fix this scrolling issue and still keep the css property workaround I have for the firefox zoom issues?
I will look for other possible modal scripts as you suggest, but I feel like what you have created is accomplishing what I want.
So start with id=âlink1â and then increase for every element that is a trigger for the modal. When the close button is clicked the window.location is set to that fragment identifier ( window.location = linkClicked;). That will direct the page back down to where it was.
I havenât had time to look at the zooming problem in Firefox but I know that mobiles always have a hard time with fixed positioned elements.