Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

html - SASS/CSS multi-color wheel

I have made a color wheel, and it is looking good except for the last element which is occupying more space and it has a larger width. How can I make it so all my span's share an equal width

demo

@for $i from 1 through 25 {
  span:nth-child(#{$i}) {
    border-top-color: hsl($i * 15, 50%, 50%);
    border-left-color: transparent;
    border-right-color: transparent;
    border-bottom-color: transparent;
  }
}

@for $i from 1 through 25 {
  span:nth-child(#{$i}) {
    transform: rotate($i * 14.4deg);
  }
}

#colorWheel {
  -webkit-animation: intro 3s ease;
  width: 100px;
  height: 100px;
  margin: 0 auto;
  position: relative;
  transform-origin: 50px 150px;
  transition: all 0.5s linear;
}

#colorWheel::before {
  content: "";
  width: 300px;
  height: 300px;
  overflow: hidden;
  position: absolute;
  top: -30px;
  left: -130px;
  border-radius: 100%;
  border: 30px solid #ffffff;
  z-index: 100;
}

#colorWheel span {
  position: absolute;
  -webkit-transform-origin: 50% 50%;
  border-style: solid;
  border-width: 150px 50px;
  box-sizing: border-box;
}
<div id="colorWheel">
    <span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span> <!-- 25 span's -->
  </div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is an optimization of your code based on this previous answer:

$num:25;

@for $i from 1 through $num {
  span:nth-child(#{$i}) {
    background-color: hsl($i * 15, 50%, 50%);
    transform: rotate($i * (360deg/$num));
  }
}

#colorWheel {
  height: 300px;
  width: 300px;
  position:relative;
  display:block;
  margin:auto;
}
#colorWheel > * {
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  border-radius:50%;
  clip-path:polygon(
    50% 50%, 
    50% 0%, 
    (50%*(1 + tan(360deg/$num)))  0%); 
}

Demo: https://jsfiddle.net/7patx6bn/

Compiled version:

span:nth-child(1) {
  background-color: #bf6040;
  transform: rotate(14.4deg);
}

span:nth-child(2) {
  background-color: #bf8040;
  transform: rotate(28.8deg);
}

span:nth-child(3) {
  background-color: #bf9f40;
  transform: rotate(43.2deg);
}

span:nth-child(4) {
  background-color: #bfbf40;
  transform: rotate(57.6deg);
}

span:nth-child(5) {
  background-color: #9fbf40;
  transform: rotate(72deg);
}

span:nth-child(6) {
  background-color: #80bf40;
  transform: rotate(86.4deg);
}

span:nth-child(7) {
  background-color: #60bf40;
  transform: rotate(100.8deg);
}

span:nth-child(8) {
  background-color: #40bf40;
  transform: rotate(115.2deg);
}

span:nth-child(9) {
  background-color: #40bf60;
  transform: rotate(129.6deg);
}

span:nth-child(10) {
  background-color: #40bf80;
  transform: rotate(144deg);
}

span:nth-child(11) {
  background-color: #40bf9f;
  transform: rotate(158.4deg);
}

span:nth-child(12) {
  background-color: #40bfbf;
  transform: rotate(172.8deg);
}

span:nth-child(13) {
  background-color: #409fbf;
  transform: rotate(187.2deg);
}

span:nth-child(14) {
  background-color: #4080bf;
  transform: rotate(201.6deg);
}

span:nth-child(15) {
  background-color: #4060bf;
  transform: rotate(216deg);
}

span:nth-child(16) {
  background-color: #4040bf;
  transform: rotate(230.4deg);
}

span:nth-child(17) {
  background-color: #6040bf;
  transform: rotate(244.8deg);
}

span:nth-child(18) {
  background-color: #8040bf;
  transform: rotate(259.2deg);
}

span:nth-child(19) {
  background-color: #9f40bf;
  transform: rotate(273.6deg);
}

span:nth-child(20) {
  background-color: #bf40bf;
  transform: rotate(288deg);
}

span:nth-child(21) {
  background-color: #bf409f;
  transform: rotate(302.4deg);
}

span:nth-child(22) {
  background-color: #bf4080;
  transform: rotate(316.8deg);
}

span:nth-child(23) {
  background-color: #bf4060;
  transform: rotate(331.2deg);
}

span:nth-child(24) {
  background-color: #bf4040;
  transform: rotate(345.6deg);
}

span:nth-child(25) {
  background-color: #bf6040;
  transform: rotate(360deg);
}

#colorWheel {
  height: 300px;
  width: 300px;
  position: relative;
  display: block;
  margin: auto;
}

#colorWheel > * {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border-radius: 50%;
  clip-path: polygon(50% 50%, 50% 0%, 63% 0%);
}
<div id="colorWheel">
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
  </div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...