Javascript div的removeChild中出现DOM 8错误
我是个新手,我知道我的代码不是最好的,但我正在学习。我把几个图像序列动画放在一起,作为事件的函数触发。单击按钮,单击动画。将鼠标悬停在div、动画上。等等 我想我遇到的麻烦是动画被打断了。我在第二个else if walk\u div.removeChild(img)中得到“Uncaught Error:NOT\u FOUND\u ERR:DOM Exception 8” 然后,图像序列基本上开始闪烁。有什么帮助吗Javascript div的removeChild中出现DOM 8错误,javascript,dom,appendchild,removechild,Javascript,Dom,Appendchild,Removechild,我是个新手,我知道我的代码不是最好的,但我正在学习。我把几个图像序列动画放在一起,作为事件的函数触发。单击按钮,单击动画。将鼠标悬停在div、动画上。等等 我想我遇到的麻烦是动画被打断了。我在第二个else if walk\u div.removeChild(img)中得到“Uncaught Error:NOT\u FOUND\u ERR:DOM Exception 8” 然后,图像序列基本上开始闪烁。有什么帮助吗 function loadUp(){ var cyclenum = 175; v
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);
}
函数加载(){
var-cyclenum=175;
var folder=“animations/rollO/”;
var imgType='.png';
var计数=0;
var子计数=1;
var initialImg=新图像();
initialImg.src=文件夹+1.png';
var walk_div=document.getElementById('begin');
var img=新图像();
步行区儿童(初始值);
函数动画(){
计数++;
子计数=计数-1;
if(count==cyclenum){
clearInterval(walkAnimate);
}
否则如果(计数=1){
次计数=2;
img.src=文件夹+子计数+imgType;
步行区搬迁儿童(初始值);
步行儿童组(img);
计数=2;
}
else if(计数<循环枚举){
img.src=文件夹+子计数+imgType;
徒步儿童组(img);
img.src=文件夹+计数+imgType;
步行儿童组(img);
}
}
var walkAnimate=setInterval(动画,42);
}
函数preAction(){
var-cyclenum=30;
var folder=“animations/pre/”;
var imgType='.png';
var计数=0;
var子计数=1;
var initialImg=新图像();
initialImg.src=文件夹+1.png';
//清理步行舱
var rmv=document.getElementById('begin');
if(rmv.hasChildNodes()){
while(rmv.childNodes.length>=1)
{
rmv.removeChild(rmv.firstChild);
}
}
//了结
var walk_div=document.getElementById('begin');
var img=新图像();
步行区儿童(初始值);
函数动画(){
计数++;
子计数=计数-1;
if(count==cyclenum){
clearInterval(walkAnimate);
}
否则如果(计数=1){
次计数=2;
img.src=文件夹+子计数+imgType;
步行区搬迁儿童(初始值);
步行儿童组(img);
计数=2;
}
else if(计数<循环枚举){
img.src=文件夹+子计数+imgType;
徒步儿童组(img);
img.src=文件夹+计数+imgType;
步行儿童组(img);
}
}
var walkAnimate=setInterval(动画,42);
}
函数nextAction(){
var-cyclenum=30;
var folder=“animations/next/”;
var imgType='.png';
var计数=0;
var子计数=1;
var initialImg=新图像();
initialImg.src=文件夹+1.png';
//清理步行舱
var rmv=document.getElementById('begin');
if(rmv.hasChildNodes()){
while(rmv.childNodes.length>=1)
{
rmv.removeChild(rmv.firstChild);
}
}
//了结
var walk_div=document.getElementById('begin');
var img=新图像();
步行区儿童(初始值);
函数动画(){
计数++;
子计数=计数-1;
if(count==cyclenum){
clearInterval(walkAnimate);
}
否则如果(计数=1){
次计数=2;
img.src=文件夹+子计数+imgType;
步行区搬迁儿童(初始值);
步行儿童组(img);
计数=2;
}
else if(计数<循环枚举){
img.src=文件夹+子计数+imgType;
徒步儿童组(img);
img.src=文件夹+计数+imgType;
步行儿童组(img);
}
}
var walkAnimate=setInterval(动画,42);
}
函数galleryRightIn(){
var-cyclenum=15;
var folder=“animations/galleryRIn/”;
var imgType='.png';
var计数=0;
var子计数=1;
var initialImg=新图像();
initialImg.src=文件夹+1.png';
//清理步行舱
var rmv=document.getElementById('begin');
if(rmv.hasChildNodes()){
while(rmv.childNodes.length>=1)
{
rmv.removeChild(rmv.firstChild);
}
}
//了结
var walk_div=document.getElementById('begin');
var img=新图像();
步行区儿童(初始值);
函数动画(){
计数++;
子计数=计数-1;
if(count==cyclenum){
clearInterval(walkAnimate);
}
否则如果(计数=1){
次计数=2;
img.src=文件夹+子计数+imgType;
步行区搬迁儿童(初始值);
步行儿童组(img);
计数=2;
}
else if(计数<循环枚举){
img.src=文件夹+子计数+imgType;
徒步儿童组(img);
img.src=文件夹+计数+imgType;
步行儿童组(img);
}
}
var walkAnimate=setInterval(动画,42);
}
函数galleryRightOut(){
var-cyclenum=15;
var folder=“animations/galleryROut/”;
var imgType='.png';
var计数=0;
var子计数=1;
var initialImg=新图像();
initialImg.src=文件夹+1.png';
//清理步行舱
var rmv=document.getElementById('begin');
if(rmv.hasChildNodes()){
while(rmv.childNodes.length>=1)
{
rmv.removeChild(rmv.firstChild);
}
}
//了结
var walk_div=document.getElementById('begin');
var img=新图像();
步行区儿童(初始值);
函数动画(){
计数++;
子计数=计数-1;
if(count==cyclenum){
clearInterval(walkAnimate);
}
否则如果(计数=1){
次计数=2;
img.src=文件夹+子计数+imgType;
步行区搬迁儿童(初始值);
步行儿童组(img);
计数=2;
}
else if(计数<循环枚举){
img.src=文件夹+子计数+imgType;
徒步儿童组(img);
img.src=文件夹+计数+imgType
// Wait for the window load event and start the animation.
window.addEventListener('load', init, false);
var frame = 0; // Current frame
var frames = 8; // Frame count
var frameWidth = 64; // Frame dimensions
var frameHeight = 64;
var element; // DOM element to animate
function init() {
// Fetch the dom element.
element = document.getElementById('anim');
// Begin the animation. Ideally you would use requestAnimationFrame but I'm
// using setInterval for simplicity.
setInterval(animate, 120);
}
function animate() {
// Increment the frame number.
frame = (frame + 1) % frames;
// Set the element's backgroundPosition to "0px -#px" according to the frame
// number and size.
element.style.backgroundPosition = '0px -' + (frame * frameHeight) + 'px';
// You use negative dimensions here because it's an offset of the background
// image, and moving the image up requires negative dimensions, since
// positive on the Y-axis is down.
}