2016-08-23 88 views
1

我有一个div中的五个图像,他们看起来像这样[1] [2] [3] [4] [5]。 我想制作一个JavaScript,将它们自动变成[2] [3] [4] [5] [1],然后[3] [4] [5] [1] [2]等等。 现在,我设法只进行第一次图像更改。使用我拥有的脚本来改变它们全部是可行的吗? 预先感谢您。 (我不知道的jQuery,我在JavaScript的初学者。)图片库-javascript

var i = 1; 
 
var x = ["img/facebook.png", "img/linkedin.png", "img/mail.png", "img/twiter.png", "img/skype.png"]; 
 
var y = ["1", "2", "3", "4", "5"]; 
 

 
function slide(num) { 
 
    
 
    i = i + num; 
 
\t if(i < 0) i = x.length - 1; 
 
\t if (i > x.length - 1) i = 0 \t 
 
    document.getElementById('image01').src = x[i]; 
 
\t document.getElementById('text01').innerHTML = i; 
 
} 
 

 
function autorun(){setInterval("slide(1)", 3000);}
body{margin: 0; padding: 0; color:#fdbf7d; font-family: courier new;} 
 
.container{margin-left:auto; margin-right:auto; width:940px; padding:10px; background-color: yellow;} 
 
#photo_gallery_container{background-color: brown;} 
 
#photo_gallery{}
<!doctype html> 
 
<html lang="en"> 
 
<head> 
 
    
 
    <title>Gallery</title> 
 
    
 
    <meta http-equiv="content" content="text/html"; charset=UTF-8> 
 
    <link rel="stylesheet" type="text/css" href="style.css"/> 
 
    
 
</head> 
 

 
<body onload="autorun()"> 
 
    <div class="container"> 
 
     <div id="photo_gallery_container"> 
 
\t  <div id="photo_gallery"> 
 
\t \t <center> 
 
\t \t  <img id="image01" src="img/facebook.png" width="100px" height="100px"> 
 
\t \t  <img id="image02" src="img/linkedin.png" width="100px" height="100px"> 
 
\t \t  <img id="image03" src="img/mail.png" width="100px" height="100px"> 
 
\t \t  <img id="image04" src="img/twiter.png" width="100px" height="100px"> 
 
\t \t  <img id="image05" src="img/skype.png" width="100px" height="100px"> 
 
\t \t \t <span id="text01">1</span>&nbsp;&nbsp;&nbsp; 
 
\t \t \t <span id="text02">2</span>&nbsp;&nbsp;&nbsp; 
 
\t \t \t <span id="text03">3</span>&nbsp;&nbsp;&nbsp; 
 
\t \t \t <span id="text04">4</span>&nbsp;&nbsp;&nbsp; 
 
\t \t \t <span id="text05">5</span>&nbsp;&nbsp;&nbsp; 
 
\t \t </center> \t 
 
\t \t </div> 
 
\t \t <script type="text/javascript" src="myscript.js"></script> 
 
\t </div> 
 
    </div> 
 
</body> 
 
</html>

+0

你可以删除Child和addChild,或者使用jQuery作为标记 – mplungjan

回答

0

你可以通过克隆图像和最后一个图像后,将它做到这一点:

var thisImage = $('#image01').clone(); //clone image 
$('#image01').remove(); //remove original image 
var lastImage = $('center img').eq(x.length-1); //get last image 
thisImage.insertAfter(lastImage); //insert clone after last image