简体   繁体   English

不使用比例缩放图像

[英]Zoom image without using scale

General Info基本信息
Working on creating my own carousel.致力于创建我自己的轮播。 The idea is that the image zooms in slowly at the start of each carousel "page".这个想法是图像在每个轮播“页面”的开头慢慢放大。

The Problem To achieve the result I'm looking for, all I have to do is change the scale of the background image using a transition.问题要达到我正在寻找的结果,我所要做的就是使用过渡更改背景图像的比例。 Simply wrap it in a container with overflow:hidden and it works.只需将其包装在一个带有overflow:hidden就可以了。 The problem is that the next carousel page needs to start its image "zoomed out".问题是下一个轮播页面需要从“缩小”的图像开始。 So the scale needs to be set to 1 again.所以规模需要再次设置为1。 However, since the CSS class has the transition, zooming out isn't instant.但是,由于 CSS 类具有过渡,因此缩小不是即时的。

The solution to that problem should be to simply remove the class from the element.该问题的解决方案应该是简单地从元素中删除类。 Then change the scale and finally add the class back in. However, for some reason, that doesn't seem to work.然后更改比例,最后重新添加类。但是,由于某种原因,这似乎不起作用。

Another issue is that scale seems to have a very bad browser support at the moment: https://caniuse.com/?search=scale另一个问题是scale目前似乎对浏览器的支持很差: https ://caniuse.com/?search=scale

Example code示例代码

 let scaled = false; const test = document.getElementById("test"); window.addEventListener("click", function() { if(scaled) { // Remove the class test.classList.remove("foo"); scaled = false; // Reset the scale test.style.transform = "scale(1)"; // Add class back in making sure the animation should be updated window.requestAnimationFrame(() => { test.classList.add("foo"); }); } else { // Leave the animation running scaled = true; test.style.transform = "scale(0.5)"; } })
 .foo { width: 500px; height: 500px; background: blue; transition: transform 2s; }
 <div id="test" class="foo"> </div>

You'll have to run the above snipped in Firefox or Safari as scale is not supported in other browsers at this time.您必须在 Firefox 或 Safari 中运行上述截图,因为目前其他浏览器不支持缩放。

On the first click, the animation plays as it should.在第一次单击时,动画会按应有的方式播放。 On the second click, the animation still plays while it shouldn't as the class has been removed.在第二次单击时,动画仍然会播放,而它不应该播放,因为该类已被删除。

My questions我的问题
Is there an alternative way to achieve the same effect?有没有其他方法可以达到同样的效果? (so don't use scale at all) Why is it playing the animation anyway despite the class being removed? (所以根本不要使用比例)为什么尽管类被删除了,但它仍然在播放动画?

The problem based on Event Loop .基于事件循环的问题。 You can try this solution with width if you like, but actually not sure that cover you needs.如果你愿意,你可以试试这个带宽度的解决方案,但实际上不确定你需要那个封面。

 const test = document.getElementById("test"); window.addEventListener("click", function() { test.style.transition = 'none' test.style.width = '100%'; window.requestAnimationFrame(() => { test.style.width = "50%"; test.style.transition = 'width 2s' }); })
 .foo { width: 500px; height: auto; background: blue; }
 <img src="https://images.freeimages.com/images/large-previews/76e/abstract-1-1174741.jpg" class="foo" id="test"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM