动态改变所有
的样式标签
我试图通过更改 div 中 img 标签的位置来模拟晃动。
我让它一次只处理一张图片。
有没有办法一次性改变div中所有img标签的样式?
这就是我当前正在做的事情:
bird = document.createElement('img');
bird.setAttribute('src',birdie);
bird.setAttribute('class', 'birdie');
bird.setAttribute('id', id);
bird.setAttribute('onLoad', 'shakeAll()');
map.appendChild(bird);
birds++;
if(birdmap[0] == 0){
birdmap = [id];
}else{
birdmap+=[,id];
}
这个 ShakeAll
函数也在 body 的 onLoad
处:
function shakeAll(){
if (birdmap[0] == 0) return;
i = 1;
while(i <= birds){
shakeIt(birdmap[i]);
i++;
}
setTimeout("shakeAll()",initialSpeed);
}
注意:imgs 是绝对的
function shakeIt(id){
shake = document.getElementById(id);
j=1;
while(j<4){
if (j==1){
shake.style.top=parseInt(shake.style.top)+distance+"px";
}
else if (j==2){
shake.style.left=parseInt(shake.style.left)+distance+"px";
}
else if (j==3){
shake.style.top=parseInt(shake.style.top)-distance+"px";
}
else{
shake.style.left=parseInt(shake.style.left)-distance+"px";
}
j++;
}
//setTimeout("shakeIt(id)", 50);
}
I am trying to simulate shaking by changing the position of img tags within a div.
I have it working for one img at a time.
Is there a way to change the style of all the img tags within a div at once?
This is what I am currently doing:
bird = document.createElement('img');
bird.setAttribute('src',birdie);
bird.setAttribute('class', 'birdie');
bird.setAttribute('id', id);
bird.setAttribute('onLoad', 'shakeAll()');
map.appendChild(bird);
birds++;
if(birdmap[0] == 0){
birdmap = [id];
}else{
birdmap+=[,id];
}
this ShakeAll
function is also at onLoad
of body:
function shakeAll(){
if (birdmap[0] == 0) return;
i = 1;
while(i <= birds){
shakeIt(birdmap[i]);
i++;
}
setTimeout("shakeAll()",initialSpeed);
}
Note: the imgs are absolute
function shakeIt(id){
shake = document.getElementById(id);
j=1;
while(j<4){
if (j==1){
shake.style.top=parseInt(shake.style.top)+distance+"px";
}
else if (j==2){
shake.style.left=parseInt(shake.style.left)+distance+"px";
}
else if (j==3){
shake.style.top=parseInt(shake.style.top)-distance+"px";
}
else{
shake.style.left=parseInt(shake.style.left)-distance+"px";
}
j++;
}
//setTimeout("shakeIt(id)", 50);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我真的无法告诉你你想在你的代码中做什么。下面是一些代码,显示了来回移动某些图像的基础知识:
此处显示工作演示和 HTML: http:// jsfiddle.net/jfriend00/ED5yA/
而且,这是一个有趣的动画,它为动画添加了一些随机抖动(抖动):http://jsfiddle.net/jfriend00/jM8jx/。
获取要操作的图像列表的基础是这部分:
获取容器对象,然后获取该容器中的所有图像对象。你可以在jsFiddle中看到相应的HTML。该代码实现了一种定位方案,该方案在边缘处减慢速度,并在范围中间速度最快。运动的其余部分由
shakeAll()
中声明的变量的初始值控制。这些可以被编辑或更改以传递到函数中。I couldn't really tell what you were trying to do in your code. Here's some code that shows the basics of moving some images back and forth:
Working demo and HTML shown here: http://jsfiddle.net/jfriend00/ED5yA/
And, here's a fun one that adds some random shakiness (jitter) to the animation: http://jsfiddle.net/jfriend00/jM8jx/.
The basics of obtaining the list of images to operate on is this part:
This gets the container object and then gets all image objects in that container. You can see the corresponding HTML in the jsFiddle. This code implements a positioning scheme that slows the velocity down at the edges and goes the fastest in the middle of the range. The rest of the motion is controlled by the intial value of the variables declared in
shakeAll()
. These can be edited or can be changed to be passed into the function.为您想要摇动的所有图像指定一个通用的类名称。然后,用户 getElementsByClassName() 而不是 getElementById() 返回一个数组具有特定类名的元素。然后使用循环来为每个动画设置动画。
但如果您希望所有图像都具有动画效果,请使用 element.getElementsByTagName() 或document.getElementsByTagName() 反而。
give a common class name to all imgs that you wanna shake. and then, user getElementsByClassName() instead of getElementById() to return an array of the elements which have the specific class name. then use a loop to animate each.
but if you want ALL imgs to be animated, use element.getElementsByTagName() or document.getElementsByTagName() instead.