我找到了有关使用JavaScript创建简单幻灯片的教程。所以我尝试了,那是成功的。但是导航是“播放/停止”的按钮类型。
我想将其更改为一个名为play的图像导航和另一个名为stop的图像。当我单击播放图像时,将出现图像停止并且播放图像将被隐藏。当我单击停止图像时,将出现播放图像,并且停止图像将停止。
这是我的JavaScript:
var interval = 2000; // You can change this value to your desired speed. The value is in milliseconds, so if you want to advance a slide every 5 seconds, set this to 5000.
var switching = setInterval("toggleSlide(true)", interval);
window.paused = false;
function toggleInterval() {
var button = document.getElementById("pauseButton");
if(!window.paused) {
clearInterval(switching);
button.value = "Resume";
} else {
switching = setInterval("toggleSlide(true)", interval);
button.value = "Pause";
}
window.paused = !(window.paused);
}
这是我的HTML(第一篇):
<div align="right">
<input id="pauseButton" onclick="toggleInterval()" type="button" value="Pause" />
</div>
这是我现在的HTML:
<div align="right">
<img src="../asset/images/play.png" title="Play" id="pauseButton" onclick="toggleInterval()" />
<img src="../asset/images/stop.png" style="cursor:pointer; display:none;" title="Stop" onclick="toggleinterval(true)" />
</div>
感谢您的回答
最佳答案
可以在img
和button
css中使用而不是在classes
中包含background-image
元素。之后,管理起来更容易,您只需设置所需的类,图像就会据此更改。这样的事情可以让您轻松切换图像:
window.paused = true;
// You can keep the same logic as with your button
// but instead of changing value, you change the class
// And in your css you assign different images to different classes
function toggleInterval() {
var button = document.getElementById("pauseButton");
if (!window.paused) {
button.className = 'resume';
} else {
button.className = 'pause';
}
window.paused = !(window.paused);
}
#pauseButton {
width: 100px;
height: 100px;
background-size: contain;
}
#pauseButton.pause {
background-image: url('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTqDrugj4g4aPOBxEXi7zv1zH_c1OXaaWbOg8HRkQuVDhb8LVn46PP8wTo')
}
#pauseButton.resume {
background-image: url('https://images-eu.ssl-images-amazon.com/images/G/02/uk-electronics/product_content/Nikon/ECS-NK1308141-B00ECGX8FM-2L-1024w683q85s10-BKEH_London_Paris__0316.jpg')
}
<div>
<div id="pauseButton" class="resume" onclick="toggleInterval()"></div>
</div>
关于javascript - 将按钮更改为图像导航滑块Javascript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33551189/