重叠的三角形

时间:2015-11-24 15:13:13

标签: css css3 svg css-shapes

这是我试图在纯CSS中创建的形状:

CSS overlapping triangles

我有一个更完整的jsfiddle http://jsfiddle.net/8Lxr5s57/7/。是否有更好,更有效的方法来实现同样的结果?

$(document).ready(function () {
  $('#jqxWidget').on('click', function () {

    var items = $("#jqxWidget").jqxListBox("getSelectedItems");
    if (items.length > 0) {
      var lstItem="";
      for (var i = 0; i < items.length;i++) {
        lstItem += items[i].label; //what does this label? 
      }
      alert(lstItem);
    }
  });
});
.angled_container {
  background-color: #fff;
  height: 200px;
  position: relative;
  overflow: hidden;
  clear: both;
}
.angled_container:before,
.angled_container:after {
  content: "";
  width: 110%;
  height: 100%;
  display: block;
  position: absolute;
  top: 0;
}
.angled_container:before {
  background-color: #606060;
  transform: rotate(12deg);
  -webkit-transform-origin: left top;
  left: 0;
}
.angled_container:after {
  background-color: #6bb2c6;
  transform: rotate(-12deg);
  -webkit-transform-origin: right top;
  left: -10%;
}
.angled_container--open-left:before {
  background-color: #6bb2c6;
  z-index: 2;
}
.angled_container--open-left:after {
  background-color: #606060;
  z-index: 1;
}

3 个答案:

答案 0 :(得分:9)

CSS

对于这两个三角形,我建议使用skewY()代替rotate()。它将避免一些定位问题,并防止使用比容器更广泛的伪元素:

.angled_container {
  height: 200px;
  position: relative;
  overflow: hidden;
}
.angled_container:before,
.angled_container:after {
  content: "";
  width: 100%; height: 100%;
  display: block;
  position: absolute;
  top: 0; left: 0;
}
.angled_container:before {
  background-color: #606060;
  transform: skewY(12deg);
  transform-origin: left top;
}
.angled_container:after {
  background-color: #6bb2c6;
  transform: skewY(-12deg);
  transform-origin: right top;
}
<div class="angled_container angled_container--open-right"></div>

SVG

或者,您可以使用带有polygon elements的内联SVG。这是完全响应的,可能更容易制作/维护,因为您可以使用fill属性在CSS中设置三角形的样式:

svg{display:block; width:100%;}
.first{
  fill:#606060;
}
.second{
  fill:#6bb2c6;
}
<svg viewbox="0 0 100 30">
  <polygon class="first" points="0 0 100 28 0 25 0 28"/>
  <polygon class="second" points="0 28 0 25 100 0 100 28 52 28 50 30 48 28 0"/>
</svg>

答案 1 :(得分:0)

将它们分解为三角形。这支持IE8 +。

.container {
  position: relative;
  width: 1000px;
  height: 260px;
  border-bottom: 40px solid #65abc2;
}

.grey {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  border-bottom: 260px solid #595959;
  border-right: 1000px solid transparent;
}

.blue {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  border-bottom: 260px solid #65abc2;
  border-left: 1000px solid transparent;
}

.container:after {
  content: '';
  display: block;
  position: absolute;
  bottom: -53px;
  /* included thickness of border-bottom */
  left: 50%;
  margin-left: -17px;
  width: 0px;
  height: 0px;
  border-top: 13px solid #65abc2;
  border-left: 17px solid transparent;
  border-right: 17px solid transparent;
}
<div class="container">
  <div class="grey"></div>
  <div class="blue"></div>
</div>

答案 2 :(得分:0)

您可以使用CSS triangles完成所有操作,但我不确定它是否比您已经拥有的更好。你需要伪元素::before::after来获得底部和迷你箭头的额外空间。

div {
    width:0;
    height:0;
    margin-top:55px;
    border-top:130px solid white;
    border-right:500px solid #6DB1C3;
    border-bottom:140px solid #6DB1C3;
    border-left:500px solid #5F5F5F;
    position:relative;
}
div:before {
    content:" ";
    position:absolute;
    bottom:-170px;
    width:1000px;
    height:30px;
    left:-500px;
    background:#6DB1C3;
}
div:after {
    content:" ";
    position:absolute;
    bottom:-202px;
    left:-20px;
    width:0;
    height:0;
    border:20px solid transparent;
    border-top:12px solid #6DB1C3;
}
<div></div>