如何在JavaScript中针对一个div中的特定图像进行动画效果?

huangapple go评论74阅读模式
英文:

How to target specific images within a div for animation in JavaScript?

问题

JS

var index = 0;

function changeBanner() {
  var imagesInDiv = document.querySelectorAll('.auto img.mySlides');
  [].forEach.call(imagesInDiv, function(v, i) {
    imagesInDiv[i].hidden = i !== index;
  });
  index = (index + 1) % imagesInDiv.length;
}

window.onload = function() {
  setInterval(changeBanner, 1000);
};

我想修改JavaScript代码,以便只有在<div class="auto">内的图像才会参与动画。我该如何实现这个目标?

英文:

I have a webpage with multiple images inside a specific <div> element, and I want to create a JavaScript function that will animate these images. However, I'm having trouble targeting only the images within the div and excluding others on the page.

HTML

&lt;div class=&quot;auto&quot; style=&quot;max-width: 1050px&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;/images/revital sub logo.png&quot; style=&quot;width:550px&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;/images/design.png&quot; style=&quot;width:550px&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;/images/Design4.png&quot; style=&quot;width:550px&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;/images/design2.png&quot; style=&quot;width:550px&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;/images/design6.png&quot; style=&quot;width:550px&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;/images/design5.png&quot; style=&quot;width:550px&quot;&gt;
&lt;/div&gt;

JS

 var index = 0;

 function changeBanner() {
   [].forEach.call(document.images, function(v, i) {
   document.images[i].hidden = i !== index;
 });
index = (index + 1) % document.images.length;
 }

 window.onload = function() {
   setInterval(changeBanner, 1000);
 };

I want to modify the JavaScript code so that only the images within the <div class="auto"> are included in the animation. How can I achieve this?

答案1

得分: 0

使用JavaScript,您可以根据各种属性选择HTML元素,包括ID、类和标签名称。以下是一些方法:

鉴于您的所有图像都具有类名 "mySlides",您可以使用 getElementsByClassName 来选择它们所有。

var index = 0;
const imgs = document.getElementsByClassName('mySlides');

function changeBanner() {
  [].forEach.call(imgs, function(v, i) {
    imgs[i].hidden = i !== index;
  });
  index = (index + 1) % imgs.length;
}

window.onload = function() {
  setInterval(changeBanner, 1000);
};
<img src="https://dummyimage.com/100x50/f55/000">
<div class="auto" style="max-width: 1050px">
  <img class="mySlides" src="https://dummyimage.com/100x100/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/90x90/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/80x80/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/70x70/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/60x60/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/50x50/aaa/fff">
</div>

或者,通过链接 getElementsByClassNamegetElementsByTagName,选择所有位于第一个具有类名 "auto" 的元素内的 &lt;img&gt; 元素。

var index = 0;
const imgs = document.getElementsByClassName('auto')[0].getElementsByTagName('img');

function changeBanner() {
  [].forEach.call(imgs, function(v, i) {
    imgs[i].hidden = i !== index;
  });
  index = (index + 1) % imgs.length;
}

window.onload = function() {
  setInterval(changeBanner, 1000);
};
<img src="https://dummyimage.com/100x50/f55/000">
<div class="auto" style="max-width: 1050px">
  <img class="mySlides" src="https://dummyimage.com/100x100/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/90x90/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/80x80/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/70x70/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/60x60/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/50x50/aaa/fff">
</div>

类似地,您可以使用 querySelectorAll

var index = 0;
const imgs = document.querySelectorAll('.auto img');

function changeBanner() {
  [].forEach.call(imgs, function(v, i) {
    imgs[i].hidden = i !== index;
  });
  index = (index + 1) % imgs.length;
}

window.onload = function() {
  setInterval(changeBanner, 1000);
};
<img src="https://dummyimage.com/100x50/f55/000">
<div class="auto" style="max-width: 1050px">
  <img class="mySlides" src="https://dummyimage.com/100x100/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/90x90/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/80x80/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/70x70/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/60x60/aaa/fff">
  <img class="mySlides" src="https://dummyimage.com/50x50/aaa/fff">
</div>

或者,您可以使用CSS 使用CSS动画精灵表

英文:

Using JavaScript, you can select HTML elements according to various attributes, including ID, class, and tag name. Here are a few approaches:

Given that your images all have the class "mySlides", you can select them all with getElementsByClassName.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var index = 0;
const imgs = document.getElementsByClassName(&#39;mySlides&#39;);

function changeBanner() {
  [].forEach.call(imgs, function(v, i) {
    imgs[i].hidden = i !== index;
  });
  index = (index + 1) % imgs.length;
}

window.onload = function() {
  setInterval(changeBanner, 1000);
};

<!-- language: lang-html -->

&lt;img src=&quot;https://dummyimage.com/100x50/f55/000&quot;&gt;
&lt;div class=&quot;auto&quot; style=&quot;max-width: 1050px&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/100x100/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/90x90/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/80x80/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/70x70/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/60x60/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/50x50/aaa/fff&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

Or, select all &lt;img&gt; elements inside the first element with class "auto" by chaining getElementsByClassName and getElementsByTagName.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var index = 0;
const imgs = document.getElementsByClassName(&#39;auto&#39;)[0].getElementsByTagName(&#39;img&#39;);

function changeBanner() {
  [].forEach.call(imgs, function(v, i) {
    imgs[i].hidden = i !== index;
  });
  index = (index + 1) % imgs.length;
}

window.onload = function() {
  setInterval(changeBanner, 1000);
};

<!-- language: lang-html -->

&lt;img src=&quot;https://dummyimage.com/100x50/f55/000&quot;&gt;
&lt;div class=&quot;auto&quot; style=&quot;max-width: 1050px&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/100x100/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/90x90/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/80x80/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/70x70/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/60x60/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/50x50/aaa/fff&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

Similarly, you could use querySelectorAll.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var index = 0;
const imgs = document.querySelectorAll(&#39;.auto img&#39;);

function changeBanner() {
  [].forEach.call(imgs, function(v, i) {
    imgs[i].hidden = i !== index;
  });
  index = (index + 1) % imgs.length;
}

window.onload = function() {
  setInterval(changeBanner, 1000);
};

<!-- language: lang-html -->

&lt;img src=&quot;https://dummyimage.com/100x50/f55/000&quot;&gt;
&lt;div class=&quot;auto&quot; style=&quot;max-width: 1050px&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/100x100/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/90x90/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/80x80/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/70x70/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/60x60/aaa/fff&quot;&gt;
  &lt;img class=&quot;mySlides&quot; src=&quot;https://dummyimage.com/50x50/aaa/fff&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

Alternatively, animate a sprite sheet with CSS.

huangapple
  • 本文由 发表于 2023年6月22日 09:05:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定