【问题标题】:Add SVG tooltip to stroke on <circle />将 SVG 工具提示添加到 <circle /> 上的描边
【发布时间】:2018-01-20 05:56:15
【问题描述】:

我正在使用 svg 创建一个圆环图,并且我希望在圆环环悬停时提供工具提示。我正在制作这样的甜甜圈:

.container {
    display: flex;
    flex-flow: row wrap;
}

.card {
    width: 20em;
    height: 20em;
    padding: 2em;
    background-color: white;
    margin: 2em;
    box-shadow: 0 0 5px #222;
}

.pie-center {
    background: transparent;
    border-radius: 50%;
    transform: rotate(-90deg);
}

.circle1 {
    fill: transparent;
    stroke: teal;
    stroke-width: 7;
    stroke-dasharray: 30 70;    
}

.circle2 {
    fill: transparent;
    stroke: orangered;
    stroke-width: 7;
    stroke-dasharray: 45 55;
    stroke-dashoffset: -30;
    
}

.circle3 {
    fill: transparent;
    stroke: orchid;
    stroke-width: 7;
    stroke-dasharray: 20 80;
    stroke-dashoffset: -75;
}

.circle4 {
    fill: transparent;
    stroke: yellowgreen;
    stroke-width: 7;
    stroke-dasharray: 5 95;
    stroke-dashoffset: -95;
}
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <div class="container">
            <div class="card">
                <svg class="pie-center" viewBox="0 0 32 32">
                    <circle class="circle1" r="15.915494309" cx="16" cy="16" />
                    <circle class="circle2" r="15.915494309" cx="16" cy="16" />
                    <circle class="circle3" r="15.915494309" cx="16" cy="16" />
                    <circle class="circle4" r="15.915494309" cx="16" cy="16" />
                </svg>
            </div>
        </div>
    </body>
</html>

我知道我可以使用&lt;set /&gt; 标签来捕获鼠标事件,并且我可以使用它们来创建工具提示。问题是甜甜圈环的每个部分实际上都是一个圆圈,而圆圈上的stroke 属性是我真正想要为其捕获悬停事件的部分。

因此,当我尝试向我的圈子添加悬停动作时,我没有得到想要的结果。

这是我尝试过的(只是在悬停时将甜甜圈部分变为红色以模拟捕获事件以添加工具提示):

.container {
    display: flex;
    flex-flow: row wrap;
}

.card {
    width: 20em;
    height: 20em;
    padding: 2em;
    background-color: white;
    margin: 2em;
    box-shadow: 0 0 5px #222;
}

.pie-center {
    background: transparent;
    border-radius: 50%;
    transform: rotate(-90deg);
}

.circle1 {
    fill: transparent;
    stroke: teal;
    stroke-width: 7;
    stroke-dasharray: 30 70;
}

.circle2 {
    fill: transparent;
    stroke: orangered;
    stroke-width: 7;
    stroke-dasharray: 45 55;
    animation: dash3 1s ease 0s 1 forwards;
    stroke-dashoffset: -30;
}

.circle3 {
    fill: transparent;
    stroke: orchid;
    stroke-width: 7;
    stroke-dasharray: 20 80;
    animation: dash2 1s ease 0s 1 forwards;
    stroke-dashoffset: -75;
}

.circle4 {
    fill: transparent;
    stroke: yellowgreen;
    stroke-width: 7;
    stroke-dasharray: 5 95;
    animation: dash 1s ease 0s 1 forwards;
    stroke-dashoffset: -95;
}
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <div class="container">
            <div class="card new">
                <svg class="pie-center" viewBox="0 0 32 32">
                    <circle class="circle1" r="15.915494309" cx="16" cy="16" >
                        <set attributeName='stroke' from='teal' to='red' begin='mouseover' end='mouseout' />
                    </circle>
                    <circle class="circle2" r="15.915494309" cx="16" cy="16" >
                        <set attributeName='stroke' from='orangered' to='red' begin='mouseover' end='mouseout' />
                    </circle>
                    <circle class="circle3" r="15.915494309" cx="16" cy="16" >
                        <set attributeName='stroke' from='orchid' to='red' begin='mouseover' end='mouseout' />
                    </circle>
                    <circle class="circle4" r="15.915494309" cx="16" cy="16" >
                        <set attributeName='stroke' from='yellowgreen' to='red' begin='mouseover' end='mouseout' />
                    </circle>
                </svg>
            </div>
    </body>
</html>

我的问题是:有什么方法可以捕捉圆圈笔划上的悬停事件?或者是否有另一种方法来创建圆环图,例如使用 &lt;path /&gt; 或其他一些可以更好地支持悬停事件的 svg 元素?

如果可能,我不想使用第三方库(没有 D3 或 chart.js)。

【问题讨论】:

    标签: svg


    【解决方案1】:

    使用 fill: none 而不是 fill: transparent 以便填充不会反应。事实上,真的没有充分的理由使用 fill: transparent ever。

    .container {
        display: flex;
        flex-flow: row wrap;
    }
    
    .card {
        width: 20em;
        height: 20em;
        padding: 2em;
        background-color: white;
        margin: 2em;
        box-shadow: 0 0 5px #222;
    }
    
    .pie-center {
        background: none;
        border-radius: 50%;
        transform: rotate(-90deg);
    }
    
    .circle1 {
        fill: none;
        stroke: teal;
        stroke-width: 7;
        stroke-dasharray: 30 70;
    }
    
    .circle2 {
        fill: none;
        stroke: orangered;
        stroke-width: 7;
        stroke-dasharray: 45 55;
        animation: dash3 1s ease 0s 1 forwards;
        stroke-dashoffset: -30;
    }
    
    .circle3 {
        fill: none;
        stroke: orchid;
        stroke-width: 7;
        stroke-dasharray: 20 80;
        animation: dash2 1s ease 0s 1 forwards;
        stroke-dashoffset: -75;
    }
    
    .circle4 {
        fill: none;
        stroke: yellowgreen;
        stroke-width: 7;
        stroke-dasharray: 5 95;
        animation: dash 1s ease 0s 1 forwards;
        stroke-dashoffset: -95;
    }
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="style.css">
        </head>
        <body>
            <div class="container">
                <div class="card new">
                    <svg class="pie-center" viewBox="0 0 32 32">
                        <circle class="circle1" r="15.915494309" cx="16" cy="16" >
                            <set attributeName='stroke' from='teal' to='red' begin='mouseover' end='mouseout' />
                        </circle>
                        <circle class="circle2" r="15.915494309" cx="16" cy="16" >
                            <set attributeName='stroke' from='orangered' to='red' begin='mouseover' end='mouseout' />
                        </circle>
                        <circle class="circle3" r="15.915494309" cx="16" cy="16" >
                            <set attributeName='stroke' from='orchid' to='red' begin='mouseover' end='mouseout' />
                        </circle>
                        <circle class="circle4" r="15.915494309" cx="16" cy="16" >
                            <set attributeName='stroke' from='yellowgreen' to='red' begin='mouseover' end='mouseout' />
                        </circle>
                    </svg>
                </div>
        </body>
    </html>

    【讨论】:

    • 非常感谢!!我正在尝试使用路径重新创建它(这更困难)。我永远不会知道使用fill: none 而不是fill: transparent。谢谢先生。
    • 我遇到了同样的问题,只是读了这个。谢谢罗伯特朗森,这真的很有帮助。
    猜你喜欢
    • 2015-03-22
    • 2017-08-03
    • 1970-01-01
    • 2015-09-24
    • 2019-03-03
    • 2012-11-02
    • 2019-03-08
    • 2021-12-13
    相关资源
    最近更新 更多