CSS3饼图和旋转图像

时间:2017-08-29 12:30:27

标签: html css css3

我一直在寻找这个CSS3 Pie。 https://codepen.io/anon/pen/gxQarR

HTML

<div class="pie big" data-start="270" data-value="360"></div>
<div class="pie" data-start="0" data-value="90"></div>
<div class="pie" data-start="90" data-value="180">2</div>
<div class="pie" data-start="180" data-value="90">3</div>
<div class="pie" data-start="180" data-value="270">4</div>

CSS

/* 
  make each pie piece a rectangle twice as high as it is wide.
  move the transform origin to the middle of the left side.
  Also ensure that overflow is set to hidden.
*/
  .pie {
        position:absolute;
        width:400px;
        height:800px;
        overflow:hidden;
        left:600px;
        -moz-transform-origin:left center;
        -ms-transform-origin:left center;
        -o-transform-origin:left center;
        -webkit-transform-origin:left center;
        transform-origin:left center;
    }
/*
  unless the piece represents more than 50% of the whole chart.
  then make it a square, and ensure the transform origin is
  back in the center.

  NOTE: since this is only ever a single piece, you could
  move this to a piece specific rule and remove the extra class
*/
    .pie.big {
        width:800px;
        height:800px;
        left:50px;
        -moz-transform-origin:center center;
        -ms-transform-origin:center center;
        -o-transform-origin:center center;
        -webkit-transform-origin:center center;
        transform-origin:center center;
    }
/*
  this is the actual visible part of the pie. 
  Give it the same dimensions as the regular piece.
  Use border radius make it a half circle.
  move transform origin to the middle of the right side.
  Push it out to the left of the containing box.
*/
    .pie:BEFORE {
        content:"";
        position:absolute;
        width:400px;
        height:800px;
        left:-400px;
        border-radius:400px 0 0 400px;
        -moz-transform-origin:right center;
        -ms-transform-origin:right center;
        -o-transform-origin:right center;
        -webkit-transform-origin:right center;
        transform-origin:right center;

    }
 /* if it's part of a big piece, bring it back into the square */
    .pie.big:BEFORE {
        left:0px;
    }
/* 
  big pieces will also need a second semicircle, pointed in the
  opposite direction to hide the first part behind.
*/
    .pie.big:AFTER {
        content:"";
        position:absolute;
        width:200px;
        height:400px;
        left:200px;
        border-radius:0 100px 100px 0;
    }
/*
  add colour to each piece.
*/
    .pie:nth-of-type(1):BEFORE,
    .pie:nth-of-type(1):AFTER {
    background: url(http://takeawayshop.dk/image/frontpage/pizza-1471406_960_720.jpg) no-repeat;
    -ms-transform: rotate(360deg); /* IE 9 */
    -webkit-transform: rotate(360deg); /* Chrome, Safari, Opera */
    transform: rotate(360deg);
    }

    .pie:nth-of-type(2):AFTER,
    .pie:nth-of-type(2):BEFORE {
        background-color:green; 
    }
    .pie:nth-of-type(3):AFTER,
    .pie:nth-of-type(3):BEFORE {
        background-color:red;   
    }
    .pie:nth-of-type(4):AFTER,
    .pie:nth-of-type(4):BEFORE {
        background-color:orange;    
    }
/*
  now rotate each piece based on their cumulative starting
  position
*/
    .pie[data-start="0"] {
        -moz-transform: rotate(0deg); /* Firefox */
        -ms-transform: rotate(0deg); /* IE */
        -webkit-transform: rotate(0deg); /* Safari and Chrome */
        -o-transform: rotate(0deg); /* Opera */
        transform:rotate(0deg);
    }
    .pie[data-start="90"] {
        -moz-transform: rotate(90deg); /* Firefox */
        -ms-transform: rotate(90deg); /* IE */
        -webkit-transform: rotate(90deg); /* Safari and Chrome */
        -o-transform: rotate(90deg); /* Opera */
        transform:rotate(90deg);
    }
    .pie[data-start="180"] {
        -moz-transform: rotate(180deg); /* Firefox */
        -ms-transform: rotate(180deg); /* IE */
        -webkit-transform: rotate(180deg); /* Safari and Chrome */
        -o-transform: rotate(180deg); /* Opera */
        transform:rotate(180deg);
    }

    .pie[data-start="270"] {
        -moz-transform: rotate(270deg); /* Firefox */
        -ms-transform: rotate(270deg); /* IE */
        -webkit-transform: rotate(270deg); /* Safari and Chrome */
        -o-transform: rotate(270deg); /* Opera */
        transform:rotate(270deg);
    }

    .pie[data-start="360"] {
        -moz-transform: rotate(360deg); /* Firefox */
        -ms-transform: rotate(360deg); /* IE */
        -webkit-transform: rotate(360deg); /* Safari and Chrome */
        -o-transform: rotate(360deg); /* Opera */
        transform:rotate(360deg);
    }
/*
  and rotate the amount of the pie that's showing.

  NOTE: add an extra degree to all but the final piece, 
  to fill in unsightly gaps.
*/
    .pie[data-value="90"]:BEFORE {
        -moz-transform: rotate(90deg); /* Firefox */
        -ms-transform: rotate(90deg); /* IE */
        -webkit-transform: rotate(90deg); /* Safari and Chrome */
        -o-transform: rotate(90deg); /* Opera */
        transform:rotate(90deg);
    }
    .pie[data-value="180"]:BEFORE {
        -moz-transform: rotate(180deg); /* Firefox */
        -ms-transform: rotate(180deg); /* IE */
        -webkit-transform: rotate(180deg); /* Safari and Chrome */
        -o-transform: rotate(180deg); /* Opera */
        transform:rotate(180deg);
    }
    .pie[data-value="270"]:BEFORE {
        -moz-transform: rotate(270deg); /* Firefox */
        -ms-transform: rotate(270deg); /* IE */
        -webkit-transform: rotate(270deg); /* Safari and Chrome */
        -o-transform: rotate(270deg); /* Opera */
        transform:rotate(270deg);
    }

    .pie[data-value="360"]:BEFORE {
        -moz-transform: rotate(360deg); /* Firefox */
        -ms-transform: rotate(360deg); /* IE */
        -webkit-transform: rotate(360deg); /* Safari and Chrome */
        -o-transform: rotate(360deg); /* Opera */
        transform:rotate(360deg);
    }
/*
NOTE: you could also apply custom classes (i.e. .s0 .v30)
but if the CSS3 attr() function proposal ever gets implemented,
then all the above custom piece rules could be replaced with
the following:

.pie[data-start] {
   transform:rotate(attr(data-start,deg,0);
}
.pie[data-value]:BEFORE {
   transform:rotate(attr(data-value,deg,0);
}
*/

我正在考虑创建这个馅饼,每个90 * peice都有图片。 然而,我没有运气旋转图片,所以它看起来很合适。 我已经考虑过使用转换旋转,但没有运气。

我想过在Photoshop中旋转图片,所以它很适合。还有其他方法吗?

3 个答案:

答案 0 :(得分:1)

为什么不尝试这样的事情codepen.io

<div class="wrapper"> 
  <div class="box box-1"></div>
  <div class="box box-2"></div>
  <div class="box box-3"></div>
  <div class="box box-4"></div>
</div> 


<style>
    .wrapper {
      width:800px;
    }

    .box {

        float: left;
      display:inline-block;
      width: 400px;
      height:400px;
      background-color:#000;
    }

    .box  + .box + .box {
      margin-top:-4px;
    }

    .box-1 {
            background: url(http://takeawayshop.dk/image/frontpage/pizza-1471406_960_720.jpg) no-repeat;
      border-top-left-radius:100%;
    }
    .box-2 {
              background: url(http://media.rtl.fr/cache/kfnpEy__8SYEunR6sbHTZg/2000v1333-0/online/image/2017/0331/7787896201_le-masque-insecte-d-une-grenouille.jpg) no-repeat;
      border-top-right-radius:100%;
    }

    .box-3 {
            background: url(https://cdn.pixabay.com/photo/2016/06/18/17/42/image-1465348_960_720.jpg) no-repeat;
      border-bottom-left-radius:100%;
    }

    .box-4 {
              background: url(https://cdn.pixabay.com/photo/2017/04/05/11/56/image-in-the-image-2204798_960_720.jpg) no-repeat;
      border-bottom-right-radius:100%;
    }
</style>

答案 1 :(得分:1)

一个选项是在饼图中添加一个新元素,您可以独立于整个片段进行控制,并将背景图像放在该内部元素上。

<div class="pie" data-start="0" data-value="90">
  <div class="pie background"></div>
</div>
<div class="pie" data-start="90" data-value="180">2</div>
<div class="pie" data-start="180" data-value="90">3</div>
<div class="pie" data-start="180" data-value="270">4</div>

(请注意我如何删除你所拥有的额外饼图。这个例子没有必要。)

然后在你的CSS中,这个:

.pie:nth-of-type(1):BEFORE,
.pie:nth-of-type(1):AFTER {
  background: url(http://takeawayshop.dk/image/frontpage/pizza-1471406_960_720.jpg) no-repeat;
  -ms-transform: rotate(360deg); /* IE 9 */
  -webkit-transform: rotate(360deg); /* Chrome, Safari, Opera */
  transform: rotate(360deg);
}

变为:

.pie.background:BEFORE,
.pie.background:AFTER {
  background: url(http://takeawayshop.dk/image/frontpage/pizza-1471406_960_720.jpg) no-repeat;
  -ms-transform: rotate(360deg); /* IE 9 */
  -webkit-transform: rotate(360deg); /* Chrome, Safari, Opera */
  transform: rotate(360deg);
}

你可以添加:

.pie.background {
  left: 0;
}
.pie.background::before {
  left: 0;
  transform-origin: initial;
  border-radius: 0 400px 400px 0;
}

&#13;
&#13;
.pie {
  position: absolute;
  width: 400px;
  height: 800px;
  overflow: hidden;
  left: 600px;
  -moz-transform-origin: left center;
  -ms-transform-origin: left center;
  -o-transform-origin: left center;
  -webkit-transform-origin: left center;
  transform-origin: left center;
}
.pie.background {
  left: 0;
}
.pie.background::before {
  left: 0;
  transform-origin: initial;
  border-radius: 0 400px 400px 0;
}
.pie.background:BEFORE,
.pie.background:AFTER {
  background: url(http://takeawayshop.dk/image/frontpage/pizza-1471406_960_720.jpg) no-repeat;
  -ms-transform: rotate(360deg); /* IE 9 */
  -webkit-transform: rotate(360deg); /* Chrome, Safari, Opera */
  transform: rotate(360deg);
}
.pie:BEFORE {
  content: "";
  position: absolute;
  width: 400px;
  height: 800px;
  left: -400px;
  border-radius: 400px 0 0 400px;
  -moz-transform-origin: right center;
  -ms-transform-origin: right center;
  -o-transform-origin: right center;
  -webkit-transform-origin: right center;
  transform-origin: right center;
}
.pie:nth-of-type(2):AFTER,
.pie:nth-of-type(2):BEFORE {
  background-color: green;
}
.pie:nth-of-type(3):AFTER,
.pie:nth-of-type(3):BEFORE {
  background-color: red;
}
.pie:nth-of-type(4):AFTER,
.pie:nth-of-type(4):BEFORE {
  background-color: orange;
}
.pie[data-start="0"] {
  -moz-transform: rotate(0deg); /* Firefox */
  -ms-transform: rotate(0deg); /* IE */
  -webkit-transform: rotate(0deg); /* Safari and Chrome */
  -o-transform: rotate(0deg); /* Opera */
  transform: rotate(0deg);
}
.pie[data-start="90"] {
  -moz-transform: rotate(90deg); /* Firefox */
  -ms-transform: rotate(90deg); /* IE */
  -webkit-transform: rotate(90deg); /* Safari and Chrome */
  -o-transform: rotate(90deg); /* Opera */
  transform: rotate(90deg);
}
.pie[data-start="180"] {
  -moz-transform: rotate(180deg); /* Firefox */
  -ms-transform: rotate(180deg); /* IE */
  -webkit-transform: rotate(180deg); /* Safari and Chrome */
  -o-transform: rotate(180deg); /* Opera */
  transform: rotate(180deg);
}
.pie[data-start="270"] {
  -moz-transform: rotate(270deg); /* Firefox */
  -ms-transform: rotate(270deg); /* IE */
  -webkit-transform: rotate(270deg); /* Safari and Chrome */
  -o-transform: rotate(270deg); /* Opera */
  transform: rotate(270deg);
}
.pie[data-value="90"]:BEFORE {
  -moz-transform: rotate(90deg); /* Firefox */
  -ms-transform: rotate(90deg); /* IE */
  -webkit-transform: rotate(90deg); /* Safari and Chrome */
  -o-transform: rotate(90deg); /* Opera */
  transform: rotate(90deg);
}
.pie[data-value="180"]:BEFORE {
  -moz-transform: rotate(180deg); /* Firefox */
  -ms-transform: rotate(180deg); /* IE */
  -webkit-transform: rotate(180deg); /* Safari and Chrome */
  -o-transform: rotate(180deg); /* Opera */
  transform: rotate(180deg);
}
.pie[data-value="270"]:BEFORE {
  -moz-transform: rotate(270deg); /* Firefox */
  -ms-transform: rotate(270deg); /* IE */
  -webkit-transform: rotate(270deg); /* Safari and Chrome */
  -o-transform: rotate(270deg); /* Opera */
  transform: rotate(270deg);
}
&#13;
<div class="pie" data-start="0" data-value="90">
  <div class="pie background"></div>
</div>
<div class="pie" data-start="90" data-value="180">2</div>
<div class="pie" data-start="180" data-value="90">3</div>
<div class="pie" data-start="180" data-value="270">4</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

希望这会有所帮助

/* 
  make each pie piece a rectangle twice as high as it is wide.
  move the transform origin to the middle of the left side.
  Also ensure that overflow is set to hidden.
*/
  .pie {
        position:absolute;
        width:400px;
        height:800px;
        overflow:hidden;
        left:600px;
        -moz-transform-origin:left center;
        -ms-transform-origin:left center;
        -o-transform-origin:left center;
        -webkit-transform-origin:left center;
        transform-origin:left center;
    }
/*
  unless the piece represents more than 50% of the whole chart.
  then make it a square, and ensure the transform origin is
  back in the center.

  NOTE: since this is only ever a single piece, you could
  move this to a piece specific rule and remove the extra class
*/
    .pie.big {
        width:800px;
        height:800px;
        left:50px;
        -moz-transform-origin:center center;
        -ms-transform-origin:center center;
        -o-transform-origin:center center;
        -webkit-transform-origin:center center;
        transform-origin:center center;
    }
/*
  this is the actual visible part of the pie. 
  Give it the same dimensions as the regular piece.
  Use border radius make it a half circle.
  move transform origin to the middle of the right side.
  Push it out to the left of the containing box.
*/
    .pie:BEFORE {
        content:"";
        position:absolute;
        width:400px;
        height:800px;
        left:-400px;
        border-radius:400px 0 0 400px;
        -moz-transform-origin:right center;
        -ms-transform-origin:right center;
        -o-transform-origin:right center;
        -webkit-transform-origin:right center;
        transform-origin:right center;

    }
 /* if it's part of a big piece, bring it back into the square */
    .pie.big:BEFORE {
        left:0px;
    }
/* 
  big pieces will also need a second semicircle, pointed in the
  opposite direction to hide the first part behind.
*/
    .pie.big:AFTER {
        content:"";
        position:absolute;
        width:200px;
        height:400px;
        left:200px;
        border-radius:0 100px 100px 0;
    }
/*
  add colour to each piece.
*/
    .pie:nth-of-type(1):BEFORE,
    .pie:nth-of-type(1):AFTER {
    background: url(http://takeawayshop.dk/image/frontpage/pizza-1471406_960_720.jpg) no-repeat;
    -ms-transform: rotate(360deg); /* IE 9 */
    -webkit-transform: rotate(360deg); /* Chrome, Safari, Opera */
    transform: rotate(360deg);
    }

    .pie:nth-of-type(2):AFTER,
    .pie:nth-of-type(2):BEFORE {
        background-color:green; 
    }
    .pie:nth-of-type(3):AFTER,
    .pie:nth-of-type(3):BEFORE {
        background-color:red;   
    }
    .pie:nth-of-type(4):AFTER,
    .pie:nth-of-type(4):BEFORE {
        background-color:orange;    
    }
/*
  now rotate each piece based on their cumulative starting
  position
*/
    .pie[data-start="0"] {
        -moz-transform: rotate(0deg); /* Firefox */
        -ms-transform: rotate(0deg); /* IE */
        -webkit-transform: rotate(0deg); /* Safari and Chrome */
        -o-transform: rotate(0deg); /* Opera */
        transform:rotate(0deg);
    }
    .pie[data-start="90"] {
        -moz-transform: rotate(90deg); /* Firefox */
        -ms-transform: rotate(90deg); /* IE */
        -webkit-transform: rotate(90deg); /* Safari and Chrome */
        -o-transform: rotate(90deg); /* Opera */
        transform:rotate(90deg);
    }
    .pie[data-start="180"] {
        -moz-transform: rotate(180deg); /* Firefox */
        -ms-transform: rotate(180deg); /* IE */
        -webkit-transform: rotate(180deg); /* Safari and Chrome */
        -o-transform: rotate(180deg); /* Opera */
        transform:rotate(180deg);
    }

    .pie[data-start="270"] {
        -moz-transform: rotate(270deg); /* Firefox */
        -ms-transform: rotate(270deg); /* IE */
        -webkit-transform: rotate(270deg); /* Safari and Chrome */
        -o-transform: rotate(270deg); /* Opera */
        transform:rotate(270deg);
    }

    .pie[data-start="360"] {
        -moz-transform: rotate(360deg); /* Firefox */
        -ms-transform: rotate(360deg); /* IE */
        -webkit-transform: rotate(360deg); /* Safari and Chrome */
        -o-transform: rotate(360deg); /* Opera */
        transform:rotate(360deg);
    }
/*
  and rotate the amount of the pie that's showing.

  NOTE: add an extra degree to all but the final piece, 
  to fill in unsightly gaps.
*/
    .pie[data-value="90"]:BEFORE {
        -moz-transform: rotate(90deg); /* Firefox */
        -ms-transform: rotate(90deg); /* IE */
        -webkit-transform: rotate(90deg); /* Safari and Chrome */
        -o-transform: rotate(90deg); /* Opera */
        transform:rotate(90deg);
    }
    .pie[data-value="180"]:BEFORE {
        -moz-transform: rotate(180deg); /* Firefox */
        -ms-transform: rotate(180deg); /* IE */
        -webkit-transform: rotate(180deg); /* Safari and Chrome */
        -o-transform: rotate(180deg); /* Opera */
        transform:rotate(180deg);
    }
    .pie[data-value="270"]:BEFORE {
        -moz-transform: rotate(270deg); /* Firefox */
        -ms-transform: rotate(270deg); /* IE */
        -webkit-transform: rotate(270deg); /* Safari and Chrome */
        -o-transform: rotate(270deg); /* Opera */
        transform:rotate(270deg);
    }

    .pie[data-value="360"]:BEFORE {
        -moz-transform: rotate(360deg); /* Firefox */
        -ms-transform: rotate(360deg); /* IE */
        -webkit-transform: rotate(360deg); /* Safari and Chrome */
        -o-transform: rotate(360deg); /* Opera */
        transform:rotate(360deg);
    }
/*
NOTE: you could also apply custom classes (i.e. .s0 .v30)
but if the CSS3 attr() function proposal ever gets implemented,
then all the above custom piece rules could be replaced with
the following:

.pie[data-start] {
   transform:rotate(attr(data-start,deg,0);
}
.pie[data-value]:BEFORE {
   transform:rotate(attr(data-value,deg,0);
}
*/
.pie:nth-of-type(2):BEFORE, .pie:nth-of-type(2):AFTER {
    background: url(https://dummyimage.com/600x400/eb0707/fff) no-repeat;
    -ms-transform: rotate(90deg);
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg);
}
.pie:nth-of-type(3):BEFORE, .pie:nth-of-type(3):AFTER {
    background: url(https://dummyimage.com/600x400/e7fa14/fff) no-repeat;
    -ms-transform: rotate(90deg);
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg);
}
.pie:nth-of-type(4):BEFORE, .pie:nth-of-type(4):AFTER {
    background: url(https://dummyimage.com/600x400/9a05eb/fff) no-repeat;
    -ms-transform: rotate(90deg);
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg);
}
.pie[data-start="270"] {
    -moz-transform: rotate(270deg);
    -ms-transform: rotate(270deg);
    -webkit-transform: rotate(270deg);
    -o-transform: rotate(270deg);
    transform: rotate(270deg);
}
<div class="pie" data-start="0" data-value="90"></div>
<div class="pie" data-start="90" data-value="180">2</div>
<div class="pie" data-start="180" data-value="90">3</div>
<div class="pie" data-start="270" data-value="270">4</div>