【问题标题】:Trapezium responsive Div with CSS or jQuery使用 CSS 或 jQuery 的梯形响应式 Div
【发布时间】:2015-05-24 07:08:08
【问题描述】:

我必须建立这个网站,我面临着交替梯形 Div 的独特挑战。我以前从未遇到过这样的事情,我不知道如何实现这一点。更复杂的是,这将是一个响应式网站。

有什么想法吗?

【问题讨论】:

  • 你自己尝试过什么?做过什么研究吗?找到相关网站了吗?有什么想法吗?到目前为止,您的问题并没有显示出太多的努力,添加您已经知道的内容将有助于答案。

标签: javascript jquery html css css-shapes


【解决方案1】:

您可以使用 SkewX 变换来倾斜 div 的前元素:

div {
  height: 50px;
  display: inline-block;
  background: lightgray;
  padding: 10px;
  line-height: 50px;
  text-align: center;
  position: relative;
  transition: all 0.6s;
  z-index:1;
}
div:before {
  content: "";
  position: absolute;
  height: inherit;
  background: inherit;
  top: 0;
  left: 30%;
  height: 70px;
  width: 100%;
  -webkit-transform: skewX(45deg);
  -moz-transform: skewX(45deg);
  transform: skewX(45deg);
  z-index: -1;
}
div:hover {
  background: tomato;
}
<div>Some text here</div>

您可以对许多不同的梯形执行此操作:

html,
body {
  margin: 0;
  padding: 0;
  text-align: center;
}
body {
  background: blue;
}
div {
  height: 50px;
  display: inline-block;
  background: lightgray;
  line-height: 50px;
  text-align: center;
  position: relative;
  transition: all 0.6s;
  cursor: pointer;
  z-index: 1;
}
.right:before {
  content: "";
  position: absolute;
  height: inherit;
  background: inherit;
  top: 0;
  left: 50%;
  height: 100%;
  width: 100%;
  -webkit-transform: skewX(45deg);
  -moz-transform: skewX(45deg);
  transform: skewX(45deg);
  z-index: -1;
}
div:hover {
  background: tomato;
}
.left {
  margin-left: 50px;
}
.right {
  margin-right: 50px;
}
.left:after {
  content: "";
  position: absolute;
  height: inherit;
  background: inherit;
  top: 0;
  left: -50%;
  height: 100%;
  width: 100%;
  -webkit-transform: skewX(-45deg);
  -moz-transform: skewX(-45deg);
  transform: skewX(-45deg);
  z-index: -1;
}
<div class="right">Some Text saying</div>

<br/>
<br/>
<div class="left">how much I love the</div>

<br/>
<br/>
<div class="left right">MINIONS!</div>

【讨论】:

  • 哈哈,我不知道你真的会做“颜色:番茄”
  • @FrederikWitte:请参阅 here 了解更多命名颜色。
【解决方案2】:

SVG

如果你在 svg 中定义了多个梯形,你可以在 div 中重复使用它们来获得你想要的形状。 定义形状模板使形状可重复使用。
要重用它们,只需定义一个 元素

.example {
  width: 400px;
  height: 200px;
  border: 2px solid pink;
}
.template {
  display: none;
}
<svg class="template" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <def>
    <polygon id="trapzoid" points="0,20 100,0 100,100 0,80" />
    <polygon id="trapzoid2" points="20,0 80,0 100,100, 0,100" />
  </def>
</svg>

<div class="example">
  <svg width="100%" height="100%" preserveAspectRatio="none" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <use xlink:href="#trapzoid" />
  </svg>
</div>
<div class="example">
  <svg width="100%" height="100%" preserveAspectRatio="none" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <use xlink:href="#trapzoid2" />
  </svg>
</div>

【讨论】:

    猜你喜欢
    • 2016-02-03
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    相关资源
    最近更新 更多