【问题标题】:How can I make a circular sector using CSS如何使用 CSS 制作圆形扇区
【发布时间】:2021-03-04 21:06:35
【问题描述】:

我想使用 CSS 制作一个圆形扇区。这些扇区将形成一个完整的圆圈。如何使用 CSS 制作它?

我找到了sample,但它是四分之一圆形扇区。我想做六个扇形来组成一个完整的圆。我怎样才能做到?

注意:我不擅长画画,但这里有一个我想要的样本......

div {
  width: 50px;
  height: 50px;
  background-color: #ccc;
  background-color: #ccc;
  -moz-border-radius: 100px;
  border-radius: 100px;
}
#center {
  position: relative;
  margin: 100px 0 0 100px;
  border: solid #fff 1px;
}
#tl,#tr,#bl,#br {
  position: absolute;
  height: 75px;
  width: 75px;
  z-index: -1;
}
#tl {
  border-radius: 100px 0 0 0;
  -moz-border-radius: 100px 0 0 0;
  top: -50px;
  left: -50px;
}
#tr {
  border-radius: 0 100px 0 0;
  -moz-border-radius: 0 100px 0 0;
  top: -50px;
  left: 26px;
}
#bl {
  border-radius: 0 0 0 100px;
  -moz-border-radius: 0 0 0 100px;
  top: 26px;
  left: -50px;
}
#br {
  border-radius: 0 0 100px 0;
  -moz-border-radius: 0 0 100px 0;
  top: 26px;
  left: 26px;
}
<div id="center">
  <div id="tl"></div>
  <div id="tr"></div>
  <div id="bl"></div>
  <div id="br"></div>
</div>

【问题讨论】:

标签: html css css-shapes


【解决方案1】:

我已经完成了菜单:Circular menu v6 - jsFiddle

此菜单中不涉及 JavaScript。它是用纯 CSS 完成的!

代码如下:

#menu {
  background: #aaa;
  position: relative;
  width: 200px;
  height: 200px;
  margin: 0 auto;
  overflow: hidden;
  border-radius: 100px;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
}

#center {
  position: absolute;
  left: 70px;
  top: 70px;
  width: 60px;
  height: 60px;
  z-index: 10;
  background: #eee;
  background: linear-gradient(top, #eee, #aaa);
  background: -moz-linear-gradient(top, #eee, #aaa);
  background: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#aaa));
  border-radius: 30px;
  -moz-border-radius: 30px;
  -webkit-border-radius: 30px;
}

#center a {
  display: block;
  width: 100%;
  height: 100%
}

.item {
  background: #aaa;
  overflow: hidden;
  position: absolute;
  width: 100px;
  height: 100px;
  transform-origin: 100% 100%;
  -moz-transform-origin: 100% 100%;
  -webkit-transform-origin: 100% 100%;
  transition: background .5s;
  -moz-transition: background .5s;
  -webkit-transition: background .5s;
  -o-transition: background .5s;
  -ms-transition: background .5s;
}

.item:hover {
  background: #eee
}

.item1 {
  z-index: 1;
  transform: rotate(60deg);
  -moz-transform: rotate(60deg);
  -webkit-transform: rotate(60deg);
}

.item2 {
  z-index: 2;
  transform: rotate(120deg);
  -moz-transform: rotate(120deg);
  -webkit-transform: rotate(120deg);
}

.item3 {
  z-index: 3;
  transform: rotate(180deg);
  -moz-transform: rotate(180deg);
  -webkit-transform: rotate(180deg);
}

.item4 {
  z-index: 4;
  transform: rotate(240deg);
  -moz-transform: rotate(240deg);
  -webkit-transform: rotate(240deg);
}

.item5 {
  z-index: 5;
  transform: rotate(300deg);
  -moz-transform: rotate(300deg);
  -webkit-transform: rotate(300deg);
}

.item6 {
  border: none;
  position: absolute;
  z-index: 6;
  transform: rotate(-30deg);
  -moz-transform: rotate(-30deg);
  -webkit-transform: rotate(-30deg);
}

#wrapper6 {
  position: absolute;
  width: 100px;
  height: 100px;
  overflow: hidden;
  transform-origin: 100% 100%;
  -moz-transform-origin: 100% 100%;
  -webkit-transform-origin: 100% 100%;
}

.item1 .content {
  left: -10px;
  top: 15px;
  transform: rotate(-60deg);
  -moz-transform: rotate(-60deg);
  -webkit-transform: rotate(-60deg);
}

.item2 .content {
  left: -11px;
  top: 16px;
  transform: rotate(-120deg);
  -moz-transform: rotate(-120deg);
  -webkit-transform: rotate(-120deg);
}

.item3 .content {
  left: -7px;
  top: 12px;
  transform: rotate(-180deg);
  -moz-transform: rotate(-180deg);
  -webkit-transform: rotate(-180deg);
}

.item4 .content {
  left: -5px;
  top: 18px;
  transform: rotate(-240deg);
  -moz-transform: rotate(-240deg);
  -webkit-transform: rotate(-240deg);
}

.item5 .content {
  left: -10px;
  top: 20px;
  transform: rotate(-300deg);
  -moz-transform: rotate(-300deg);
  -webkit-transform: rotate(-300deg);
}

.item6 .content {
  left: 20px;
  top: -10px;
  transform: rotate(30deg);
  -moz-transform: rotate(30deg);
  -webkit-transform: rotate(30deg);
}

.content,
.content a {
  width: 100%;
  height: 100%;
  text-align: center
}

.content {
  position: absolute;
}

.content a {
  line-height: 100px;
  display: block;
  position: absolute;
  text-decoration: none;
  font-family: 'Segoe UI', Arial, Verdana, sans-serif;
  font-size: 20px;
  text-shadow: 1px 1px #eee;
  text-shadow: 0 0 5px #fff, 0 0 5px #fff, 0 0 5px #fff
}

.display-target {
  display: none;
  text-align: center;
  opacity: 0;
}

.display-target:target {
  display: block;
  opacity: 1;
  animation: fade-in 1s;
  -moz-animation: fade-in 1s;
  -webkit-animation: fade-in 1s;
  -o-animation: fade-in 1s;
  -ms-animation: fade-in 1s;
}

@keyframes fade-in {
  from {
    opacity: 0
  }
  to {
    opacity: 1
  }
}

@-moz-keyframes fade-in {
  from {
    opacity: 0
  }
  to {
    opacity: 1
  }
}

@-webkit-keyframes fade-in {
  from {
    opacity: 0
  }
  to {
    opacity: 1
  }
}

@-o-keyframes fade-in {
  from {
    opacity: 0
  }
  to {
    opacity: 1
  }
}

@-ms-keyframes fade-in {
  from {
    opacity: 0
  }
  to {
    opacity: 1
  }
}
<div id="menu">
  <div class="item1 item">
    <div class="content"><a href="#one">one</a></div>
  </div>
  <div class="item2 item">
    <div class="content"><a href="#two">two</a></div>
  </div>
  <div class="item3 item">
    <div class="content"><a href="#three">three</a></div>
  </div>
  <div class="item4 item">
    <div class="content"><a href="#four">four</a></div>
  </div>
  <div class="item5 item">
    <div class="content"><a href="#five">five</a></div>
  </div>
  <div id="wrapper6">
    <div class="item6 item">
      <div class="content"><a href="#six">six</a></div>
    </div>
  </div>
  <div id="center">
    <a href="#"></a>
  </div>
</div>
<div id="one" class="display-target">Welcome!
  <br />This changing effect is done by ...</div>
<div id="two" class="display-target">... having <code>&lt;div&gt;</code>s with <code>id</code>s ... </div>
<div id="three" class="display-target">... that have the style <code>display: none</code> and the style
  <pre style="text-align: left">:target {
    display: block;
}</pre>so that these messages appear when there is a hash tag like <code>#three</code> (look at the address bar!)</div>
<div id="four" class="display-target">Look at the source of this page ...</div>
<div id="five" class="display-target">... to see how the circular menu works.</div>
<div id="six" class="display-target">By Shaquin Trifonoff</div>
<p><a href="http://stackoverflow.com/users/1421049/shaquin-trifonoff" title="My profile on Stack Overflow">By Shaquin Trifonoff</a></p>

【讨论】:

  • +1 我喜欢这个用 CSS 完成的答案,包括淡入淡出动画。干杯!
  • @arttronics 感谢您将它放在 jsFiddle 上!我忘了!
  • @arttronics - 我已经用菜单上的链接(使用:target)链接到一个小提琴(在编辑中),这就是我网站上的样子。
  • 小提琴似乎在.item 类上缺少border-radius: 100px 0 0 0;,否则环绕在chrome 中不是圆形的。
  • @Jeemusu - 谢谢你,它看起来像一个仅限 Chrome 的错误(它不会在 #menu 上听 overflow: hidden 并且在 Firefox 上工作正常),我做了一个小提琴@ 987654322@ 与 border-radius-webkit-border-radius,因为我不知道这个 bug 的年龄。 (这可能是因为transform,Firefox 有一个z-index 错误与transform 而不是overflow 错误)。
【解决方案2】:

简单的圆圈分成 6 个扇区

在伪元素上使用边框来制作三角形:

DEMO

输出:

.circle {
  position: relative;
  display: inline-block;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  overflow: hidden;
}

.circle:before,
.circle:after,
.circle > div:before,
.circle > div:after {
  content: '';
  position: absolute;
  z-index: -1;
}

.circle:before {
  bottom: 50%;
  left: 50%;
  margin-left: 2px;
  margin-bottom: 2px;
  border-top: 300px solid transparent;
  border-right: 150px solid gold;
}

.circle:after {
  top: 50%;
  left: 50%;
  margin-left: 2px;
  margin-top: 2px;
  border-right: 150px solid gold;
  border-bottom: 300px solid transparent;
}

.sector1:before {
  bottom: 50%;
  right: 50%;
  margin-right: 2px;
  margin-bottom: 2px;
  border-top: 300px solid transparent;
  border-left: 150px solid gold;
}

.sector1:after {
  bottom: 50%;
  margin-bottom: 5px;
  border-top: 300px solid gold;
  border-right: 150px solid transparent;
  border-left: 150px solid transparent;
}

.sector2:before {
  top: 50%;
  right: 50%;
  margin-right: 2px;
  margin-top: 2px;
  border-bottom: 300px solid transparent;
  border-left: 150px solid gold;
}

.sector2:after {
  top: 50%;
  margin-top: 5px;
  border-right: 150px solid transparent;
  border-bottom: 300px solid gold;
  border-left: 150px solid transparent;
}

.round-middle {
  position: absolute;
  top: 96px;
  left: 96px;
  width: 100px;
  height: 100px;
  border: 4px solid #fff;
  border-radius: 50%;
  background: gold;
}
<div class="circle">
  <div class="sector1"></div>
  <div class="sector2"></div>
  <div class="round-middle"></div>
</div>

【讨论】:

    【解决方案3】:

    可以使用-moz-transform 来完成,试试这个fiddle。实际上,我们不需要.sec1.sec_.sec4.sec_。然后.sec5.sec_.sec6

    #container {
      margin: 50px 0 0 50px;
      position: relative;
    }
    
    #center {
      position: absolute;
      width: 50px;
      height: 50px;
      background-color: #ccc;
      -moz-border-radius: 100px;
      border-radius: 100px;
      position: relative;
      margin: 50px 0 0 0px;
      border: solid #fff 1px;
      z-index: 1000;
    }
    
    .outer_sec {
      width: 75px;
      height: 75px;
      overflow: hidden;
      position: absolute;
      left: -50px;
      top: 25px;
    }
    
    .sec {
      position: absolute;
      height: 75px;
      width: 75px;
      z-index: 1;
      border-radius: 100px 0 0 0;
      -moz-border-radius: 100px 0 0 0;
      top: -50px;
      left: -50px;
      -moz-transform-origin: 100% 100%;
      cursor: pointer;
    }
    
    .sec1.sec {
      -moz-transform: rotate(0deg);
      z-index: 1;
      background-color: #cc1;
    }
    
    .sec2.sec {
      -moz-transform: rotate(60deg);
      z-index: 2;
      background-color: #cb2;
    }
    
    .sec3.sec {
      -moz-transform: rotate(120deg);
      z-index: 3;
      background-color: #ca3;
    }
    
    .sec4.sec {
      -moz-transform: rotate(180deg);
      z-index: 4;
      background-color: #c94;
    }
    
    .sec5.sec {
      -moz-transform: rotate(240deg);
      z-index: 5;
      background-color: #c85;
    }
    
    .sec6.sec {
      -moz-transform: rotate(300deg);
      z-index: 6;
      margin: 0px 0px 0px 50px;
      background-color: #c76;
      top: -74px;
    }
    
    .sec:hover {
      background-color: #06c;
    }
    <div id="container">
      <div class="sec1 sec"></div>
      <div class="sec2 sec"></div>
      <div class="sec3 sec"></div>
      <div class="sec4 sec"></div>
      <div class="sec5 sec"></div>
      <div class="outer_sec">
        <div class="sec6 sec"></div>
      </div>
      <div id="center"></div>
    </div>

    【讨论】:

    • 我已经更新了the fiddle。太糟糕了,:hover 仍然无法正常工作。
    • 我已经弄清楚了,问题在于它的 z-index。我又更新了fiddle
    • 可惜这个例子不允许在每个扇区内显示文本,严重限制了它的使用。
    • 它肯定允许文本。在每个扇区内放置一个div 并使用 css 设置位置。这个例子不使用文本,因为我们不知道问题想要什么,例如对齐方式、方向、大小等。
    • @raymondralibi,这是一个公平的声明。但考虑到另一个答案中有text(例如,允许 text-linkslabels),该栏已被提升到 更高的标准。可以肯定的是,我昨天确实对你投了赞成票。干杯!
    猜你喜欢
    • 2011-10-08
    • 2014-02-07
    • 2016-08-03
    • 2022-01-09
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多