2017-08-04 132 views
-2

如何在一幀內收集上述圖像並做GIF?我可以使用CSS和PHP來做到這一點嗎?4x4 CSS將JPG文件轉換爲GIF

enter image description here

我想說明這些4x4的圖像作爲在短短的1幀的GIF。

+0

作爲'gif'具體而言,不是用CSS,而是作爲一次顯示一個圖像的動畫,而不是用jquery/css。如果所有合成圖像的尺寸相同/部件位於同一位置,則它只是一個帶有溢出隱藏的div,並將邊距設置爲偏移到每個圖像。 –

+0

你手裏有示例代碼嗎? – rootist

+0

我現在不能給你工作代碼,但基本上'

'然後js'$(function(){setTimeout(function(){$(「#wrapper」).css(「margin-left」,「 - 150px「);},2500);});'(改變寬度/高度/邊距以適應圖像的較小部分) –

回答

0

這是第一行的動畫。更改y軸將移動圖像以顯示第二行。 PHP或Javascript不是必需的。

.animation { 
 
    width: 317px; 
 
    height: 174px; 
 
    background: url('https://i.stack.imgur.com/xo7T3.jpg') 0 0; 
 
    animation: play 16s infinite; 
 
} 
 

 
@keyframes play { 
 
0% { background-position: 0 0; } 
 
5.99% { background-position: 0 0; } 
 
6% { background-position: -317px 0; } 
 
11.99% { background-position: -317px 0; } 
 
12% { background-position: -634px 0; } 
 
17.99% { background-position: -634px 0; } 
 
18% { background-position: -951px 0; } 
 
23.99% { background-position: -951px 0; } 
 
24% { background-position: -1268px 0; } 
 
99.99% { background-position: -1268px 0; } 
 
100% { background-position: 0 0; } 
 
}
<div class="animation"></div>

0

這是給你一個工作feedle由我。 在JavaScript上實現,使用jQuery庫。

https://jsfiddle.net/271qnqrj/

JS:

(function($) { 
    $.fn.extend({ 
     mygif: function(speed, width, height, verticalBorderWidth, horizontalBorderWidth) { 
      speed = speed || 400; 
      width = width || 315; 
      height = height || 175; 
      verticalBorderWidth = verticalBorderWidth || 5; 
      horizontalBorderWidth = horizontalBorderWidth || 5; 

      this.each(function() { 
       var img = $(this).attr('src'); 
       var div = $('<div class="mygif" data-frame="1" style="width: ' + width + 'px;height:' + height + 'px;background-image: url(\'' + img + '\');"/>').insertAfter($(this)); 
       $(this).remove(); 
      }); 

      window.setInterval(function() { 
       $('.mygif').each(function() { 
       var frame = $(this).data('frame'); 
       if (frame == 16) { 
        frame = 1; 
       } else { 
        frame++; 
       } 
       $(this).data('frame', frame); 

       var posX = (width + verticalBorderWidth) * ((frame-1) % 4); 
       var posY = (height + horizontalBorderWidth) * (Math.floor((frame-1)/4)); 
       $(this).css('background-position', -posX + 'px ' + -posY + 'px'); 
       }); 
      }, speed); 
     } 
    }); 

    $('.mygif').mygif() 
})(jQuery); 

HTML:

<img src="https://i.stack.imgur.com/xo7T3.jpg" class="mygif" /> 

CSS:

img.mygif { display: none; }