javascript - CSS spritesheet 动画协助

标签 javascript css

我有一个 Sprite 表,宽度为 5760 像素,高度为 5400 像素,包含 10 行和 6 列,每列宽度为 960 像素,高度为 540 像素。 Sprite 表中的所有 Sprite 都是从左到右、从上到下排列的。我想使用 CSS 将 60 个图像作为动画播放,但我无法理解需要什么代码。有人有什么建议吗?

最佳答案

最简单的解决方案是将背景位置更改按顺序与 css 类关联。

即。

.class1{background-position:0,0} // top left
.class2{background-position:-960px, 0} // indent by 960
// go to .class6 minus 960 to first px value
.class7{background-position:0,540px} // got to left of 2nd row 
.class8{background-position:-960px,-540px} // got to left of 2nd row
// keep going for 10 rows, minus 540 per row and 960 per col 

然后在 javascript 中(使用 jQuery)

var viewarea = documentGetElementByID('my_window'); // id of DIV
var counter = 0; 
var speed = 300; // miliseconds between frame change

function loopAnimation(){
  // remove old frame
  if($(viewarea).hasClass("class"+counter){
     $(viewarea).removeClass("class"+counter);
  {
  // change counter
  if(counter == 60 ){
     counter = ;
  }else {
  counter++;
  }   
  // move to next frame
  $(viewarea).addClass("class"+counter);
  // set animation to do another frame
  var t = setTimeout(function(){loopAnimation();}, speed);
}
// begin animation
loopAnimation();

关于javascript - CSS spritesheet 动画协助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40647797/

相关文章:

javascript - 将 Javascript 的 setInterval 函数设置为可变数量——可能吗?

css - 图片灯箱旁边的文字

html - 为什么我的链接彼此重叠?

css - 有序列表 CSS 样式包括父编号

javascript - 使用 node.js 提取所有超链接(来自外部网站)并请求

javascript - highcharts范围线问题

javascript - jQuery $(this).data() 正在返回旧值

javascript - 查找并清除 toast (Toastr)

jquery - 如何在屏幕顶部显示通知?

html - Bootstrap 和 Leaflet 集成