【问题标题】:How can I use fade effect like a css transition to a image tag in SVG?如何使用淡入淡出效果,如 CSS 过渡到 SVG 中的图像标签?
【发布时间】:2019-08-21 20:21:13
【问题描述】:

我有一个被多边形裁剪的 svg 图像。 我总共有 5 张图片,我让 JS 每 3 秒更改一次图片。

看起来还可以,但是图像切换太快太突然,所以我想在它们切换时对它们使用一些淡入淡出效果。

谁能帮我弄清楚如何实现这一目标? 到目前为止,我了解到 css 转换不能用于 svg 属性。

<?xml version="1.0" encoding="utf-8"?>
<svg x="0px" y="0px" viewBox="0 0 300 300"
   style="position: absolute; top: 0; left: 0;" xml:space="preserve">
   <clipPath id="clip01">
      <polygon class="st0 line01"
         points="212.1,0.7 212.1,175.5 434.9,85.2" />
   </clipPath>
   <image xlink:href="img/bg_01.jpg" x="-100" y="0" width="1000"
      height="500" style="clip-path: url(#clip01);" opacity="1" />
</svg>


<script>
$(function () {
var i = 1;
function changeBG() {
document.querySelector("image").setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', 'img/bg_0' + i + '.jpg');
i++;
}
setInterval(changeBG, 2000);
});
</script>

【问题讨论】:

    标签: image animation svg


    【解决方案1】:

    我会这样做:在 Javascript 中,在我的示例中每 3 秒更改一次不透明度,而在 CSS 中,您有 transition:opacity 1s;

    let theopacity = 1;
    setInterval(function(){ 
    theopacity = Math.abs(theopacity - 1) ;
    img.style.opacity = theopacity;
    }, 3000);
    svg{border:1px solid;}
    image{
    opacity:1;
    transition:opacity 1s;
    }
    <svg x="0px" y="0px" viewBox="170 0 300 175" style="position: absolute; top: 0; left: 0;" xml:space="preserve">
       <clipPath id="clip01">
          <polygon class="st0 line01"
             points="212.1,0.7 212.1,175.5 434.9,85.2"/>
       </clipPath>
       <g style="clip-path: url(#clip01);">
       <image xlink:href="https://www.rocketgardens.co.uk/wp-content/uploads/2017/10/edible-flower-planter-1000x500.jpg" x="-100" y="0" width="1000" height="500"  opacity="1">
       </image>
       <image id="img" xlink:href="https://images.discerningassets.com/image/upload/c_fit,h_1000,w_1000/c_fit,fl_relative,h_1.0,l_deco_watermark,o_40,w_1.0/v1522547348/Lilac-Blooms-Lavender_crpsoc.jpg" x="-100" y="0" width="1000" height="500"  opacity="1">
       </image>
       </g>  
    </svg>

    【讨论】:

    • 非常感谢,enxaneta。我实际上尝试了一种我发现自己的不同方式并且它有效,但是你上面建议的方式也很完美。谢谢。
    • @gwen 最好为答案加分
    【解决方案2】:

    我就是这样做的。这个想法有点类似于 enxaneta 的。

    <?xml version="1.0" encoding="utf-8"?>
        <svg id="svg02" class="svg02" x="0px" y="0px" viewBox="0 0 575.2 481.5" style="position: absolute; top: 0; left: 0;" xml:space="preserve">
    
        <clipPath id="clip01">
            <polygon class="st0 line01" points="212.1,0.7 212.1,175.5 434.9,85.2    " />
    
        </clipPath>
        <image class="image_01" xlink:href="img/bg_01.jpg" x="-200" y="0" width="1000" height="500" style="clip-path: url(#clip01);" />
        <image class="image_02" xlink:href="img/bg_01.jpg" x="-200" y="0" width="1000" height="500" style="clip-path: url(#clip01);" />
    </svg>
    
    <script>
    var imgChange = function() {
    var i = 1;
    
    function changeBackground() {
        $(".image_02")
            .hide()
            .fadeIn();
    
        if (i > 5) {
            i = 1;
        }
    
        document
            .querySelector(".image_01")
            .setAttributeNS(
                "http://www.w3.org/1999/xlink",
                "xlink:href",
                "img/bg_0" + (i - 1) + ".jpg"
            );
    
        document
            .querySelector(".image_02")
            .setAttributeNS(
                "http://www.w3.org/1999/xlink",
                "xlink:href",
                "img/bg_0" + i + ".jpg"
            );
    
        i++;
    }
    setInterval(changeBackground, 3000);
    };
    
    imgChange();
    
    </script>
    

    【讨论】:

      【解决方案3】:

      添加&lt;animate&gt; 标签:

      <animate attributeType="CSS" attributeName="opacity" from="1" to="0" dur="5s" repeatCount="indefinite"/>
      

      <?xml version="1.0" encoding="utf-8"?>
      <svg x="0px" y="0px" viewBox="0 0 300 300" style="position: absolute; top: 0; left: 0;" xml:space="preserve">
         <clipPath id="clip01">
            <polygon class="st0 line01"
               points="212.1,0.7 212.1,175.5 434.9,85.2"/>
         </clipPath>
         <image xlink:href="img/bg_01.jpg" x="-100" y="0" width="1000" height="500" style="clip-path: url(#clip01);" opacity="1"/>
         
         <animate attributeType="CSS" attributeName="opacity" from="1" to="0" dur="5s" repeatCount="indefinite"/>
         
      </svg>

      【讨论】:

      • 谢谢你的想法,zer00ne。我没有意识到 svg 有一个动画标签。效果很好。
      猜你喜欢
      • 2012-07-19
      • 2011-06-18
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      相关资源
      最近更新 更多