【问题标题】:create cylinder shape in pure css 3d在纯 css 3d 中创建圆柱形状
【发布时间】:2014-08-31 11:53:13
【问题描述】:

我正在研究画布 3D 形状,对此我很陌生。我正在尝试创建没有任何插件的纯 css3d 圆柱体。

这是我正在尝试的,使用输出代码,我得到了一个圆圈。

CSS 代码:

div {
    height:200px;
    width:200px;
    border:solid 5px black;
    background:#159;
    border-radius:100%;
    display:inline-block;
    margin:1em;
    position:relative;
    text-align:center;
    line-height:200px;
    color:white;
    font-size:2em;
    transform:rotate(45deg);
    box-shadow:0 0 5px black, inset 0 0 5px #48a;
}

HTML 代码

<div>&nbsp;</div>

谁能帮帮我。

【问题讨论】:

  • 发布您的代码。发布您尝试过的内容。 SO 不是开箱即用的完整解决方案。
  • 嗨@gion_13 我添加了我正在尝试输出的代码只是圆形但我想要圆柱形状

标签: html css html5-canvas css-shapes


【解决方案1】:

有一些像这样的高级示例:
http://x.dtott.com/3d/
http://cssdeck.com/labs/pure-css-3d-primitives

还有一些有用的 CSS 形状,例如:
http://css-tricks.com/examples/ShapesOfCSS/

我个人构建了这个简单的 HTML

<div class="tank">
    <div class="bottom"></div>
    <div class="middle"></div>
    <div class="top"></div>  
</div>

和 CSS

.tank{
    position:relative;
     margin:50px;
}

.tank .middle{
    width:120px;
    height:180px;
    background-color:#444;
    position:absolute;
}

.tank .top{
    width: 120px;
    height: 50px;
    background-color:#666;
    -moz-border-radius: 60px / 25px;
    -webkit-border-radius: 60px / 25px;
    border-radius: 60px / 25px;
    position:absolute;
    top:-25px;
}

.tank .bottom{
    width: 120px;
    height: 50px;
    background-color:#444;
    -moz-border-radius: 60px / 25px;
    -webkit-border-radius: 60px / 25px;
    border-radius: 60px / 25px;
    position:absolute;
    top:155px;
    box-shadow:0px 0px 10px rgba(0,0,0,0.75)
}

你可以看到DEMO

【讨论】:

  • 不错的答案。也可以通过使用伪元素来完成单个 HTML 元素。
  • @harry 你是什么意思?由于在他的示例中有一个顶部中间和底部,你会使用哪个伪类?
  • @xxstevenxo 您可以使用 main 元素作为中间部分,使用两个伪元素作为顶部和底部,如 this fiddle
  • 哦,呃……不知道我是怎么错过的
  • 这实际上是 2D。旋转.tank 会暴露这一点。
【解决方案2】:

提供 ACTUAL 3D 示例的答案:

最终结果如下:

JSfiddle 演示:https://jsfiddle.net/allenski/xyfhag8d/(添加了有趣的动画)。

HTML 标记:

<div id="container">
  <div id="frame">
    <div class="strips">
      <div class="strip-1"></div>
      <div class="strip-2"></div>
      <div class="strip-3"></div>
      <div class="strip-4"></div>
      <div class="strip-5"></div>
      <div class="strip-6"></div>
      <div class="strip-7"></div>
      <div class="strip-8"></div>
      <div class="strip-9"></div>
      <div class="strip-10"></div>
      <div class="strip-11"></div>
      <div class="strip-12"></div>
      <div class="strip-13"></div>
      <div class="strip-14"></div>
      <div class="strip-15"></div>
      <div class="strip-16"></div>
      <div class="strip-17"></div>
      <div class="strip-18"></div>
      <div class="strip-19"></div>
      <div class="strip-20"></div>
      <div class="strip-21"></div>
      <div class="strip-22"></div>
      <div class="strip-23"></div>
      <div class="strip-24"></div>
    </div>
  </div>
</div>

CSS 标记:

#container {
  width: 100%;
  height: 100%;
  perspective: 900px;
}
#frame {
  position: relative;
  top: 50%;
  left: 50%;
  width: 23px;
  transform-style: preserve-3d;
  transform: rotateX(-20deg) rotateY(0deg) rotateZ(0deg);
}
.strips {
  transform-style: preserve-3d;
  transition: all 420ms;
}
.strips > div {
  position: absolute;
  top: -60px;
  left: -12px;
  margin-top: -10px;
  background: limegreen;
  border: solid darkgreen;
  border-width: 5px 0;
  height: 120px;
  width: 24px;
}
.strip-1 { transform: rotateY(0deg) translateZ(85px); }
.strip-2 { transform: rotateY(15deg) translateZ(85px); }
.strip-3 { transform: rotateY(30deg) translateZ(85px); }
.strip-4 { transform: rotateY(45deg) translateZ(85px); }
.strip-5 { transform: rotateY(60deg) translateZ(85px); }
.strip-6 { transform: rotateY(75deg) translateZ(85px); }
.strip-7 { transform: rotateY(90deg) translateZ(85px); }
.strip-8 { transform: rotateY(105deg) translateZ(85px); }
.strip-9 { transform: rotateY(120deg) translateZ(85px); }
.strip-10 { transform: rotateY(135deg) translateZ(85px); }
.strip-11 { transform: rotateY(150deg) translateZ(85px); }
.strip-12 { transform: rotateY(165deg) translateZ(85px); }
.strip-13 { transform: rotateY(180deg) translateZ(85px); }
.strip-14 { transform: rotateY(195deg) translateZ(85px); }
.strip-15 { transform: rotateY(210deg) translateZ(85px); }
.strip-16 { transform: rotateY(225deg) translateZ(85px); }
.strip-17 { transform: rotateY(240deg) translateZ(85px); }
.strip-18 { transform: rotateY(255deg) translateZ(85px); }
.strip-19 { transform: rotateY(270deg) translateZ(85px); }
.strip-20 { transform: rotateY(285deg) translateZ(85px); }
.strip-21 { transform: rotateY(300deg) translateZ(85px); }
.strip-22 { transform: rotateY(315deg) translateZ(85px); }
.strip-23 { transform: rotateY(330deg) translateZ(85px); }
.strip-24 { transform: rotateY(345deg) translateZ(85px); }

【讨论】:

    【解决方案3】:

    您的问题可能有几种不同的方法。
    第一个(也是最简单的)一个是有多个堆叠的圆圈,这会给你一个圆柱体的印象。
    但是谁想要一个页面中有数百个divs 只是为了呈现一个简单的图形模块?您可以在单个元素上使用多个 box-shadow 值来模拟最终模拟圆柱体的多个圆圈:

    div {
        box-shadow: black 0px 0px 1px,
            black 1px 1px 1px,
            black 2px 2px 1px,
            ...
            black 99px 99px 1px,
            black 100px 100px 1px;
    }
    

    下面是这个例子的一个小技巧:http://jsfiddle.net/gion_13/nDCme/

    【讨论】:

    • 嗨@gion_13 感谢分享代码,您可以将其设为字符串并降低圆柱体的不透明度。当我更改转换值时,它没有发生请帮助我
    • 这是非常棒的代码,我正在寻找这种类型的解决方案,现在我只想要两个校准 1) 使直线和 2) 使圆柱体不透明
    • 嗨@gion_13 请帮助我的上述查询,请做必要的事情
    • 圆柱体的方向box-shadow值的前2个坐标给出。它们实际上形成了一个给你一个角度的向量。例如,如果您希望它垂直向下,那么您应该将 box-shadow 向量更改为:black 0 1px 1px, black 0 2px 1px, black 0 3px 1px... 等等。
    • @user3820621 关于不透明度..您的要求可能有点不清楚。您可以设置 div 的 css opacity 属性,也可以使用 rgba() 值而不是 rgb/hex/color 名称来混淆实际颜色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多