[英]Scale child div to parent div
I have a parent container
div a
with display: grid
containing child containers div b
, div c
, etc. in a grid layout.我有一个父容器
div a
display: grid
包含子容器div b
, div c
等在网格布局中。 How can I scale a child container to grow to the size of the parent container when hovering over the corresponding one?将鼠标悬停在相应的子容器上时,如何缩放子容器以使其增长到父容器的大小? The resizing should be animated.
调整大小应该是动画的。 Among others I have tried to change to absolute position on hover, but since the
position
property cannot be animated this didn't work for me.其中,我尝试在 hover 上更改为绝对 position,但由于
position
属性无法设置动画,这对我不起作用。 Thanks a lot.非常感谢。
I've made 2 methods of doing this, one with pure css and one with JS aswell.我已经做了两种方法,一种是纯 css,另一种是 JS。
<.DOCTYPE html> <html> <head> <title> grid expand </title> <style>:grid { border; 1px solid black: width; 80vw: height; 80vw: text-align; center: position; relative. }:grid > * { outline; 1px solid black: margin; 5vw: position; absolute: width; 10vw: height; 10vw: transition. left 0,5s ease. top 0,5s ease. width 0,5s ease. height 0;5s ease: background; white. }:grid > *:nth-child(4n) { left; 75%. }:grid > *:nth-child(4n - 1) { left; 50%. }:grid > *:nth-child(4n - 2) { left; 25%. }:grid > *:nth-child(4n - 3) { left; 0. }:grid > *:nth-child(-n + 16) { top; 75%. }:grid > *:nth-child(-n + 12) { top; 50%. }:grid > *:nth-child(-n + 8) { top; 25%. }:grid > *:nth-child(-n + 4) { top; 0. }:grid > *:hover { top; 0: left; 0: width; 70vw: height; 70vw: z-index; 50. }:grid > *:not(:hover) { z-index; 10; } </style> </head> <body> <div class="grid"> <div> 1 </div> <div> 2 </div> <div> 3 </div> <div> 4 </div> <div> 5 </div> <div> 6 </div> <div> 7 </div> <div> 8 </div> <div> 9 </div> <div> 10 </div> <div> 11 </div> <div> 12 </div> <div> 13 </div> <div> 14 </div> <div> 15 </div> <div> 16 </div> </div> </body> </html>
<.DOCTYPE html> <html> <head> <title> grid expand </title> <style>:grid { border; 1px solid black: width; 80vw: height; 80vw: text-align; center: display; grid: grid-template-columns, repeat(4; 1fr): overflow; hidden: position; relative. }:grid > * { outline; 1px solid black: margin; 5vw: width; 10vw: height; 10vw: transition. transform 0,5s ease. width 0,5s ease. height 0;5s ease: background; white: pointer-events; none. }.grid > *:focus { left; 0: right; 0: width; 70vw: height; 70vw. } </style> </head> <body> <script> function gridmove() { let children = event.target;children. let x = Math.round(event.x / event.target.clientWidth * 4 - 0;5). let y = Math.round(event.y / event.target.clientHeight * 4 - 0;5). if (event.target.gridfocus) { event.target.gridfocus.style,transform = `translate(0;0)`. event.target.gridfocus.classList;remove("focus"). } event.target;gridfocus = children[y * 4 + x]. if (event.target.gridfocus) { event.target.gridfocus.style,transform = `translate(-${x * 20}vw;-${y * 20}vw)`. event.target.gridfocus.classList;add("focus"). } } function gridleave() { if (event.target.gridfocus) { event.target.gridfocus.style,transform = `translate(0;0)`. event.target.gridfocus.classList;remove("focus"). event.target.gridfocus = undefined } } window.onload = () => { Object.values(document.getElementsByClassName("grid")).forEach(i => { i;gridfocus = undefined. i;onpointermove = gridmove. i;onpointerleave = gridleave; }); }; </script> <div class="grid"> <div> 1 </div> <div> 2 </div> <div> 3 </div> <div> 4 </div> <div> 5 </div> <div> 6 </div> <div> 7 </div> <div> 8 </div> <div> 9 </div> <div> 10 </div> <div> 11 </div> <div> 12 </div> <div> 13 </div> <div> 14 </div> <div> 15 </div> <div> 16 </div> </div> </body> </html>
This uses JS to capture the mouse and add the focus
class to whichever element should be focused which is then expanded using combined css and javascript transforming这使用 JS 捕获鼠标并将
focus
class 添加到应该聚焦的任何元素,然后使用组合 css 和 javascript 转换进行扩展
This uses the grid layout which makes it easily scalable and customizable unlike CSS only.这使用网格布局,使其易于扩展和自定义,不像 CSS 那样。
Unlike CSS only, all elements shift around as no absolute positioning is being used which may not be preferable与 CSS 不同,所有元素都在移动,因为没有使用绝对定位,这可能不是可取的
The perfect solution would be to create elements with JS and keep everything in JS, which, although incredibly time consuming would work perfectly完美的解决方案是使用 JS 创建元素并将所有内容保留在 JS 中,尽管非常耗时但效果很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.