CSS箭头填充渐变百分比

时间:2013-08-05 12:48:09

标签: css

Arrow

嗨,我正在摸着这个。我想创建一个类似于显示的CSS渐变箭头,并能够以百分比的形式定义红色部分的填充。红色块只是一种纯色。

从这个JFiddle示例中,我在创建绿色箭头方面取得了一些进展,但三角形上的渐变方向错误。我仍然不确定如何让红色块填满绿色箭头的百分比。例如,如果红色部分为90%,我将不得不以某种方式创建半个三角形。请帮助:)

绿色箭头的示例代码:

div.a3 {
   width: 100px;
   height: 50px;
   position: relative;
   margin: 50px;

   background: -webkit-linear-gradient(top, #99c739 0%,#4eb739 100%);
   background:    -moz-linear-gradient(top, #99c739 0%, #4eb739 100%); 
}

div.a3:after {
   z-index: -1;
   content: "";
   display: block;
   position: absolute;
   right: -75px;
   top: -50px;
   margin: 50px;
   height: 50px;
   width: 50px;

   -webkit-transform:rotate( -45deg );
   -moz-transform:rotate( -45deg );
        transform:rotate( -45deg );

   background: -webkit-linear-gradient(135deg, #99c739 0%, #4eb739 50%, #ffffff 50%, #ffffff 100%);
   background:    -moz-linear-gradient(135deg, #99c739 0%, #4eb739 50%, #ffffff 50%, #ffffff 100%);
}

2 个答案:

答案 0 :(得分:1)

div{
    width: 200px;
    height: 100px;
    margin: 50px 0;
    position:relative;
    background: #d72200;
    background: -moz-linear-gradient(left,  #d72200 0%, #d72200 20%, #9ac739 20%, #6bbe39 100%);
    background: -webkit-gradient(linear, left top, right top, color-stop(0%,#d72200), color-stop(20%,#d72200), color-stop(20%,#9ac739), color-stop(100%,#6bbe39));
    background: -webkit-linear-gradient(left,  #d72200 0%,#d72200 20%,#9ac739 20%,#6bbe39 100%);
    background: -o-linear-gradient(left,  #d72200 0%,#d72200 20%,#9ac739 20%,#6bbe39 100%);
    background: -ms-linear-gradient(left,  #d72200 0%,#d72200 20%,#9ac739 20%,#6bbe39 100%);
    background: linear-gradient(to right,  #d72200 0%,#d72200 20%,#9ac739 20%,#6bbe39 100%);
}
div:after{
    content:'';
    width: 100px;
    height: 100px;
    position: absolute;
    left: 100%;
    top: 0;
    background: #4eb739;
    background: -moz-linear-gradient(45deg,  #4eb739 0%, #6bbe39 50%, #6bbe39 100%);
    background: -webkit-gradient(linear, left bottom, right top, color-stop(0%,#4eb739), color-stop(50%,#6bbe39), color-stop(100%,#6bbe39));
    background: -webkit-linear-gradient(45deg,  #4eb739 0%,#6bbe39 50%,#6bbe39 100%);
    background: -o-linear-gradient(45deg,  #4eb739 0%,#6bbe39 50%,#6bbe39 100%);
    background: -ms-linear-gradient(45deg,  #4eb739 0%,#6bbe39 50%,#6bbe39 100%);
    background: linear-gradient(45deg,  #4eb739 0%,#6bbe39 50%,#6bbe39 100%);
    -moz-transform: scale(1) rotate(45deg) translate(0px, 0px) skew(0deg, 0deg);
    -webkit-transform: scale(1) rotate(45deg) translate(0px, 0px) skew(0deg, 0deg);
    -o-transform: scale(1) rotate(45deg) translate(0px, 0px) skew(0deg, 0deg);
    -ms-transform: scale(1) rotate(45deg) translate(0px, 0px) skew(0deg, 0deg);
    transform: scale(1) rotate(45deg) translate(0px, 0px) skew(0deg, 0deg);
    z-index: -2;
    margin: 0 0 0 -50px;
}
div:before{
    content:'';
    position:absolute;
    top: 0;
    right: 0;
    height: 100%;
    padding: 22px 0;
    z-index: -1;
    background: #fff;
    margin: -22px 0;
    width: 100%;
}

答案 1 :(得分:0)

代码下方似乎创建了一个箭头。

这是你在找什么?

    div.a3 {
    width: 100px;
    height: 50px;
    position: relative;
    margin: 50px;

    background: -webkit-linear-gradient(left, #1e5799 0%,#2989d8 100%);
    background:    -moz-linear-gradient(left, #1e5799 0%, #2989d8 100%); 
}

div.a3:after {
    z-index: -1;
    content: "";
    display: block;
    position: absolute;
    right: -75px;
    top: -50px;
    margin: 50px;
    height: 50px;
    width: 50px;
    background: #000;  
            transform:rotate( -45deg );

    background: -webkit-linear-gradient(135deg, #1e5799 0%, #2989d8 50%, #ffffff 50%, #ffffff 100%);
    background:    -moz-linear-gradient(135deg, #1e5799 0%, #2989d8 50%, #ffffff 50%, #ffffff 100%);
}