【问题标题】:SVG CSS3 Show and Hide element on hover from other elementSVG CSS3在从其他元素悬停时显示和隐藏元素
【发布时间】:2016-07-23 14:52:30
【问题描述】:

鉴于下面的 SVG,我正在尝试使用 CSS 允许将鼠标悬停在 g#Main_Layer 中的 tspan.hovertext 分类元素上,以取消隐藏 #Hover_Panels 中不相关的 g 元素。见下文:

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 20.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1280px"
     height="1569.2px" viewBox="0 0 1280 1569.2" style="enable-background:new 0 0 1280 1569.2;" xml:space="preserve">
<style type="text/css">
    text{font-family:'Lato';font-weight:300;}

    #Hover_Text {
        text-transform: uppercase;
    }

    .st0{fill:#404041;}
    .st1{font-family:'Lato';font-weight:300;}
    .st5{font-family:'Lato';font-weight:400;}
    .st6{font-size:20px;}
    .st7{fill:none;stroke:#5880AC;stroke-width:1.073;}
    .st22{display:none;}
    .st23{display:inline;fill:#016699;}
    .st24{font-size:26.44px;}

    .hovertext {
        cursor: pointer;
    }

    .hovertext:hover {
        font-weight: bold;
    }

    .hovertext:hover + #Hidden_Text {
        display: block;
    }

</style>
<g id="Main_Layer">
    <text id="Hover_Text" transform="matrix(1 0 0 1 1044.0001 878.011)">
        <tspan x="0" y="0" class="st4 st5 st6">hover </tspan>
        <tspan x="0" y="180" class="hovertext st4 st1 st6">HOVER TEXT</tspan>
    </text>
</g>
<g id="Hover_panels">
    <g id="Default_Text" >
        <text transform="matrix(1 0 0 1 459.2306 1051.639)">
            <tspan x="0" y="0" class="st16 st1 st19">Default Text </tspan>
        </text>
    </g>
    <g id="Hidden_Text" class="st22">
        <text transform="matrix(1 0 0 1 566.0988 1163.5396)" class="st23 st1 st24">Hidden Text</text>
    </g>
</g>
</svg>

问题是我不能完全控制 XML 布局(由我们的设计师提供的 Illustrator 生成),并且出于我希望我可以在这里讨论的原因,我们不能使用 Javascript 来实现这种效果。

当仅使用 CSS 时,如何在将鼠标悬停在 .hovertext 元素上时,将 display: block 添加到 #Hidden_Text 元素,并隐藏 #Default_Text 元素?

谢谢!

【问题讨论】:

  • 您无法仅使用 CSS 来实现这一点 - 您可以在悬停时更改子元素的状态,而不是父元素或兄弟元素。

标签: xml css svg sass


【解决方案1】:

单独使用 CSS 无法指定不直接位于(“+”运算符)或下方(某些子级)的元素。

但是您可以使用visibility: collapse 而不是display: none 代替#Hidden_Text(看起来一样),并在#Hidden_Text 中定义一个&lt;set&gt; 动画,如果文本是徘徊。您需要将隐藏文本包装在 &lt;tspan&gt; 中,并将 id 应用于 hovertext 以实现这一点:

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 20.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1280px"
     height="1569.2px" viewBox="0 0 1280 1569.2" style="enable-background:new 0 0 1280 1569.2;" xml:space="preserve">
<style type="text/css">
    text{font-family:'Lato';font-weight:300;}

    #Hover_Text {
        text-transform: uppercase;
    }

    .st0{fill:#404041;}
    .st1{font-family:'Lato';font-weight:300;}
    .st5{font-family:'Lato';font-weight:400;}
    .st6{font-size:20px;}
    .st7{fill:none;stroke:#5880AC;stroke-width:1.073;}
    .st22{visibility: collapse;}
    .st23{display:inline;fill:#016699;}
    .st24{font-size:26.44px;}

    .hovertext {
        cursor: pointer;
    }

    .hovertext:hover {
        font-weight: bold;
    }

    .hovertext:hover + #Hidden_Text {
        display: block;
    }

</style>
<g id="Main_Layer">
    <text id="Hover_Text" transform="matrix(1 0 0 1 1044.0001 878.011)">
        <tspan x="0" y="0" class="st4 st5 st6">hover </tspan>
        <tspan x="0" y="180" id="hovertext" class="hovertext st4 st1 st6">HOVER TEXT</tspan>
   </text>
</g>
<g id="Hover_panels">
    <g id="Default_Text" >
        <text transform="matrix(1 0 0 1 459.2306 1051.639)">
            <tspan x="0" y="0" class="st16 st1 st19">Default Text </tspan>
        </text>
        <g id="Hidden_Text" class="st22">
            <text transform="matrix(1 0 0 1 566.0988 1163.5396)" class="st23 st1 st24">
                <tspan>Hidden Text</tspan>
                <set attributeName="visibility" to="visible" begin="hovertext.mouseover" end="hovertext.mouseout"/>
            </text>
        </g>
    </g>
</g>
</svg>

我不太了解 Illustrator 允许您做什么,但至少它可以在没有 JavaScript 的情况下工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多