2017-07-06 43 views
0

我創建了簡單的幻燈片。如果yu想要改變圖像,你必須點擊鍵盤上的箭頭按鈕。我想添加說明,例如「從7 1個項目」 等幻燈片顯示頁面編號在js

這裏是我的源:

var img_index = 0; 
 
var imgs = [ 
 
    "assets/1.jpg", 
 
    "assets/2.jpg", 
 
    "assets/3.jpg", 
 
    "assets/4.jpg", 
 
    "assets/5.jpg", 
 
    "assets/6.jpg", 
 
    "assets/7.jpg" 
 

 
]; 
 

 
function changeImage() { 
 
    var img = document.getElementById("images"); 
 
    img_index = ++img_index % 7; 
 
    img.src = imgs[img_index]; 
 
} 
 

 

 
function checkKey(e) { 
 

 
    e = e || window.event; 
 
    if (e.keyCode == '37') { 
 
    changeImage(); 
 
    } else if (e.keyCode == '39') { 
 
    changeImage(); 
 
    } 
 

 
} 
 

 
document.onkeydown = checkKey; 
 

 

 

 
function sliderText(){ 
 
    
 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="UTF-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> 
 
    <link rel="stylesheet" href="style12.css"> 
 
    <title>Task 12</title> 
 
</head> 
 
<div id="slideshow_section"> 
 
    <img id="images" src="assets/1.jpg" /> 
 
</div> 
 

 
<body> 
 

 
</body> 
 
<script src="script12.js"></script> 
 

 
</html>

你能給我一些提示我該怎麼辦呢?

謝謝 鮎魚

回答

0

試試這個,這可能會幫助你的!

refer this link(或)嘗試此代碼

HTML

<div class="slideshow-container"> 
    <div class="mySlides fade"> 
    <div class="numbertext">1/3</div> 
    <img src="img1.jpg" style="width:100%"> 
    <div class="text">Caption Text</div> 
    </div> 

    <div class="mySlides fade"> 
    <div class="numbertext">2/3</div> 
    <img src="img2.jpg" style="width:100%"> 
    <div class="text">Caption Two</div> 
    </div> 

    <div class="mySlides fade"> 
    <div class="numbertext">3/3</div> 
    <img src="img3.jpg" style="width:100%"> 
    <div class="text">Caption Three</div> 
    </div> 

    <a class="prev" onclick="plusSlides(-1)">&#10094;</a> 
    <a class="next" onclick="plusSlides(1)">&#10095;</a> 
</div> 
<br> 

<div style="text-align:center"> 
    <span class="dot" onclick="currentSlide(1)"></span> 
    <span class="dot" onclick="currentSlide(2)"></span> 
    <span class="dot" onclick="currentSlide(3)"></span> 
</div> 

的CSS

{box-sizing:border-box} 

/* Slideshow container */ 
.slideshow-container { 
    max-width: 1000px; 
    position: relative; 
    margin: auto; 
} 

.mySlides { 
    display: none; 
} 

/* Next & previous buttons */ 
.prev, .next { 
    cursor: pointer; 
    position: absolute; 
    top: 50%; 
    width: auto; 
    margin-top: -22px; 
    padding: 16px; 
    color: white; 
    font-weight: bold; 
    font-size: 18px; 
    transition: 0.6s ease; 
    border-radius: 0 3px 3px 0; 
} 

/* Position the "next button" to the right */ 
.next { 
    right: 0; 
    border-radius: 3px 0 0 3px; 
} 

/* On hover, add a black background color with a little bit see-through */ 
.prev:hover, .next:hover { 
    background-color: rgba(0,0,0,0.8); 
} 

/* Caption text */ 
.text { 
    color: #f2f2f2; 
    font-size: 15px; 
    padding: 8px 12px; 
    position: absolute; 
    bottom: 8px; 
    width: 100%; 
    text-align: center; 
} 

/* Number text (1/3 etc) */ 
.numbertext { 
    color: #f2f2f2; 
    font-size: 12px; 
    padding: 8px 12px; 
    position: absolute; 
    top: 0; 
} 

/* The dots/bullets/indicators */ 
.dot { 
    cursor:pointer; 
    height: 13px; 
    width: 13px; 
    margin: 0 2px; 
    background-color: #bbb; 
    border-radius: 50%; 
    display: inline-block; 
    transition: background-color 0.6s ease; 
} 

.active, .dot:hover { 
    background-color: #717171; 
} 

/* Fading animation */ 
.fade { 
    -webkit-animation-name: fade; 
    -webkit-animation-duration: 1.5s; 
    animation-name: fade; 
    animation-duration: 1.5s; 
} 

@-webkit-keyframes fade { 
    from {opacity: .4} 
    to {opacity: 1} 
} 

@keyframes fade { 
    from {opacity: .4} 
    to {opacity: 1} 
} 

JS:

var slideIndex = 1; 
showSlides(slideIndex); 

function plusSlides(n) { 
    showSlides(slideIndex += n); 
} 

function currentSlide(n) { 
    showSlides(slideIndex = n); 
} 

function showSlides(n) { 
    var i; 
    var slides = document.getElementsByClassName("mySlides"); 
    var dots = document.getElementsByClassName("dot"); 
    if (n > slides.length) {slideIndex = 1} 
    if (n < 1) {slideIndex = slides.length} 
    for (i = 0; i < slides.length; i++) { 
     slides[i].style.display = "none"; 
    } 
    for (i = 0; i < dots.length; i++) { 
     dots[i].className = dots[i].className.replace(" active", ""); 
    } 
    slides[slideIndex-1].style.display = "block"; 
    dots[slideIndex-1].className += " active"; 
} 

所有最適合您的JS項目;)

+0

嗨,我看到了。這對我沒有幫助,因爲我想保留我的代碼。在這個代碼中沒有任何值得我寫的js文件。謝謝。 –

+0

好的。它可能有助於其他一些人。 –

+0

是的,這是真的:) –

1

繼承人另一個工作代碼(分別爲IAM要解釋,如何設置圖像DESCR):

  var images = [ 
      "http://www.helloworld.com/uploads/slider/1.jpg", 
      "http://www.helloworld.com/uploads/slider/2.jpg", 
      "http://www.helloworld.com/uploads/slider/3.jpg" 
      ]; 
      var num = 0; 
     function next() { 
      var slider = document.getElementById("slider"); 
      num++; 
      if(num >= images.length) { 
      num = 0; 
      } 
      slider.src = images[num]; 
      setDescription();//calling function 
      } 

     function prev() { 
      var slider = document.getElementById("slider"); 
      num--; 
      if(num < 0) { 
      num = images.length-1; 
      } 
      slider.src = images[num]; 
       setDescription(); 
//calling function - this can be included to your changeImage() function without any errors. 
     } 
    //To set the image description, i use following function: 


      function setDescription(){ 
      var descr = document.getElementById("Description"); 
      var selectedimageindex = num + 1;/*beause Arrays start counting at 0 
*/ 
     descr.innerHTML= "image "+ selectedimageindex +" of " + images.length; 

     } 

參考你的代碼,它會看起來像這樣(運行它! ;)): 我需要chage一些部分,如圖像資源。

var img_index = 0; 
 
var imgs = [ 
 
    "http://www.sololearn.com/uploads/slider/1.jpg", 
 
    "http://www.sololearn.com/uploads/slider/2.jpg", 
 
    "http://www.sololearn.com/uploads/slider/3.jpg" 
 
]; 
 

 
function changeImage() { 
 
    var img = document.getElementById("images"); 
 
    img_index = ++img_index % 3; 
 
    img.src = imgs[img_index]; 
 
    sliderText(); 
 
} 
 

 
//needed to remove keychange functions because its not working on stackoverflow 
 

 
function sliderText(){ 
 
    var Description = document.getElementById("description"); 
 
    var imagenumber = img_index + 1; //Arrays start counting at 0. 
 
    Description.innerHTML = "image "+ imagenumber + " of "+imgs.length; 
 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="UTF-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> 
 
    <link rel="stylesheet" href="style12.css"> 
 
    <title>Task 12</title> 
 
</head> 
 
<div id="slideshow_section"> 
 
    <img id="images" src="http://www.sololearn.com/uploads/slider/1.jpg" /> 
 
    <span id="description">Image 1 of 3</span> 
 
    <button id="nextbtn" onclick="changeImage()">Next</button> 
 
</div> 
 

 
<body> 
 

 
</body> 
 

 
</html>

反饋給這個答案?請!希望它有幫助!


祝您好運!

+0

非常感謝你muuuuch;)))我會用你的想法;)))laim2003 –