我有点新手,我知道我的代码不是最好的,但我正在学习。我将几个作为事件函数触发的图像序列动画放在一起。点击一个按钮,动画。将鼠标悬停在 div 上,动画。等等。
我认为我遇到的麻烦是动画被中断时。我在第二个 else if walk_div.removeChild(img);
然后图像序列开始基本上闪烁。有什么帮助吗?
function loadUp(){
var cyclenum = 175;
var folder = "animations/rollO/";
var imgType = '.png';
var count = 0;
var subCount = 1;
var initialImg = new Image();
initialImg.src = folder + '1.png';
var walk_div = document.getElementById('begin');
var img = new Image();
walk_div.appendChild(initialImg);
function animation(){
count++;
subCount = count - 1;
if(count == cyclenum){
clearInterval(walkAnimate);
}
else if(count == 1){
subCount = 2;
img.src = folder + subCount + imgType;
walk_div.removeChild(initialImg);
walk_div.appendChild(img);
count = 2;
}
else if (count < cyclenum){
img.src = folder + subCount + imgType;
walk_div.removeChild(img);
img.src = folder + count + imgType;
walk_div.appendChild(img);
}
}
var walkAnimate = setInterval(animation, 42);
}
function preAction(){
var cyclenum = 30;
var folder = "animations/pre/";
var imgType = '.png';
var count = 0;
var subCount = 1;
var initialImg = new Image();
initialImg.src = folder + '1.png';
//Clears out the walk div
var rmv = document.getElementById('begin');
if (rmv.hasChildNodes()){
while (rmv.childNodes.length >= 1)
{
rmv.removeChild(rmv.firstChild);
}
}
//end clear
var walk_div = document.getElementById('begin');
var img = new Image();
walk_div.appendChild(initialImg);
function animation(){
count++;
subCount = count - 1;
if(count == cyclenum){
clearInterval(walkAnimate);
}
else if(count == 1){
subCount = 2;
img.src = folder + subCount + imgType;
walk_div.removeChild(initialImg);
walk_div.appendChild(img);
count = 2;
}
else if (count < cyclenum){
img.src = folder + subCount + imgType;
walk_div.removeChild(img);
img.src = folder + count + imgType;
walk_div.appendChild(img);
}
}
var walkAnimate = setInterval(animation, 42);
}
function nextAction(){
var cyclenum = 30;
var folder = "animations/next/";
var imgType = '.png';
var count = 0;
var subCount = 1;
var initialImg = new Image();
initialImg.src = folder + '1.png';
//Clears out the walk div
var rmv = document.getElementById('begin');
if (rmv.hasChildNodes()){
while (rmv.childNodes.length >= 1)
{
rmv.removeChild(rmv.firstChild);
}
}
//end clear
var walk_div = document.getElementById('begin');
var img = new Image();
walk_div.appendChild(initialImg);
function animation(){
count++;
subCount = count - 1;
if(count == cyclenum){
clearInterval(walkAnimate);
}
else if(count == 1){
subCount = 2;
img.src = folder + subCount + imgType;
walk_div.removeChild(initialImg);
walk_div.appendChild(img);
count = 2;
}
else if (count < cyclenum){
img.src = folder + subCount + imgType;
walk_div.removeChild(img);
img.src = folder + count + imgType;
walk_div.appendChild(img);
}
}
var walkAnimate = setInterval(animation, 42);
}
function galleryRightIn(){
var cyclenum = 15;
var folder = "animations/galleryRIn/";
var imgType = '.png';
var count = 0;
var subCount = 1;
var initialImg = new Image();
initialImg.src = folder + '1.png';
//Clears out the walk div
var rmv = document.getElementById('begin');
if (rmv.hasChildNodes()){
while (rmv.childNodes.length >= 1)
{
rmv.removeChild(rmv.firstChild);
}
}
//end clear
var walk_div = document.getElementById('begin');
var img = new Image();
walk_div.appendChild(initialImg);
function animation(){
count++;
subCount = count - 1;
if(count == cyclenum){
clearInterval(walkAnimate);
}
else if(count == 1){
subCount = 2;
img.src = folder + subCount + imgType;
walk_div.removeChild(initialImg);
walk_div.appendChild(img);
count = 2;
}
else if (count < cyclenum){
img.src = folder + subCount + imgType;
walk_div.removeChild(img);
img.src = folder + count + imgType;
walk_div.appendChild(img);
}
}
var walkAnimate = setInterval(animation, 42);
}
function galleryRightOut(){
var cyclenum = 15;
var folder = "animations/galleryROut/";
var imgType = '.png';
var count = 0;
var subCount = 1;
var initialImg = new Image();
initialImg.src = folder + '1.png';
//Clears out the walk div
var rmv = document.getElementById('begin');
if (rmv.hasChildNodes()){
while (rmv.childNodes.length >= 1)
{
rmv.removeChild(rmv.firstChild);
}
}
//end clear
var walk_div = document.getElementById('begin');
var img = new Image();
walk_div.appendChild(initialImg);
function animation(){
count++;
subCount = count - 1;
if(count == cyclenum){
clearInterval(walkAnimate);
}
else if(count == 1){
subCount = 2;
img.src = folder + subCount + imgType;
walk_div.removeChild(initialImg);
walk_div.appendChild(img);
count = 2;
}
else if (count < cyclenum){
img.src = folder + subCount + imgType;
walk_div.removeChild(img);
img.src = folder + count + imgType;
walk_div.appendChild(img);
}
}
var walkAnimate = setInterval(animation, 42);
}
function galleryBackIn(){
var cyclenum = 15;
var folder = "animations/galleryBIn/";
var imgType = '.png';
var count = 0;
var subCount = 1;
var initialImg = new Image();
initialImg.src = folder + '1.png';
//Clears out the walk div
var rmv = document.getElementById('begin');
if (rmv.hasChildNodes()){
while (rmv.childNodes.length >= 1)
{
rmv.removeChild(rmv.firstChild);
}
}
//end clear
var walk_div = document.getElementById('begin');
var img = new Image();
walk_div.appendChild(initialImg);
function animation(){
count++;
subCount = count - 1;
if(count == cyclenum){
clearInterval(walkAnimate);
}
else if(count == 1){
subCount = 2;
img.src = folder + subCount + imgType;
walk_div.removeChild(initialImg);
walk_div.appendChild(img);
count = 2;
}
else if (count < cyclenum){
img.src = folder + subCount + imgType;
walk_div.removeChild(img);
img.src = folder + count + imgType;
walk_div.appendChild(img);
}
}
var walkAnimate = setInterval(animation, 42);
}
function galleryBackOut(){
var cyclenum = 15;
var folder = "animations/galleryBOut/";
var imgType = '.png';
var count = 0;
var subCount = 1;
var initialImg = new Image();
initialImg.src = folder + '1.png';
//Clears out the walk div
var rmv = document.getElementById('begin');
if (rmv.hasChildNodes()){
while (rmv.childNodes.length >= 1)
{
rmv.removeChild(rmv.firstChild);
}
}
//end clear
var walk_div = document.getElementById('begin');
var img = new Image();
walk_div.appendChild(initialImg);
function animation(){
count++;
subCount = count - 1;
if(count == cyclenum){
clearInterval(walkAnimate);
}
else if(count == 1){
subCount = 2;
img.src = folder + subCount + imgType;
walk_div.removeChild(initialImg);
walk_div.appendChild(img);
count = 2;
}
else if (count < cyclenum){
img.src = folder + subCount + imgType;
walk_div.removeChild(img);
img.src = folder + count + imgType;
walk_div.appendChild(img);
}
}
var walkAnimate = setInterval(animation, 42);
}
编辑:首先,感谢您的回复!
使用 setAnimationFrame 的部分问题是它在 IE 中不可靠。我也有最多 175 帧的图像序列,所以映射一张巨大的图像听起来像是很多工作 - 但是,我至少现在知道我什至可以做到这一点。可能对其他项目很方便。
这些动画有一个非常具体的开始和停止。当用户与网页交互时,卡通会指向不同的元素。我最终从 div 中删除了 firstChild。我意识到我正在调用一个特定的图像,所以当动画被中断时它不存在时,这就是我得到 DOM 8 的原因。它现在工作得更好了。
我现在需要做的是在另一个动画开始时停止动画。我发现你不能用 Javascript 真正做到这一点。我可以清除间隔,但它们需要是全局的,而且我还没有弄清楚如何做到这一点。我唯一的另一个想法是在单独的 div 中运行它们并根据活动功能隐藏或显示。然而,这意味着动画继续运行......只是在后台。除了开场动画外,它们只有 30 帧,而且我将图像的文件大小制作得非常小,但要继续运行就需要很多。我会用它作为最后的手段,但我更愿意找到更好的方法。
编辑编辑:
我最终对图像使用了相同的做法 - 添加和删除动画运行的 div。我知道这可能不是最优雅的方法,但直到事情变得更标准(咳嗽IE咳嗽)它会必须要做。它也适用于所有主流浏览器,甚至支持 IE7。
感谢大家抽出宝贵的时间!