Closed artemartemov closed 5 years ago
You can achieve this behavior with the current API:
mediumZoom('#zoom-margin', {
margin: window.innerWidth < 1000 ? 0 : 48,
})
Let me know if your case is more complex.
I just came here with this exact question.
Might I suggest adding this example to the docs? I'm not a developer and this solution wouldn't have occured to me.
Hi! @francoischalifour and @artemartemov I managed to solve this by adding support for css variables while supporting int values as well. CSS variables can be changed with media queries as usual.
Merging this code to medium-zoom would be awesome.
I added var margin
below these lines. If something goes bad it will fall back to 0.
var viewportWidth = void 0;
var viewportHeight = void 0;
var margin = parseInt(getComputedStyle(document.documentElement).getPropertyValue(zoomOptions.margin) || zoomOptions.margin) || 0;
Then, replaced the usage of zoomOptions.margin
in the code below those lines with just margin
And everything works as expected. It only supports pixels by now. Haven't figured out how to make it work with em/rem or percentage values. But it is still far better than nothing.
For instance, the css code in the page stylesheet would be:
:root {
--medium-zoom-margin: 100px;
}
@media only screen and (max-width: 1000px) {
:root {
--medium-zoom-margin: 40px;
}
}
@media only screen and (max-width: 650px) {
:root {
--medium-zoom-margin: 0px;
}
}
And the code to call mediumZoom:
const zoom = mediumZoom('[data-zoomable]', {
margin: '--medium-zoom-margin',
})
@Arecsu Interesting, I'll consider this when updating the API.
Setting different margins on mobile devices vs larger screens
Implementation