css - 使用 CSS 转换的伪 3d 效果?

标签 css 3d css-transforms

我有一个包含小 divdiv,每个小 div 都有一个彩色球的 background-image:

<div class="container">
  <div class="ball" style="left: 100px; top: 50px;">
  <div class="ball" style="left: 120px; top: 100px;">
  <div class="ball" style="left: 140px; top: 150px;">
  <div class="ball" style="left: 160px; top: 200px;">
  <div class="ball" style="left: 180px; top: 250px;">
</div>

现在我想创建某种伪 3D 效果,其中 'top' 值越小的球越小,离底部越近的球看起来越大。

我已经阅读了一些关于 CSS3 变换和透视的内容,但我不知道该怎么做。

有什么方法可以根据 top 调整 divwidthheight -属性(property)?

最佳答案

您可以像这样创建一个变量来在 css 中存储最高值 style="--top: 10px" 并在以后像这样使用它 var(--top).

更新

您需要使用 JavaScript 使用 setProperty 方法设置 --top 属性,您可以使用 --top 获取值getPropertyValue 方法。请参阅动态更改 --top onclick 事件的示例,该事件会更改 --top 值以及 widthheight会自动调整。

代码片段

balls = document.getElementsByClassName('ball');

Array.prototype.forEach.call(balls, function(ball){
	ball.onclick = function() {
		ball.style.setProperty('--top', parseInt(ball.style.getPropertyValue('--top')) * 1.2 + 'px')
  }
})
.ball {
    background-image:url(https://upload.wikimedia.org/wikipedia/commons/e/ec/Soccer_ball.svg);
    width: calc(var(--top) * 1.2);
    height: calc(var(--top) * 1.2);
    background-size: cover;
    position: absolute;
}
<div class="container">
  <div class="ball" style="left: 100px; --top: 50px;"></div>
  <div class="ball" style="left: 120px; --top: 100px;"></div>
  <div class="ball" style="left: 140px; --top: 150px;"></div>
  <div class="ball" style="left: 160px; --top: 200px;"></div>
  <div class="ball" style="left: 180px; --top: 250px;"></div>
</div>

关于css - 使用 CSS 转换的伪 3d 效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42357308/

相关文章:

html - CSS :selectors animation stops working correctly after removing "required" tag from input field

css - bootstrap hidden-lg 类在 ie9 中不起作用

css - 使用纯 CSS 的垂直文本在浏览器之间不一致

html - 是否有类似 VRML 的高级 API 用于使用 HTML5 进行 3D 处理?

javascript - 没有 stretch img child 的 CSS ScaleY

javascript - 当 chrome 中的比例发生变化时,滚动条不会被修改

html - 跨越文本边框自身重叠

html - CSS - 如何制作带有 flex 边缘的倒置边框底部?

opengl-es - 在 WebGL 中创建 3D 自由相机 - 为什么这两种方法都不起作用?

ios - nineveh vs cocos3d