簡體   English   中英

JavaScript幻燈片顯示按鈕不起作用

[英]Javascript slideshow buttons not working

我似乎無法正常運行幻燈片,當它在空白HTML頁面上單獨運行時,它可以正常工作,但是當我嘗試將其合並到我的網站時,它確實出現了,但是我無法使用“下一個”和“上一個”按鈕。 有人可以幫我解決問題嗎? 謝謝。

HTML

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Kawasaki Motorcycle Club UK</title>
    <link rel="stylesheet" href="main.css">

</head>
<body>
    <div id="wrapper">
        <header>
        </header>
            <nav>
                <ul class="navbar">
                    <li><a href="bikes.html">BIKES</a></li>
                    <li><a href="news.html">NEWS</a></li>
                    <li><a href="events.html">EVENTS</a></li>
                    <li><a href="join.html">JOIN</a></li>
                    <li><a href="contact.html">CONTACT</a></li>
                </ul>                        
            </nav>
        </div>
    <div class="contentbox">

        <div id="maincontent"> //slideshow starts here
            <img src="mybike.jpg" id="slideshow"/>
            <br>

    <div id="caption">
        caption for image 1

    </div>

    <a href="#" onclick="changeImage(-1); return false;">Previous</a><br>
    <a href="#" onclick="changeImage(-1); return false;"?>Next</a><br>

    <script src="slideshow.js"></script>

        </div>

    </div>
<br>
    <div>
    <div class="socialcontainer">
        <a href="facebooklink"><img id="facebookbutton"/></a>
        <a href="twitterlink"><img id="twitterbutton"/></a>
        <a href="googlelink"><img id="googlebutton"/></a>



</div>



    </div>





</body>

使用Javascript

var images = ["mybike.jpg", "image2.jpg", "image3.jpg"];
var caption = ["My bike", "House", "Chess"];

var imageNumber = 0;
var imageLength = images.length - 1;

function changeImage(x) {
"use strict";
imageNumber += x;
// if reached end of array start over
if (imageNumber > imageLength) {
    imageNumber = 0;
}
if (imageNumber < 0) {
    imageNumber = imageLength;
}

document.getElementById("slideshow").src = images[imageNumber];
document.getElementById("caption").innerHTML = caption[imageNumber];

return false;
}

CSS

body {
font-family: 'Arial', sans-serif;
background-color: #000;

}

#wrapper {
max-width: 1000px;
margin: 0 auto;
background-color: #fff
padding: 32px;

}

header {

height: 110px;
background: url(header.png);
}


header h1 { //NOT NEEDED
text-align: center;
color: #FFF;

}

header h2 { //NOT NEEDED
font-variant: small-caps;
text-align: center;
color: #fff;
}

.navbar {
padding: 5px 0 5px 0;
text-align: center;
line-height: 35px;
background: url(navbar.png);
background-size: contain;


}

ul.navbar {
margin-top: 15px;

}

.navbar li {
display: inline;

padding: 0 40px 0 40px;
font-size: 30px;
font-weight: 800;      
}

a:hover, a:visited, a:link, a:active {
text-decoration: none;

background: #60bf19;
 color: #FFF;
text-shadow:
-5px -5px 0 #000;
}

a:hover {
color:dimgrey;
text-shadow:
-5px -5px 0 #000;
}

a:active {
color: #FFF
text-shadow:
-5px -5px 0 #000;
}

.contentbox {
width: 1000px;
overflow:auto;
margin: 0 auto;
background-color: #383131;
border-radius: 5px;
}

#maincontent {
color: #FFF;
}

.sideimage {
float: right;
}



.socialcontainer {
width: auto;
height: auto;
text-align: center;   
}

#facebookbutton {
    background-image: url(facebook-hover.png);

height: 48px;
width: 48px;


-webkit-transition: all ease 0.3s;
-moz-transition: all ease 0.3s;
-o-transition: all ease 0.3s;
-ms-transition: all ease 0.3s;
transition: all ease 0.3s;
}

#facebookbutton:hover {
background-position: 0px -48px;
box-shadow: 0px 0px 4px 1px rgba(0,0,0,0.8);
}

#twitterbutton {
    background-image: url(twitter-hover.png);
height: 48px;
width: 48px;


-webkit-transition: all ease 0.3s;
-moz-transition: all ease 0.3s;
-o-transition: all ease 0.3s;
-ms-transition: all ease 0.3s;
transition: all ease 0.3s;
}

#twitterbutton:hover {
background-position: 0px -48px;
box-shadow: 0px 0px 4px 1px rgba(0,0,0,0.8);
}


#googlebutton {
    background-image: url(google-hover.png);
height: 48px;
width: 48px;


-webkit-transition: all ease 0.3s;
-moz-transition: all ease 0.3s;
-o-transition: all ease 0.3s;
-ms-transition: all ease 0.3s;
transition: all ease 0.3s;
}

#googlebutton:hover {
background-position: 0px -48px;
box-shadow: 0px 0px 4px 1px rgba(0,0,0,0.8);
}
if (imageNumber > imageLength) {
    imageNumber = 0;
}
if (imageNumber < 0) {
    imageNumber = imageLength;
}

這是你的問題。 想象一下x >= imageLength 您將繼續將imageNumber重置為0,然后迅速將其變成無法正常工作的循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM