[英]setInterval() not working with src linked images in HTML
New coder - 1st question here. 新编码器-这里的第一个问题。 I'm doing first free code camp project, but question is about a unrequired flourish I just want to add and learn about, it's not needed for project. 我正在做第一个免费的代码训练营项目,但是问题是我只是想添加和了解一个不需要的繁荣,项目不需要它。 I want the album covers I've linked in HTML to change every x seconds. 我希望我链接过HTML的专辑封面每隔x秒更改一次。 But nothing happens yet. 但是什么都没有发生。 Images just stay static. 图像只是保持静止。 Thanks 谢谢
<h2> Discography </h2>
<div class="row">
<div id="album" class="col-lg-8">
<div class="albums" id="album 0"> <span><img src="https://upload.wikimedia.org/wikipedia/en/b/b6/Elliottsmithromancandle.jpeg" alt="1994" class="img-responsive"></span></div>
<div class="albums" id="album 1"> <span><img src="https://upload.wikimedia.org/wikipedia/en/e/e3/Elliott_Smith_%28album%29.jpg" alt="1995" class="img-responsive"></span></div>
<div class="albums" id="album 2"> <span><img src="https://upload.wikimedia.org/wikipedia/en/f/fd/Elliottsmitheitheror55.jpg" alt="1997" class="img-responsive"></span></div>
<div class="albums" id="album 3"> <span><img src="https://upload.wikimedia.org/wikipedia/en/thumb/3/…albumcover.jpg/330px-ElliottsmithXOalbumcover.jpg" alt="1998" class="img-responsive"></span></div>
<div class="albums" id="album 4"> <span><img src="https://upload.wikimedia.org/wikipedia/en/thumb/a/…_cover.jpg/330px-Elliott_smith_figure_8_cover.jpg" alt="2000" class="img-responsive"></span></div>
<div class="albums" id="album 5"> <span><img src="https://upload.wikimedia.org/wikipedia/en/8/8c/Elliott_smith_from_a_basement_on_the_hill_cover.jpg" alt="2004" class="img-responsive"></span></div>
<div class="albums" id="album 6"> <span><img src="https://upload.wikimedia.org/wikipedia/en/4/4e/New_Moon_%28Elliott_Smith_album%29_cover.jpg" alt="2007" class="img-responsive"></span></div>
</div>
and the javascript code 和JavaScript代码
//Change album cover after 5 seconds - continous loop//
var Discography = document.getElementById("album");
var disc = ["album 0", "album 1", "album 2", "album 3", "album 4", "album 5", "album 6"];
var counter = 0;
function changeDisc() {
if (counter >= disc.length) {
counter = 0;
}
Discography.setAttribute =disc[counter];
counter++;
}
var myCounter = setInterval(changeDisc, 6000);
Discography.onClick = function() {
clearInterval(Disography);
Discography.innerHTML = "Counter stopped";
};
You aren't actually setting the attribute. 您实际上并不是在设置属性。 You are actually removing the setAttribute method and replacing it with the url. 实际上,您正在删除setAttribute方法,并将其替换为url。 I think you want this: 我想你想要这个:
function changeDisc() {
if (counter >= disc.length) {
counter = 0;
}
Discography.setAttribute('src', disc[counter]);
counter++;
}
I managed to get your code working. 我设法使您的代码正常工作。 I don't know if this is what you wanted, but I hope it helped you. 我不知道这是否是您想要的,但我希望它能对您有所帮助。
Assign Attributes 分配属性
Discography.setAttribute = disc[counter];
Should be 应该
Discography.setAttribute("src", discs[counter]);
Bind EventHandlers 绑定事件处理程序
Discography.onClick = function() {
Should be 应该
Discography.addEventListener("click", function () {
Wrong Element 元素错误
You tried to assign the src
attribute to the <div id="album" class="col-lg-8">
which is a div
and it does not have that attribute. 您试图将src
属性分配给<div id="album" class="col-lg-8">
这是一个div
,但没有该属性。
To fix this I added all possible album images to a Array
and this will be assigned to a single <img />
to get this working. 为了解决这个问题,我将所有可能的专辑图像添加到一个Array
,并将其分配给单个<img />
以使其正常工作。
Note that I modified some naming conventions, which I do prefer. 请注意,我修改了一些命名约定,但我更喜欢。
My Solution 我的解决方案
var discs = [ "https://upload.wikimedia.org/wikipedia/en/b/b6/Elliottsmithromancandle.jpeg", "https://upload.wikimedia.org/wikipedia/en/e/e3/Elliott_Smith_%28album%29.jpg", "https://upload.wikimedia.org/wikipedia/en/f/fd/Elliottsmitheitheror55.jpg", "https://upload.wikimedia.org/wikipedia/en/thumb/3/…albumcover.jpg/330px-ElliottsmithXOalbumcover.jpg", "https://upload.wikimedia.org/wikipedia/en/thumb/a/…_cover.jpg/330px-Elliott_smith_figure_8_cover.jpg", "https://upload.wikimedia.org/wikipedia/en/8/8c/Elliott_smith_from_a_basement_on_the_hill_cover.jpg" , "https://upload.wikimedia.org/wikipedia/en/4/4e/New_Moon_%28Elliott_Smith_album%29_cover.jpg", ]; var counter = 0; var discography = document.getElementById("album"); function moveToNextDisc() { if (counter >= discs.length) { counter = 0; } discography.setAttribute("src", discs[counter]); counter++; } var interval = setInterval(moveToNextDisc, 1000); discography.addEventListener("click", function () { clearInterval(interval); discography.innerHTML = "Counter stopped"; });
<h2>Discography</h2> <div class="row"> <div class="col-lg-8"> <div class="albums"> <span> <img id="album" alt="1994" class="img-responsive"> </span> </div> </div> </div>
for learning purpose, you are developing a component that: 出于学习目的,您正在开发一个组件,该组件:
Javascript should be used for business logic and html/css as a view reflection of the state of the component. 应将Javascript用于业务逻辑和html / css ,以反映组件状态 。
So we need for an Object
which has at least three methods
: 因此,我们需要一个至少具有three methods
的Object
:
public play
starts the player public play
开始玩家 public pause
pauses the player public pause
暂停播放器 private _loop
iterates through the tracks private _loop
遍历轨道 and for a Controller
which controls the html view
. 对于控制html view
的Controller
。
function Player(element, interval) { this.element = element; this.tracks = element.querySelectorAll('.albums'); this.currentTrack = null; this.isPlaying = false; this._timeout = null; this.interval = Number(interval) || 2000; } Player.prototype.pause = function() { window.clearTimeout(this._timeout); this.isPlaying = false; return this; }; Player.prototype.play = function() { this.isPlaying = true; return this._loop(); }; Player.prototype._loop = function() { var self = this; self._timeout = window.setTimeout(function() { var track; if(self.currentTrack) { track = self.currentTrack.nextElementSibling; self.currentTrack.style.opacity = 0; } if(!track) { track = self.tracks[0]; } track.style.opacity = 1; self.currentTrack = track; return self._loop(); }, self.interval); return this; } /** VIEW CTRL **/ function PlayerCtrl() { var element = document.querySelector('#album'); var player = new Player(element, 3000); var play = document.querySelector('#Play'); var pause = document.querySelector('#Pause'); play.onclick = function() { return player.isPlaying || player.play(); }; pause.onclick = function() { return player.isPlaying && player.pause(); }; player.play(); } document.addEventListener('DOMContentLoaded', PlayerCtrl);
#album { width: 300px; height: 300px; margin: 10px auto; overflow: hidden; background: lightcoral; position: relative; border: 3px dotted #333; } .albums { transition: 450ms opacity linear; opacity: 0; position: absolute; top: 0; left: 0; } .albums img { max-width: 100%; }
<button id="Play">Play</button> <button id="Pause">Pause</button> <hr /> <h2> Discography </h2> <div class="row"> <div id="album" class="col-lg-8"> <div class="albums" id="album 0"> <span><img src="https://upload.wikimedia.org/wikipedia/en/b/b6/Elliottsmithromancandle.jpeg" alt="1994" class="img-responsive"></span></div> <div class="albums" id="album 1"> <span><img src="https://upload.wikimedia.org/wikipedia/en/e/e3/Elliott_Smith_%28album%29.jpg" alt="1995" class="img-responsive"></span></div> <div class="albums" id="album 2"> <span><img src="https://upload.wikimedia.org/wikipedia/en/f/fd/Elliottsmitheitheror55.jpg" alt="1997" class="img-responsive"></span></div> <div class="albums" id="album 3"> <span><img src="https://upload.wikimedia.org/wikipedia/en/thumb/3/…albumcover.jpg/330px-ElliottsmithXOalbumcover.jpg" alt="1998" class="img-responsive"></span></div> <div class="albums" id="album 4"> <span><img src="https://upload.wikimedia.org/wikipedia/en/thumb/a/…_cover.jpg/330px-Elliott_smith_figure_8_cover.jpg" alt="2000" class="img-responsive"></span></div> <div class="albums" id="album 5"> <span><img src="https://upload.wikimedia.org/wikipedia/en/8/8c/Elliott_smith_from_a_basement_on_the_hill_cover.jpg" alt="2004" class="img-responsive"></span></div> <div class="albums" id="album 6"> <span><img src="https://upload.wikimedia.org/wikipedia/en/4/4e/New_Moon_%28Elliott_Smith_album%29_cover.jpg" alt="2007" class="img-responsive"></span></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.