【问题标题】:How can I show and hide sibling element when I mouse over in SVG?当我在 SVG 中将鼠标悬停时,如何显示和隐藏同级元素?
【发布时间】:2021-05-15 01:12:38
【问题描述】:

我使用了 SVG 文件,它是带有两个分割的圆形,当我将鼠标悬停在选定区域上时,我想显示和隐藏文本。当我鼠标悬停时我确实显示了文本,但是当我隐藏它时它仍然不起作用。我曾尝试只使用 CSS,但它不起作用。

我想要的是先显示#g0(text),然后当我将鼠标悬停在每个#g1(area) 和#g2 上时显示#text-r-1(text) 和#text-r-2(text) (区域),并使#g0(文本)隐藏。当我将每个#g1(area) 和#g2(area) 鼠标移出时,再次显示#g0(text)。

很抱歉解释得很好,但我真的需要你的帮助! :o


    <svg id="circle" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1142.28 1142.28">
        <g id="g0">
        <text id="text-0" x="50%" y="50%" text-anchor="middle" dy=".3em" font-size="50px" > I want to hide this </text>   
        </g>

        <g id="g1">
        <text id="text-r-1" x="50%" y="50%" text-anchor="middle" stroke="#51c5cf" stroke-width="0px" dy=".3em" font-size="50px"> AAA </text>
        <path id="r-1" d="M747.32,313.17,778.87,361A366.07,366.07,0,0,1,988,295.77V238.46a423.07,423.07,0,0,0-240.68,74.71Z" transform="translate(-416.92 -92.49)" fill="none" stroke="#000" stroke-miterlimit="10"/>
        </g>

        <g id="g2">
        <text id="text-r-2" x="50%" y="50%" text-anchor="middle" stroke="#51c5cf" stroke-width="0px" dy=".3em" font-size="50px"> BBB  </text>
        <path id="r-2" d="M988.06,295.77c203.16,0,367.86,164.7,367.86,367.86s-164.7,367.85-367.86,367.85S620.2,866.79,620.2,663.63C620.2,538.18,683,427.4,778.87,361l-31.55-47.84c-111.39,76.66-184.42,205-184.42,350.46,0,234.81,190.35,425.16,425.16,425.16s425.16-190.35,425.16-425.16S1222.87,238.46,988.06,238.46H988v57.31Z" transform="translate(-416.92 -92.49)" fill="none" stroke="#000" stroke-miterlimit="10"/>
        </g> 
----------------
svg{
  width:550px;
}

path{
  stroke-width:0.5px;
}

svg text{
  font-size:30px; 
}

path#r-1{
  fill: #FF313F;
  transition: all 0.5s;
}
path#r-2{
  fill: #EBA7A7;
  transition: all 0.5s;
}

svg #text-r-1 {display: none;}
svg #g1:hover #text-r-1 {display: block;}
svg #g1:hover path#r-1 {fill:#383838;}

svg #text-r-2 {display: none;}
svg #g2:hover #text-r-2 {display: block;}
svg #g2:hover path#r-2 {fill:#383838;}

.hhh {visibility:hidden;}

$(document).ready(function() {
    $("#g1").hover(function() {
        $(this).siblings("#g0").addClass("hhh");
    }, function() {
        $(this).siblings("#g0").removeClass("hhh");
    });
});
    $(document).ready(function() {
        $("#g2").hover(function() {
            $(this).siblings("#g0").addClass("hhh");
        }, function() {
            $(this).siblings("#g0").removeClass("hhh");
        });
});

【问题讨论】:

  • 有 ID 时为什么要使用 siblings()?
  • 老实说,我不知道为什么要使用它。我对javascript一无所知,但是在看到上面的堆栈溢出中的javascript后,我尝试使用它:o

标签: java html css svg siblings


【解决方案1】:

首先,你使用 jQuery 语法而不在你的项目中绑定它——所以它不能工作。

其次,您的 JavaScript 不需要两个 $( document ).ready(),只需使用一次即可。

第三,您可以在一个选择器中同时选择#g1 和#g2。

这是一个快速的工作方式:

$(function() {
  $("#g1, #g2").hover(function() {
    $("#g0").addClass("hhh");
  }, function() {
    $("#g0").removeClass("hhh");
  });
});
svg {
  width: 550px;
}

path {
  stroke-width: 0.5px;
}

svg text {
  font-size: 30px;
}

path#r-1 {
  fill: #FF313F;
  transition: all 0.5s;
}

path#r-2 {
  fill: #EBA7A7;
  transition: all 0.5s;
}

svg #text-r-1 {
  display: none;
}

svg #g1:hover #text-r-1 {
  display: block;
}

svg #g1:hover path#r-1 {
  fill: #383838;
}

svg #text-r-2 {
  display: none;
}

svg #g2:hover #text-r-2 {
  display: block;
}

svg #g2:hover path#r-2 {
  fill: #383838;
}

.hhh {
  visibility: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<svg id="circle" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1142.28 1142.28">
            <g id="g0">
            <text id="text-0" x="50%" y="50%" text-anchor="middle" dy=".3em" font-size="50px" > I want to hide this </text>   
            </g>

            <g id="g1">
            <text id="text-r-1" x="50%" y="50%" text-anchor="middle" stroke="#51c5cf" stroke-width="0px" dy=".3em" font-size="50px"> AAA </text>
            <path id="r-1" d="M747.32,313.17,778.87,361A366.07,366.07,0,0,1,988,295.77V238.46a423.07,423.07,0,0,0-240.68,74.71Z" transform="translate(-416.92 -92.49)" fill="none" stroke="#000" stroke-miterlimit="10"/>
            </g>

            <g id="g2">
            <text id="text-r-2" x="50%" y="50%" text-anchor="middle" stroke="#51c5cf" stroke-width="0px" dy=".3em" font-size="50px"> BBB  </text>
            <path id="r-2" d="M988.06,295.77c203.16,0,367.86,164.7,367.86,367.86s-164.7,367.85-367.86,367.85S620.2,866.79,620.2,663.63C620.2,538.18,683,427.4,778.87,361l-31.55-47.84c-111.39,76.66-184.42,205-184.42,350.46,0,234.81,190.35,425.16,425.16,425.16s425.16-190.35,425.16-425.16S1222.87,238.46,988.06,238.46H988v57.31Z" transform="translate(-416.92 -92.49)" fill="none" stroke="#000" stroke-miterlimit="10"/>
            </g>
</svg>

【讨论】:

  • 非常感谢!!这就是我想要的:)
【解决方案2】:

您只能使用 CSS 来实现:您将所有文本的填充不透明度设置为 0。当您将鼠标悬停在组上时,您会将内部文本的填充不透明度更改为 1。

您可以添加过渡以获得更漂亮的效果。

text{fill-opacity:0}
g{pointer-events:all;}
g:hover text{fill-opacity:1}
<svg id="circle" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1142.28 1142.28">
        <g id="g0">
        <text id="text-0" x="50%" y="50%" text-anchor="middle" dy=".3em" font-size="50px" > I want to hide this </text>   
        </g>

        <g id="g1">
        <text id="text-r-1" x="50%" y="50%" text-anchor="middle" stroke="#51c5cf" stroke-width="0px" dy=".3em" font-size="50px"> AAA </text>
        <path id="r-1" d="M747.32,313.17,778.87,361A366.07,366.07,0,0,1,988,295.77V238.46a423.07,423.07,0,0,0-240.68,74.71Z" transform="translate(-416.92 -92.49)" fill="none" stroke="#000" stroke-miterlimit="10"/>
        </g>

        <g id="g2">
        <text id="text-r-2" x="50%" y="50%" text-anchor="middle" stroke="#51c5cf" stroke-width="0px" dy=".3em" font-size="50px"> BBB  </text>
        <path id="r-2" d="M988.06,295.77c203.16,0,367.86,164.7,367.86,367.86s-164.7,367.85-367.86,367.85S620.2,866.79,620.2,663.63C620.2,538.18,683,427.4,778.87,361l-31.55-47.84c-111.39,76.66-184.42,205-184.42,350.46,0,234.81,190.35,425.16,425.16,425.16s425.16-190.35,425.16-425.16S1222.87,238.46,988.06,238.46H988v57.31Z" transform="translate(-416.92 -92.49)" fill="none" stroke="#000" stroke-miterlimit="10"/>
        </g> 
</svg>

更新

OP 正在评论:

鼠标移出 g1 和 g2 时如何显示“g0”?

一种可能的解决方案是将g0 移动到svg 元素的末尾并使用~ 选择器(Subsequent-sibling Combinator):g:hover ~ #g0 text{fill-opacity:1}

text{fill-opacity:0}
g{pointer-events:all;}
g:hover text{fill-opacity:1}

g:hover ~ #g0 text{fill-opacity:1}
<svg id="circle" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1142.28 1142.28">


        <g id="g1">
        <text id="text-r-1" x="50%" y="50%" text-anchor="middle" stroke="#51c5cf" stroke-width="0px" dy="1.3em" font-size="50px"> AAA </text>
        <path id="r-1" d="M747.32,313.17,778.87,361A366.07,366.07,0,0,1,988,295.77V238.46a423.07,423.07,0,0,0-240.68,74.71Z" transform="translate(-416.92 -92.49)" fill="none" stroke="#000" stroke-miterlimit="10"/>
        </g>

        <g id="g2">
        <text id="text-r-2" x="50%" y="50%" text-anchor="middle" stroke="#51c5cf" stroke-width="0px" dy="1.3em" font-size="50px"> BBB  </text>
        <path id="r-2" d="M988.06,295.77c203.16,0,367.86,164.7,367.86,367.86s-164.7,367.85-367.86,367.85S620.2,866.79,620.2,663.63C620.2,538.18,683,427.4,778.87,361l-31.55-47.84c-111.39,76.66-184.42,205-184.42,350.46,0,234.81,190.35,425.16,425.16,425.16s425.16-190.35,425.16-425.16S1222.87,238.46,988.06,238.46H988v57.31Z" transform="translate(-416.92 -92.49)" fill="none" stroke="#000" stroke-miterlimit="10"/>
        </g> 
  
  
        <g id="g0">
        <text id="text-0" x="50%" y="50%" text-anchor="middle" dy=".3em" font-size="50px" > I want to hide this </text>   
        </g>
</svg>

【讨论】:

  • 谢谢!但是,当我将 g1 和 g2 鼠标移出时,如何显示“g0”?因为如果我按照你教的那样使用 css,g0 将被隐藏。
【解决方案3】:

$( "#g1").hover(function() {
  $(this).prevAll().slice(0, 1).addClass('hhh');
}, function() {
        $(this).prevAll().slice(0, 1).removeClass("hhh");
    });
    
$("#g2").hover(function() {
  $(this).prev().prevAll().slice(0, 2).addClass('hhh');
}, function() {
        $(this).prevAll().slice(0, 2).removeClass("hhh");
    });
svg {
  width: 550px;
}

path {
  stroke-width: 0.5px;
}

svg text {
  font-size: 30px;
}

path#r-1 {
  fill: #FF313F;
  transition: all 0.5s;
}

path#r-2 {
  fill: #EBA7A7;
  transition: all 0.5s;
}

svg #text-r-1 {
  display: none;
}

svg #g1:hover #text-r-1 {
  display: block;
}

svg #g1:hover path#r-1 {
  fill: #383838;
}

svg #text-r-2 {
  display: none;
}

svg #g2:hover #text-r-2 {
  display: block;
}

svg #g2:hover path#r-2 {
  fill: #383838;
}

.hhh {
  visibility: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<svg id="circle" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1142.28 1142.28">
            <g id="g0">
            <text id="text-0" x="50%" y="50%" text-anchor="middle" dy=".3em" font-size="50px" > I want to hide this </text>   
            </g>

            <g id="g1">
            <text id="text-r-1" x="50%" y="50%" text-anchor="middle" stroke="#51c5cf" stroke-width="0px" dy=".3em" font-size="50px"> AAA </text>
            <path id="r-1" d="M747.32,313.17,778.87,361A366.07,366.07,0,0,1,988,295.77V238.46a423.07,423.07,0,0,0-240.68,74.71Z" transform="translate(-416.92 -92.49)" fill="none" stroke="#000" stroke-miterlimit="10"/>
            </g>

            <g id="g2">
            <text id="text-r-2" x="50%" y="50%" text-anchor="middle" stroke="#51c5cf" stroke-width="0px" dy=".3em" font-size="50px"> BBB  </text>
            <path id="r-2" d="M988.06,295.77c203.16,0,367.86,164.7,367.86,367.86s-164.7,367.85-367.86,367.85S620.2,866.79,620.2,663.63C620.2,538.18,683,427.4,778.87,361l-31.55-47.84c-111.39,76.66-184.42,205-184.42,350.46,0,234.81,190.35,425.16,425.16,425.16s425.16-190.35,425.16-425.16S1222.87,238.46,988.06,238.46H988v57.31Z" transform="translate(-416.92 -92.49)" fill="none" stroke="#000" stroke-miterlimit="10"/>
            </g>
</svg>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    相关资源
    最近更新 更多