两种颜色的SVG/CSS笔划虚线 - 是否可能?

And*_*Mao 15 css svg colors line

是否可以使用CSS来定义具有两个交替颜色的线条(或形状边缘)?也就是说,如果1和2是不同颜色的像素,那么

1212121212121212或112211221122

我基本上想要一些方法来使用两种颜色的stroke-dasharray.线条本身是完全着色的.

如果这不可能,那么接近它的好方法是什么?例如,我可以创建一个交替的两种颜色的重复线性渐变,但这很难从javascript设置两种颜色.

met*_*ion 26

仅在一个元素的SVG中这是不可能的,但您可以使用两个不同的rects然后应用stroke-dashoffset: x...

<svg xmlns="http://www.w3.org/2000/svg">
    <rect class="stroke-red" x="10" y="10" width="101" height="101" />
    <rect class="stroke-green" x="10" y="10" width="101" height="101" />
</svg>


rect.stroke-red {
  stroke: red;
  fill: none;
  stroke-width: 5;
}

rect.stroke-green {
  stroke: green;
  fill: none;
  stroke-dasharray: 5,5; 
  stroke-width: 5;
}
Run Code Online (Sandbox Code Playgroud)

演示http://jsfiddle.net/aMCsY/

  • 你的回答很好,但我会指出我更喜欢上面评论中的实现 - 虚线矩形后面的实线矩形 - 不容易出错。如果 CSS 稍微偏离或者浏览器在不同的地方开始了破折号,似乎 `stroke-dashoffset` 可能会导致看起来很有趣的矩形。 (2认同)

Col*_*ung 13

基于@duopixel的答案,您可以使用该stroke-dasharray属性构建具有多种颜色的相当复杂的模式:

.L4 {
    stroke: #000;
    stroke-dasharray: 20,10,5,5,5,10;
}
.L5 {
    stroke: #AAA;
    stroke-dasharray: 0,20,10,15,10,0
}
.L6 {
    stroke: #DDD;
    stroke-dasharray: 0,35,5,15
}
Run Code Online (Sandbox Code Playgroud)

请参阅http://jsfiddle.net/colin_young/Y38u9/演示具有复合虚线图案的线条和圆圈.

更新了SO片段:

svg {
    width: 100%;
    height: 160px;
}
path, circle {
    stroke-width: 4;
}
text {
    alignment-baseline: central;
    font-family: sans-serif;
    font-size: 10px;
    stroke-width: 0;
    fill: #000;
    text-anchor: middle;
}
.dim {
    stroke: #AAA;
    stroke-width: 1;
    stroke-dasharray: 1, 1;
}
circle.dim {
    fill: #FFF;
}
.L4 {
    stroke: #000;
    stroke-dasharray: 20, 10, 5, 5, 5, 10;
}
.L5 {
    stroke: #AAA;
    stroke-dasharray: 0, 20, 10, 15, 10, 0
}
.L6 {
    stroke: #DDD;
    stroke-dasharray: 0, 35, 5, 15
}
Run Code Online (Sandbox Code Playgroud)
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    <g fill="none" stroke="black">
        <path class="dim" d="M5 20 l0 80" />
        <path class="dim" d="M25 20 l0 80 l-10 20" />
        <path class="dim" d="M35 20 l0 80 l-10 30" />
        <path class="dim" d="M40 20 l0 120" />
        <path class="dim" d="M45 20 l0 80 l10 30" />
        <path class="dim" d="M50 20 l0 80 l10 20" />
        <path class="dim" d="M60 20 l0 80 l15 10" />

        <text x="5" y="110">0</text>
        <text x="5" y="125">20</text>
        <text x="25" y="135">30</text>
        <text x="40" y="150">35</text>
        <text x="55" y="140">40</text>
        <text x="65" y="125">45</text>
        <text x="82" y="115">55</text>

        <path class="L4" d="M5 20 l215 0" />
        <path class="L5" d="M5 20 l215 0" />
        <path class="L6" d="M5 20 l215 0" />

        <!-- separated to show composition -->
        <text x="5" y="70" style="text-anchor:start">Separated to show composition:</text>
        <path class="L4" d="M5 80 l215 0" />
        <path class="L5" d="M5 90 l215 0" />
        <path class="L6" d="M5 100 l215 0" />

        <circle class="L4" cx="400" cy="80" r="60" />
        <circle class="L5" cx="400" cy="80" r="60" />
        <circle class="L6" cx="400" cy="80" r="60" />
    </g>
</svg>
Run Code Online (Sandbox Code Playgroud)

  • 非常好的例子。 (2认同)