JQuery幻灯片,带有可点击的编号位置

时间:2015-04-07 13:59:28

标签: javascript jquery html css

好吧,我有一些问题,我正在制作我自己的Jquery幻灯片,包括播放,暂停,下一个和上一个按钮。我想制作带有编号图像当前编号的幻灯片,并且可点击以便编号为4的图像,但我想要点击数字2。 我在谷歌和这个网站上搜索,但很难找到,因为使用Jquery插件有很多可供参考,但我正在学习Jquery,所以我没有使用插件。

他们是否必须使用数组对图像进行排序,并且他们有数字列表?

无论如何这是我的代码。 HTML:

<html>
<head>
    <title>Slideshow - Menggunakan Jquery</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="jquery-2.1.3.js"></script>
</head>
<body>
<div id='items'>
    <ul>
        <li class='item first'><img src="raisa1.jpg"/></li>
        <li class='item'><img src="raisa2.jpg"/></li>
        <li class='item'><img src="raisa3.jpg"/></li>
    </ul>
</div>
<ul id='controls'>
    <button id='prev'>Prev</button>
    <button id='play'>Play</button>
    <button id='pause'>Pause</button>
    <button id='next'>Next</button>
</ul>
</body>
<script src="slideshow.js"></script>
</html>

CSS:

#items {
    position : relative;
    width : 400px;
    height : 200px;
    top : 20px;
    left : 20px;
}
.item {
    position : absolute;
    background-color : #eee;
    border : 1px solid #ccc;
    width : 398px;
    height : 198px;
    display :none;
    text-align : center;
    font-size : 72px;
}

.item img{
    text-align: center;
    display: block;
    max-width:398px;
    max-height: 198px;
    width: auto;
    height: auto;
    margin: auto;
}

.first{
    display : block;
}
#controls {
    margin-top : 30px;
}
li {
    display : inline-block;
    padding : 5px;
    border : 1px solid #ccc;
    background-color : #eee;
    cursor : pointer;
}
#play {
    display : none;
}

Jquery的:

var timeoutId;
var slideImage = function( step ) {
    if ( step == undefined ) step = 1;
    //Clear timeout if any
    clearTimeout ( timeoutId );
    //Get current image's index
    var indx = $('.item:visible').index('.item');
    //If step == 0, we don't need to do any fadein our fadeout
    if ( step != 0 ) {
       //Fadeout this item
       $('.item:visible').fadeOut();
    }
    //Increment for next item
    indx = indx + step ;
    //Check bounds for next item
    if ( indx >= $('.item').length ) {
        indx = 0;
    } else if ( indx < 0 ) {
        indx = $('.item').length - 1;
    }
    //If step == 0, we don't need to do any fadein our fadeout
    if ( step != 0 ) {
       //Fadein next item
       $('.item:eq(' + indx + ')').fadeIn();
    }
    //Set Itmeout
    timeoutId = setTimeout ( slideImage, 5000 );
};
//Start sliding
slideImage(0);
//When clicked on prev
$('#prev').click(function() {
    //slideImage with step = -1
    slideImage (-1);   
});
//When clicked on next
$('#next').click(function() {   
     //slideImage with step = 1
     slideImage (1);
});               
//When clicked on Pause
$('#pause').click(function() {
   //Clear timeout
   clearTimeout ( timeoutId );    
    //Hide Pause and show Play
    $(this).hide();
    $('#play').show();
});
//When clicked on Play
$('#play').click(function() {
   //Start slide image
   slideImage(0);
   //Hide Play and show Pause
   $(this).hide();
   $('#pause').show();
});
$(document).ready(function(){
   var sliderwidth = 500;
  $('#navigation li').bind('click',function() {
   $('.slider').animate({left:"-" + sliderwidth * $(this).index() },1000);
  });
});

1 个答案:

答案 0 :(得分:2)

我添加了

  • 每张图片的ID
  • 当前显示图片的全局变量
  • 将在点击链接时调用的函数,该函数将调用您的函数slideImage

JSFiddle:http://jsfiddle.net/p0gdopha/

//To store timeout id
var timeoutId,
    currImg = 1;

var slideImage = function( step ) {

  if ( step == undefined ) step = 1;

  //Clear timeout if any
  clearTimeout ( timeoutId );

  //Get current image's index
  var indx = $('.item:visible').index('.item');

  //If step == 0, we don't need to do any fadein our fadeout
  if ( step != 0 ) {
    //Fadeout this item
    $('.item:visible').fadeOut();
  }

  //Increment for next item
  indx = indx + step ;

  //Check bounds for next item
  if ( indx >= $('.item').length ) {
    indx = 0;
  } else if ( indx < 0 ) {
    indx = $('.item').length - 1;
  }

  //If step == 0, we don't need to do any fadein our fadeout
  if ( step != 0 ) {
    //Fadein next item
    $('.item:eq(' + indx + ')').fadeIn();
    currImg = $('.item:eq(' + indx + ')').attr('id');
  }

  //Set Itmeout
  timeoutId = setTimeout ( slideImage, 5000 );
};

//Start sliding
slideImage(0);

//When clicked on prev
$('#prev').click(function() {

  //slideImage with step = -1
  slideImage (-1);   
});

//When clicked on next
$('#next').click(function() {

  //slideImage with step = 1
  slideImage (1);
});

//When clicked on Pause
$('#pause').click(function() {

  //Clear timeout
  clearTimeout ( timeoutId );    

  //Hide Pause and show Play
  $(this).hide();
  $('#play').show();
});

//When clicked on Play
$('#play').click(function() {

  //Start slide image
  slideImage(0);

  //Hide Play and show Pause
  $(this).hide();
  $('#pause').show();
});

$('a').click(function(ev){
    ev.preventDefault();
    console.log(currImg + ' | ' + $(this).attr('href'));
    slideImage($(this).attr('href') - currImg);
});

$(document).ready(function(){
  var sliderwidth = 500;
  $('#navigation li').bind('click',function() {
    $('.slider').animate({left:"-" + sliderwidth * $(this).index() },1000);
  });
});
@font-face {
  src:url(../fonts/pacifico.woff);
  font-family: 'pacifico';
}

@font-face {
  src:url(../fonts/ptsans.woff);
  font-family: 'ptsans';
}

* {
  margin: 0px; padding: 0px;
}

.container {
  width: 1280px;
  margin: 0px auto;
  background-color: #eaeaea;
  font-family: 'ptsans','pacifico';
}

.admin-panel {
  height: 30px;
  line-height: 30px;
  background-color: #222222;
  color: #fff;
}

.admin-panel li{
  display: inline-block;
  padding-left: 80px;
}

.admin-panel li a{
  text-decoration: none;
  color: #808080;
}

.menu-title {
  height: 147px;
}

.menu-title li {
  display: inline-block;
  padding-right:25px;
  line-height: 147px;
  font-weight: bold;
}

.menu-title li a{
  text-decoration: none;
  color: #cc3333;
  padding:10px;
}

.menu-title li a:hover {
  border-bottom: 2px solid #cc3333;
}


.link-utama {
  padding-left: 110px;
}

.logo {
  padding-top: 20px;
  position: absolute;
  left:640px;
  text-align: center;
  font-family: 'pacifico'
}

.searchbar {
  line-height: 147px;
}

.menu-promo {
  background-color: #cc3333;
  height:147px;
  color: #fff;
  text-shadow: 0px 3px 1px #000;
  padding: 30px 50px 0px 50px;
  position: relative;

  box-shadow: inset 0px 3px 3px rgba(0,0,0,0.5);
}

.menu-promo a{
  text-decoration: none;
  color: #fff;
  font-size: 14px;

}

.menu-promo h3 {
  font-family: 'pacifico';
  font-size: 48px;
}

.purchase-promo {
  padding: 30px 50px 0px 50px;
}

a.button-purchase {
  display: block;
  padding: 15px 35px;
  width: 220px;
  height: 25px;
  background-color: #cc3333;
  border: 1px solid #90282c;
  border-radius: 3px;


  box-shadow: 0px 3px 1px #90282c, 0px 3px 1px #90282c;

  transition: all 0.3s ease 0s;

  line-height: 25px;
  text-align: center;
  text-decoration: none;
  color: #fff;
}

a.button-purchase:hover {
  box-shadow: 0px 3px 1px #90282c inset, 0px 1px 1px #bf4948;
}

a.button-purchase:active {
  background-color: #b32d2d;
  box-shadow: 0px 5px 1px #90282c inset, 0px 5px 1px #bf4948;
}

input[type="text"] {

  background-color: #eaeaea;
  box-shadow: 0px 1px 3px 0px rgba(0,0,0,0.1) inset;
  padding:6px 25px;
  border:0px;

}

input[type="text"] {

  background-color: #eaeaea;
  box-shadow: 0px 1px 3px 0px rgba(0,0,0,0.1) inset;
  padding:6px 35px;
  border:0px;

}

input[type="submit"] {

  background-color: #eaeaea;
  padding:6px 20px;
  border:0px;
  color: #888888;
  font-weight: bold;

}

input[type="text"]:focus {
  background-color: #fff;
}

input[type="text"]:focus ~ input[type="submit"]{

  background-color: #fff;
  box-shadow: 0px 1px 3px 0px rgba(0,0,0,0.3) inset;
  transition: all 0.1s ease 0s;
  padding:6px 20px;
  border:0px solid #fff;

}

.purchase-promo p {
  padding-top: 5px;
  font-size: 12px;
  text-align: center;
}

/* Content */
.content {
  padding:30px 80px 70px 80px;
  overflow: hidden;
}

.main-content {
  width: 800px;
  float: left;
}

.main-content h3 {
  font-size: 24px;
  font-weight: bold;
}

.article {
  padding: 25px 25px 25px 0px;
  text-align: justify;
  width: 800px;
}

.article img{
  padding-bottom: 15px;
  padding-left: 25px;
}

.preview h1{
  font-family: 'pacifico';
  font-weight: bold;
  font-size: 24px;
}

.top-review{
  padding: 50px 0px;
}

blockquote {
  font-family: 'ptsans';
  font-size: 22px;
  font-style: italic;
  width: 500px;
  margin: 0.25em 0;
  padding: 0.25em 40px;
  line-height: 1.45;
  position: relative;
  color: #888;
}

blockquote:before {
  display: block;
  content: "\201C";
  font-size: 120px;
  position: absolute;
  left: -20px;
  top: -40px;
  color: #888;
  font-style: normal;
}

blockquote cite {
  color: #999999;
  font-size: 14px;
  display: block;
  margin-top: 5px;
}

blockquote cite:before {
  content: "\2014 \2009";
}

.sidebar {
  width: 225px;
  float: right;
  text-align: justify;
}

.sidebar h3 {
  font-weight: bold;
  font-size: 1.5em;
  padding-bottom: 10px;
}

.sidebar1{
  padding-bottom: 25px;
  border-bottom: 2pt dashed #888888;
}

.sidebar2{
  padding-top: 25px;
}

.sidebar2 img {
  box-shadow: 1px 1px 1px #000;
  border-radius: 11px;
  text-align: center;
}

.sidebar2 p {
  padding:10px 0px;
  text-align: center;
}


/* footer */

.catatan-kaki {
  background-color: #cccccc;
  height: 180px;
  padding-left: 80px;
  padding-top: 25px;
}

.catatan1,.catatan2, .catatan3 {
  width:350px;
}

.catatan1 {
  float: left;
  text-align: left;
  padding-right: 25px;
}

.catatan2 {
  float: left;
  text-align: justify;
  padding-right: 25px;
}

.catatan3 {
  float: left;
  text-align: justify;
  padding-right: 25px;
}

.copyright {
  background-color: #222222;
  height: 30px;
  line-height: 30px;
}

.copyright li{
  display: inline-block;
  color: #fff;
  font-size: 14px;
}



/* Tata Letak */

.kiri {
  float: left;
}

.kanan {
  float: right;
}

.tengah {
  left:500px;
}

.padding-kanan {
  padding-right: 80px;
}

.padding-kiri {
  padding-left:80px;
}

#potongan {
  color: #acacac;
  text-decoration: line-through;
}

.play{background: url('../images/play.png') ;}
.pause{background: url('../images/pause.png') ;}

.player {
  padding-top: 25px;
}

#audioplayer{
  width: 350px;
  height: 75px;
  margin: 25px auto auto auto;
  border: 1px solid #fac7d2;
  border-radius: 11px;
  background: #fac7d2;
}

#audioplayer h3{
  font-family: 'pacifico';
  font-size: 20px;
}

#pbutton{
  height:80px; 
  width: 80px;
  border: none;
  background-size: 50% 50%;
  background-repeat: no-repeat;
  background-position: center;
  float:left;
  outline:none;
}

#pbutton2{
  height:60px; 
  width: 60px;
  border: none;
  background-size: 50% 50%;
  background-repeat: no-repeat;
  background-position: center;
  float:left;
  outline:none;
}

/* CSS Untuk Slideshow */

.slideshow {
  padding-top: 25px;
}

#items {
  position : relative;
  width : 800px;
  height : 200px;
  float:left;
}
.item {
  position : absolute;
  background-color : #eee;
  border : 1px solid #ccc;
  width : 800px;
  height : 198px;
  display :none;
  text-align : center;
  font-size : 72px;
  display: inline-block;
}

.item img{
  text-align: center;
  display: block;
  max-width:398px;
  max-height: 198px;
  width: auto;
  height: auto;
  margin: auto;
}

.first{
  display : block;
}
#controls {

}
.items li {
  display : inline-block;
  padding : 5px;
  border : 1px solid #ccc;
  background-color : #eee;
  cursor : pointer;
}
#play {
  display : none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<body>
  <div id='items'>
    <ul>
      <li id="1" class='item first'><img src="http://cepamagz.com/wp-content/uploads/2014/11/raisa.jpg"/></li>
      <li id="2" class='item'><img src="http://i.ytimg.com/vi/9rXJ2WZ-auY/maxresdefault.jpg"/></li>
      <li id="3" class='item'><img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRxGdx2n52WRuzD2e0cTcpm3_pbSvtNQcIhxEZHtQgaZwgnUrvH"/></li>
    </ul>
  </div>
  <ul id='controls'>
    <button id='prev'>Prev</button>
    <button id='play'>Play</button>
    <button id='pause'>Pause</button>
    <button id='next'>Next</button>
  </ul>
    <a href="1">1</a>
    <a href="2">2</a>
    <a href="3">3</a>
</body>