幻灯片与计时器和指标jquery

时间:2016-10-28 02:54:41

标签: javascript jquery html css

您好我试图通过计时器进行幻灯片放映来更改幻灯片和手动更改幻灯片的图标。我有java中的图标代码,但我想在JQuery中完成整个事情,因为我对此更好(但仍然是一个衔铁)



var slideIndex = 1;
    showDivs(slideIndex);

    function plusDivs(n) {
      showDivs(slideIndex += n);
    }

    function currentDiv(n) {
      showDivs(slideIndex = n);
    }

    function showDivs(n) {
      var i;
      var x = document.getElementsByClassName("slide");
      var dots = document.getElementsByClassName("demo");
      if (n > x.length) {slideIndex = 1}
      if (n < 1) {slideIndex = x.length}
      for (i = 0; i < x.length; i++) {
         x[i].style.display = "none";
      }
      for (i = 0; i < dots.length; i++) {
         dots[i].className = dots[i].className.replace("Jdot", "");
      }
      x[slideIndex-1].style.display = "block";
      dots[slideIndex-1].className += "Jdot";

    }

$(document).ready(function() {

$('div.table1').fadeIn(2000);

var aboveHeight = $('title').outerHeight();


$(window).scroll(function(){

        if ($(window).scrollTop() > aboveHeight){

        $('div.nav').addClass('fixed').css('top','0').next()
        .css('padding-top','46px');

        } else {

        $('div.nav').removeClass('fixed').next()
        .css('padding-top','0');
        }
    });
});
&#13;
body {
    margin: 0 auto;
    padding: 0;
    background-color: #f5f5f5;
    background-image: url("images/ReStock.jpg");
    background-attachment: fixed;
    background-repeat: no-repeat;
    background-position:center;
    font-family:Arial;
    letter-spacing: .5;
}

li {
    float: left;
}

.link {
    color: #0073e6;
    text-decoration: none;
}

.link:hover {
    color: #3399ff;
    text-decoration: underline;
}

.title {
    text-align: center;
    font-family:Arial;
    color: #252525;
    font-size: 27px;
    height: 60;
    line-height: 2.5;
    letter-spacing: 4px;
}

.rx {
    height: 90;
}

.nav{
    position:relative;
    z-index: 3;
    display:block;
    width: 100%
}

.navbar{
    color:#1075ff;
    background-color: #404040;
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    font-family:Arial;
    letter-spacing: .5;
    position:relative;
    display:block;
    z-index: 3;
    width: 100%;
}  
.navitem {
    padding: 14px 16px;
    text-align: center;
    display: block;    
}

.navitem:hover {
    background-color:#0044aa;
    -o-transition:.3s;
    -ms-transition:.3s;
    -moz-transition:.3s;
    -webkit-transition:.3s;
}

.navitem:active {
    background-color: #ffb31a;
    color: #4d88ff;
    -o-transition:.0s;
    -ms-transition:.0s;
    -moz-transition:.0s;
    -webkit-transition:.0s;
}



.slideshow {
    height: 360px;
    width: 720px;
    margin:auto;
    position:relative;
}

.slide {
    max-width: 100%;
    height: auto;
    display:none;
    box-sizing: inherit;
    -webkit-animation-name: fade;
    -webkit-animation-duration: 1.5s;
    animation-name: fade;
    animation-duration: 1.5s;
}

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

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

.slidetext {
    position:absolute;
    font-size: 23px;
    margin: 15px;
}

.slideright {
    cursor: pointer;
    float: right;
    color: #fff;
    margin-bottom: 4px;
    -webkit-transition:background-color .3s,
        color .15s,
        box-shadow .3s,
        opacity 0.3s,
        filter 0.3s;
    transition:background-color .3s,
        color .15s,
        box-shadow .3s,
        opacity 0.3s,
        filter 0.3s
}

.slideleft {
    cursor: pointer;
    float: left;
    color: #fff
    -webkit-transition:background-color .3s,
        color .15s,
        box-shadow .3s,
        opacity 0.3s,
        filter 0.3s;
    transition:background-color .3s,
        color .15s,
        box-shadow .3s,
        opacity 0.3s,
        filter 0.3s
}

.slideleft:hover, .slideright:hover {
    color:#50aaff!important
}

.slideicon {
    text-align: center;
    font-size: 18px!important;
    color: #fff!important;
    position: absolute;
    left: 50%;
    bottom: 0;
    transform: translate(-50%,0%);
    -ms-transform: translate(-50%,0%);
    box-sizing: inherit;
}

.slidedot {
    cursor: pointer;
    background-color:transparent!important;
    color:#fff;
    display: inline-block;
    padding: 5px;
    text-align: center;
    border-radius: 50%;
    border:1px solid #ccc;
    margin-top: 7px;
    -webkit-transition:background-color .3s,
        color .15s,
        box-shadow .3s,
        opacity 0.3s,
        filter 0.3s;
    transition:background-color .3s,
        color .15s,
        box-shadow .3s,
        opacity 0.3s,
        filter 0.3s
}

.slidedot:hover {
    color:#000!important;
    background-color:#fff!important
}

.Jdot {
    color:#000!important;
    background-color:#fff!important;
}

.holder {
    width: 800;
    height: 5000;
    background-color: rgba(0,0,0,.6);
    padding: 50;


}

.fixed {
    position: fixed;
}

.addPatientPane {
    background-color: rgba(240,240,240, 1);
    width: 200px;
    height: 362px;
    border-radius: .75%;
    padding: 50px;
    margin-left: 30px;
}

.aboutPane {
    background-color: rgba(240,240,240, 1);
    width: 200px;
    height: 362px;
    border-radius: .75%;
    padding: 50px;
    margin-left: 30px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="title">       
                <span>Louis's Pharmacy<br></span>      
        </div>
        <div style="text-align: center;">
           <div class="nav">
            <ul class="navbar">
                <li class = "navitem"> Patient </li>
                <li class = "navitem">Place</li>
                <li class = "navitem">Holder</li>
                <li class = "navitem">Items</li>
                <li class = "navitem">About</li>
                
                <li class = "NavItem" style="float:right">&nbsp;Login&nbsp;</li>
            </ul>
            </div>
            <center>
               <div class="holder">
                   <div style="margin-top: 50px; display: none;" class="table1" >
                    <table >
                        <tr>
                            <td colspan="3">
                                <div class="slideshow">
                                    <div class="slide"><span class="slidetext">
                                        <span style="font-size: 35px;">Louis Pharmacy</span><br>
                                        Where you are always welcome!
                                        </span>
                                        <img src="Images/drugs.jpg">
                                    </div>
                                    <img class="slide" src="Images/Stock.jpg">
                                    <img class="slide" src="">
                                    <img class="slide" src="">
    
                                    <div class="slideicon" style="width:100%">
                                        <div class="slideleft" onclick="plusDivs(-1)">&nbsp;&nbsp;&#10094;</div>
                                        <div class="slideright" onclick="plusDivs(1)">&#10095;&nbsp;&nbsp;</div>
    
                                        <span class="slidedot demo " onclick="currentDiv(1)"></span>
    
                                        <span class="slidedot demo " onclick="currentDiv(2)"></span>
    
                                        <span class="slidedot demo " onclick="currentDiv(3)"></span>
    
                                        <span class="slidedot demo " onclick="currentDiv(4)"></span>
    
    
                                    </div>
    
    
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="3 style="height="30px"></td>
                        </tr>
                        <tr>
                            <td style="max-width: 280px;max-height: 400px;">
                                <div class="addPatientPane">
                                    Add a Patient *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* <a href="" class="link">... Sign up today!</a>
                                </div>
                            </td>
                            
                            <td style="width:20px"></td>
                            
                            <td>
                                <div class="aboutPane">
                                    About Us *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* <a href="" class="link">... Learn more!</a>
                                </div>
                            </td>
                        </tr>
    
    
    
    
                    </table>
                   </div>
                </div>
            </center>
        </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要做的就是使用setInterval()在计时器上循环浏览幻灯片。我建议在你的fadeIn之后立即这样做:

$('div.table1').fadeIn(2000, function() {
  setInterval(function() {
    plusDivs(1);
  }, 5000);
});

请参阅下面的代码段中的操作:

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function currentDiv(n) {
  showDivs(slideIndex = n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("slide");
  var dots = document.getElementsByClassName("demo");
  if (n > x.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = x.length
  }
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace("Jdot", "");
  }
  x[slideIndex - 1].style.display = "block";
  dots[slideIndex - 1].className += "Jdot";

}

$(document).ready(function() {

  $('div.table1').fadeIn(2000, function() {
    setInterval(function() {
      plusDivs(1);
    }, 5000);
  });

  var aboveHeight = $('title').outerHeight();


  $(window).scroll(function() {

    if ($(window).scrollTop() > aboveHeight) {

      $('div.nav').addClass('fixed').css('top', '0').next()
        .css('padding-top', '46px');

    } else {

      $('div.nav').removeClass('fixed').next()
        .css('padding-top', '0');
    }
  });
});
body {
  margin: 0 auto;
  padding: 0;
  background-color: #f5f5f5;
  background-image: url("images/ReStock.jpg");
  background-attachment: fixed;
  background-repeat: no-repeat;
  background-position: center;
  font-family: Arial;
  letter-spacing: .5;
}
li {
  float: left;
}
.link {
  color: #0073e6;
  text-decoration: none;
}
.link:hover {
  color: #3399ff;
  text-decoration: underline;
}
.title {
  text-align: center;
  font-family: Arial;
  color: #252525;
  font-size: 27px;
  height: 60;
  line-height: 2.5;
  letter-spacing: 4px;
}
.rx {
  height: 90;
}
.nav {
  position: relative;
  z-index: 3;
  display: block;
  width: 100%
}
.navbar {
  color: #1075ff;
  background-color: #404040;
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  font-family: Arial;
  letter-spacing: .5;
  position: relative;
  display: block;
  z-index: 3;
  width: 100%;
}
.navitem {
  padding: 14px 16px;
  text-align: center;
  display: block;
}
.navitem:hover {
  background-color: #0044aa;
  -o-transition: .3s;
  -ms-transition: .3s;
  -moz-transition: .3s;
  -webkit-transition: .3s;
}
.navitem:active {
  background-color: #ffb31a;
  color: #4d88ff;
  -o-transition: .0s;
  -ms-transition: .0s;
  -moz-transition: .0s;
  -webkit-transition: .0s;
}
.slideshow {
  height: 360px;
  width: 720px;
  margin: auto;
  position: relative;
}
.slide {
  max-width: 100%;
  height: auto;
  display: none;
  box-sizing: inherit;
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}
@-webkit-keyframes slide {
  from {
    opacity: .4
  }
  to {
    opacity: 1
  }
}
@keyframes slide {
  from {
    opacity: .4
  }
  to {
    opacity: 1
  }
}
.slidetext {
  position: absolute;
  font-size: 23px;
  margin: 15px;
}
.slideright {
  cursor: pointer;
  float: right;
  color: #fff;
  margin-bottom: 4px;
  -webkit-transition: background-color .3s, color .15s, box-shadow .3s, opacity 0.3s, filter 0.3s;
  transition: background-color .3s, color .15s, box-shadow .3s, opacity 0.3s, filter 0.3s
}
.slideleft {
  cursor: pointer;
  float: left;
  color: #fff -webkit-transition: background-color .3s, color .15s, box-shadow .3s, opacity 0.3s, filter 0.3s;
  transition: background-color .3s, color .15s, box-shadow .3s, opacity 0.3s, filter 0.3s
}
.slideleft:hover,
.slideright:hover {
  color: #50aaff!important
}
.slideicon {
  text-align: center;
  font-size: 18px!important;
  color: #fff!important;
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translate(-50%, 0%);
  -ms-transform: translate(-50%, 0%);
  box-sizing: inherit;
}
.slidedot {
  cursor: pointer;
  background-color: transparent!important;
  color: #fff;
  display: inline-block;
  padding: 5px;
  text-align: center;
  border-radius: 50%;
  border: 1px solid #ccc;
  margin-top: 7px;
  -webkit-transition: background-color .3s, color .15s, box-shadow .3s, opacity 0.3s, filter 0.3s;
  transition: background-color .3s, color .15s, box-shadow .3s, opacity 0.3s, filter 0.3s
}
.slidedot:hover {
  color: #000!important;
  background-color: #fff!important
}
.Jdot {
  color: #000!important;
  background-color: #fff!important;
}
.holder {
  width: 800;
  height: 5000;
  background-color: rgba(0, 0, 0, .6);
  padding: 50;
}
.fixed {
  position: fixed;
}
.addPatientPane {
  background-color: rgba(240, 240, 240, 1);
  width: 200px;
  height: 362px;
  border-radius: .75%;
  padding: 50px;
  margin-left: 30px;
}
.aboutPane {
  background-color: rgba(240, 240, 240, 1);
  width: 200px;
  height: 362px;
  border-radius: .75%;
  padding: 50px;
  margin-left: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="title">
  <span>Louis's Pharmacy<br></span> 
</div>
<div style="text-align: center;">
  <div class="nav">
    <ul class="navbar">
      <li class="navitem">Patient</li>
      <li class="navitem">Place</li>
      <li class="navitem">Holder</li>
      <li class="navitem">Items</li>
      <li class="navitem">About</li>

      <li class="NavItem" style="float:right">&nbsp;Login&nbsp;</li>
    </ul>
  </div>
  <center>
    <div class="holder">
      <div style="margin-top: 50px; display: none;" class="table1">
        <table>
          <tr>
            <td colspan="3">
              <div class="slideshow">
                <div class="slide"><span class="slidetext">
                                        <span style="font-size: 35px;">Louis Pharmacy</span>
                  <br>Where you are always welcome!
                  </span>
                  <img src="Images/drugs.jpg">
                </div>
                <img class="slide" src="Images/Stock.jpg">
                <img class="slide" src="">
                <img class="slide" src="">

                <div class="slideicon" style="width:100%">
                  <div class="slideleft" onclick="plusDivs(-1)">&nbsp;&nbsp;&#10094;</div>
                  <div class="slideright" onclick="plusDivs(1)">&#10095;&nbsp;&nbsp;</div>

                  <span class="slidedot demo " onclick="currentDiv(1)"></span>

                  <span class="slidedot demo " onclick="currentDiv(2)"></span>

                  <span class="slidedot demo " onclick="currentDiv(3)"></span>

                  <span class="slidedot demo " onclick="currentDiv(4)"></span>


                </div>


              </div>
            </td>
          </tr>
          <tr>
            <td colspan="3 style=" height="30px"></td>
          </tr>
          <tr>
            <td style="max-width: 280px;max-height: 400px;">
              <div class="addPatientPane">
                Add a Patient *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT*
                *SAMPLE_TEXT* <a href="" class="link">... Sign up today!</a>
              </div>
            </td>

            <td style="width:20px"></td>

            <td>
              <div class="aboutPane">
                About Us *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT* *SAMPLE_TEXT*
                <a href="" class="link">... Learn more!</a>
              </div>
            </td>
          </tr>




        </table>
      </div>
    </div>
  </center>
</div>