代码之家  ›  专栏  ›  技术社区  ›  Smokey Dawson

svg lineargradient hidden如果svg隐藏在单独的类中

  •  0
  • Smokey Dawson  · 技术社区  · 5 年前

    基本上,如果svg隐藏在parallel元素中,则会隐藏lineargradient填充。

    .mobile {
      display: none;
    }
    
    .content-svg {
      border: 1px solid black;
    }
    <div class="desktop">
      <!-- stuff here -->
    </div>
    
    <div class="mobile">
      <svg class="mobile-svg" height="150" width="400">
      <defs>
        <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
          <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
          <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
        </linearGradient>
      </defs>
      <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
      Sorry, your browser does not support inline SVG.
      </svg>
    </div>
    
    <div class="content">
      <svg class="content-svg" height="150" width="400">
      <defs>
        <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
          <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
          <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
        </linearGradient>
      </defs>
      <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
      Sorry, your browser does not support inline SVG.
      </svg>
    </div>

    正如您在上面的代码片段中看到的,svg元素在那里,lineargradient填充被隐藏

    如果我在css中将填充改为纯色集,它会按预期工作,这似乎只在渐变填充时发生。

    现在这只是我整个问题的一个基本示例,我想避免重新创建svg,因为我在vue中使用它并创建了一个可重用的svg组件

    1 回复  |  直到 5 年前
        1
  •  1
  •   Temani Afif    5 年前

    请考虑不同的渐变ID:

    .mobile {
      display: none;
    }
    
    .content-svg {
      border: 1px solid black;
    }
    <div class="desktop">
      <!-- stuff here -->
    </div>
    
    <div class="mobile">
      <svg class="mobile-svg" height="150" width="400">
      <defs>
        <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
          <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
          <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
        </linearGradient>
      </defs>
      <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
      Sorry, your browser does not support inline SVG.
      </svg>
    </div>
    
    <div class="content">
      <svg class="content-svg" height="150" width="400">
      <defs>
        <linearGradient id="grad2" x1="0%" y1="0%" x2="100%" y2="0%">
          <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
          <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
        </linearGradient>
      </defs>
      <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad2)" />
      Sorry, your browser does not support inline SVG.
      </svg>
    </div>

    或者,只有在两个SVG中使用的渐变色相同时才考虑使用渐变色(相关: Gradients hidden using SVG symbols )

    .mobile{
    显示:无;
    }
    
    .content svg{
    边框:1px纯黑;
    }
    <div class="desktop">
      <!-- stuff here -->
    </div>
    
      <svg  height="0" width="0">
      <defs>
        <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
          <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
          <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
        </linearGradient>
      </defs>
      </svg>
    <div class="mobile">
      <svg class="mobile-svg" height="150" width="400">
      <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
      Sorry, your browser does not support inline SVG.
      </svg>
    </div>
    
    <div class="content">
      <svg class="content-svg" height="150" width="400">
      <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
      Sorry, your browser does not support inline SVG.
      </svg>
    </div>

    重复相同的id是无效的,只有第一个id将被同时考虑用于两个渐变,因为第一个id具有 display:none 它不会渲染。

    更改顺序将使您的代码工作,因为第一个将不再具有 显示:无

    .mobile{
    显示:无;
    }
    
    .content svg{
    边框:1px纯黑;
    }
    <div class="desktop">
      <!-- stuff here -->
    </div>
    <div class="content">
      <svg class="content-svg" height="150" width="400">
      <defs>
        <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
          <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
          <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
        </linearGradient>
      </defs>
      <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
      Sorry, your browser does not support inline SVG.
      </svg>
    </div>
    <div class="mobile">
      <svg class="mobile-svg" height="150" width="400">
      <defs>
        <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
          <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
          <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
        </linearGradient>
      </defs>
      <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
      Sorry, your browser does not support inline SVG.
      </svg>
    </div>