简体   繁体   English

将按钮更改为图像导航滑块Javascript

[英]Change Button to Image Navigation Slider Javascript

I found a tutorial about create a simple slideshow with JavaScript. 我找到了有关使用JavaScript创建简单幻灯片的教程。 So i try it and it was success. 所以我尝试了,那是成功的。 But the navigation is in button type for Play/Stop. 但是导航是“播放/停止”的按钮类型。

I want to change it to an image navigation called play and another image called stop. 我想将其更改为一个名为play的图像导航和另一个名为stop的图像。 When i click on play image, image stop will appear and play image will be hidden. 当我单击播放图像时,将出现图像停止并且播放图像将被隐藏。 And when i click stop image, play image will appear and stop image will stop. 当我单击停止图像时,将出现播放图像,并且停止图像将停止。

Here is my JavaScript: 这是我的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);
    }

And here is my HTML (First): 这是我的HTML(第一篇):

<div align="right">
    <input id="pauseButton" onclick="toggleInterval()" type="button" value="Pause" />
</div>

And this is my HTML now: 这是我现在的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>

Thanks for the answer 感谢您的回答

Instead of having img element in your button , you can work with classes and background-image css. 除了在button中包含img元素外,您还可以使用classesbackground-image CSS。 It's easier to manage afterwards, you simply set the class you need and the image will change according to this. 之后,管理起来更容易,您只需设置所需的类,图像就会据此更改。 Something like this allows you to switch images easily: 这样的事情可以让您轻松切换图像:

 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> 

A simple trick 一个简单的把戏

Set the pause tag to display: block; 将暂停标签设置为显示: and put it to the back 放回去

<div align="right">
    <img src="../asset/images/play.png" style="position: relative; z-index: 1;" title="Play" id="pauseButton" onclick="toggleInterval()" />
    <img src="../asset/images/stop.png" style="position: absolute; z-index: 0;" title="Stop" onclick="toggleinterval(true)" />
</div> 

And remove opacity from Play when you click it. 并在单击时从“播放”中删除不透明度。

function toggleInterval() {
    var button = document.getElementById("pauseButton");
    if(!window.paused) {
        clearInterval(switching);
        button.value = "Resume";
        //here is the trick
        button.style.opacity = 1;
    } else {
        switching = setInterval("toggleSlide(true)", interval);
        button.value = "Pause";
        //here is the trick
        button.style.opacity = 0;
    }
    window.paused = !(window.paused);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM