带有HTML5的Java脚本,使用图片服务Webp

时间:2019-03-14 01:03:19

标签: javascript html5 image

 <div class="col-md-6 col-lg-4">
      <div class="work-box">
          <div class="work-img" id="Starcraft">
          </div>
          <div class="work-content">
            <div class="row">
              <div class="col-sm-8">
                <h2 class="w-title">Starcraft Animation</h2>
                <div class="w-more">
                   <a href="https://github.com/ArundeepChohan/Coursework/tree/master/2130/Assignment2" target="_blank"><span class="w-ctegory">Code</span> </a>
                </div>
              </div>
              <div class="col-sm-4">
                <div class="w-like">
                  <a href="img/StarcraftAnimation.png" data- 
                     lightbox="gallery-mf"> <span class="ion-ios-plus-outline"></span></a>
                  <a href="img/StarcraftAnimation2.png" data- 
                     lightbox="gallery-mf"> <span class="ion-ios-plus-outline"></span></a>
                </div>
              </div>
            </div>
          </div>
      </div>
    </div>

    <script>
    var myIndex = 0;

    function carousel() {   
    if(myIndex==0)
        document.getElementById('Starcraft').innerHTML = ('<picture><source srcset="img/StarcraftAnimation.webp" type="image/webp" class="img-fluid"><img src="img/StarcraftAnimation.png" alt="StarcraftAnimation" class="img-fluid" ></picture>');
    else
    {
        document.getElementById('Starcraft').innerHTML = ('<picture><source srcset="img/StarcraftAnimation2.webp" type="image/webp" class="img-fluid"><img src="img/StarcraftAnimation2.png" alt="StarcraftAnimation" class="img-fluid" ></picture>');
    }
    myIndex++;
    if (myIndex > 1) {myIndex = 0}    
        setTimeout(carousel, 5000); // Change image every 5 seconds
    }
    carousel();
    </script>

我需要提供一些webp服务,并在两张图片之间切换,但后退到png。它可以工作,但是我想知道是否有比重做innerhtml和仅切换srcset更好的方法。也不想闪烁。

1 个答案:

答案 0 :(得分:0)

尝试imagekit.io,它可以自动提供正确的图像格式

免责声明:我是ImageKit.io的联合创始人