简体   繁体   English

css 动画 - 将元素从其位置缩放到中心

[英]css animation - zooming element from its position to center

This is an example of the effect I want:这是我想要的效果示例:
http://photoswipe.com/ http://photoswipe.com/
The same effect is used for image zooming on WhatsApp web.同样的效果用于 WhatsApp 网页上的图像缩放。

I want to zoom elements (not just images) to the center, with an animation scaling element from its position to the center of the page.我想将元素(不仅仅是图像)缩放到中心,并使用从其位置到页面中心的动画缩放元素。
The animation should be css based, JS should not be used for animation purposes.动画应该是基于 css 的,JS 不应该用于动画目的。

I've tried the following code, which doesn't do the job:我尝试了以下代码,但它不起作用:

<div></div>

With the css:使用CSS:

div {
  width: 100px; height: 100px; background: blue;
transition: transform 1s
}

div:hover {
  transform: scale(2);
}

And, what's the difference between animating transform: scale or width/height?而且,动画变换之间有什么区别:比例或宽度/高度?

Thanks谢谢

EDIT:编辑:
Another attempt: http://jsfiddle.net/4w06Lvms/另一种尝试: http : //jsfiddle.net/4w06Lvms/

I have made something similar to what you want (view in full screen).我做了一些类似于你想要的东西(全屏查看)。 You can modify it as per needs.您可以根据需要对其进行修改。 Move pointer out of the screen to get back to original state.将指针移出屏幕以返回原始状态。

Also, animating using scale is better as you might not know the exact height and width in pixels if there are multiple images and using the same scale value gives your code uniformity.此外,使用比例制作动画效果更好,因为如果有多个图像,您可能不知道以像素为单位的确切高度和宽度,并且使用相同的比例值可以使您的代码统一。 Hope it helps.希望能帮助到你。

 html,body{ margin:0; } .container{ background:yellow; display:flex; width:100vw; height:100vh; justify-content:center; transition: 0.5s ease; vertical-align:center; } .imageHover{ display:flex; height:300px; width:200px; transition: 0.5s ease; } img{ position:absolute; height:300px; width:200px; transition: 0.5s ease; } .container:hover > .imageHover{ height:100vh; background-color:black; width:100vw; transition: 0.5s ease; } .container:hover > .imageHover > img{ transform:translate(240%,50%) scale(1.6); transition: 0.5s ease; }
 <div class="container"> <div class="imageHover"> <img id="yo" src="https://picsum.photos/200/300 " alt=""> </div> </div>

You can set the position of the image to fixed, but you will need to know the offset of x and y position of the image, after that start the animation.您可以将图像的位置设置为固定,但是您需要知道图像的 x 和 y 位置的偏移量,然后才能启动动画。

In this case the offset x and y are 30px, because I've set parent div padding to 30px.在这种情况下,x 和 y 的偏移量为 30px,因为我已将父 div 填充设置为 30px。

When the window is scrolled down or right.当窗口向下或向右滚动时。 You have to recalculate the top and left values of the image.您必须重新计算图像的顶部和左侧值。 For that you'll need JS.为此,您将需要 JS。

I've set the top and left to the offset values before I've set the position to fixed.在将位置设置为固定之前,我已将顶部和左侧设置为偏移值。 Then the image starts moving at the right position.然后图像开始在正确的位置移动。

On photoswipe, they are using a translate3d() function and they are using JS, too.在 photowipe 上,他们使用了 translate3d() 函数,他们也在使用 JS。 I have no idea what they are doing.我不知道他们在做什么。

 #image { /* top and left offset */ top: 30px; left: 30px; width: 200px; height: 200px; transition: top 2s, left 2s, transform 2s; } #image:hover { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); } #offset { background-color: beige; height: 1000px; /* given offset */ padding: 30px; }
 <div id="offset"> <img id="image" src="https://picsum.photos/200/300 " alt=""> </div>

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

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