代码之家  ›  专栏  ›  技术社区  ›  RTC222

用CSS或JavaScript中的媒体查询改变SVG文本的X-Y坐标?

  •  0
  • RTC222  · 技术社区  · 6 年前

    我的主页上嵌入了svg文本。有一个主标题和一个副标题(见下文)。我想更改媒体查询中使用tagline_类的子标题的x-y坐标。

    <div class="logo">
    
      <svg viewBox="0 0 240 220" xmlns="http://www.w3.org/2000/svg">
    
            <defs>
                <linearGradient id="MyGradient">
                <stop offset="5%"  stop-color="rgb(71,164,71)"/>
                <stop offset="95%" stop-color="white"/>
            </linearGradient>
        </defs>
    
        <text x="0" y="25" class="logo_class three">Main Head</text>
    
        <text id="subHead" fill="url(#MyGradient)" x="24" y="33" width="33" height="24" class="tagline_class fadeIn animate_behind">subHead</text>
    
        </svg>
    </div>

    我知道我可以用JavaScript来修改SVG元素,但是如果我能使用CSS就更容易了。

    在媒体查询中,我可以仅使用css更改副标题(标记线类)的x-y坐标吗?

    如果没有,我可以使用这个javascript:

    document.getElementById("jqx-chart-axis-text").setAttribute("x", "10");
    

    但我不清楚JavaScript是否应该从媒体查询或JavaScript代码中使用的媒体查询调用。我也不知道使用什么事件来更改svg坐标。

    综上所述,如何使用CSS或JavaScript中的媒体查询改变SVG文本的X-Y坐标。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Patrick Zamarian    6 年前

    您还可以在css中使用svg元素的id:

    <div class="logo">
    <style>
        @media(max-width: 500px){
            #subHead{
                transform: translateX(-20px);
            }
        }
        @media(min-width: 1000px){
            #subHead{
                transform: translateX(20px);
            }
        }
    </style>
    <svg viewBox="0 0 240 220" xmlns="http://www.w3.org/2000/svg">
            <defs>
                <linearGradient id="MyGradient">
                <stop offset="5%"  stop-color="rgb(71,164,71)"/>
                <stop offset="95%" stop-color="white"/>
            </linearGradient>
        </defs>
    
        <text x="0" y="25" class="logo_class three">Main Head</text>
    
        <text id="subHead" fill="url(#MyGradient)" x="24" y="33" width="33" height="24" class="tagline_class fadeIn animate_behind">subHead</text>
    
        </svg>
    </div>