Jquery内的加载条在100%后继续移动

时间:2015-10-17 18:22:05

标签: jquery

我试图在第4页开始加载栏,但它仍然超出100%的布局。如果我在第1页上取消了条形加载功能,它可以正常工作。也许我需要以某种方式关闭该动画功能?

http://jsfiddle.net/reizer/9w6vu7f1/



$('#cnt2, #cnt3, #cnt4, #cnt5').hide();
$('.button').click(function() {
//code
var curr = $(".question:visible");
var next = curr.next(".question");
next.delay(300).fadeIn(300);
curr.fadeOut(300);
if (!next.next(".question").length) {
  //Begin Bar Transition
  $(".button").hide(function() {
    $("#bar").width(0);
    $("#bar").delay(500).animate({
      width: '+=50%'
    }, 'slow').delay(600).animate({
      width: '+=20%'
    }, 'slow').delay(60).animate({
      width: '+=10%'
    }).delay(2000).animate({
      width: '+=20%'
    }, 'slow');
  });
}
});

* {
    font-family: Arial, Helvetica, sans-serif;
    margin: 0px;
    padding: 0px;
    border-top-style: none;
    border-right-style: none;
    border-bottom-style: none;
    border-left-style: none;
    list-style-type: none;
}
body {
    background-color: #333;
}
#wrapper {
    background-color: #666;
    margin: 1em;
    padding: 1em;
    border: 1px solid #757575;
    overflow: hidden;
}
.question {
    height: 300px;
    background-color: #FFF;
    padding-right: 1em;
    padding-left: 1em;
}
#table {
    display: table;
    width: 100%;
    margin-top: 0.5em;
}
#row {
    display: table-row;
}
.button {
    background: rgba(148, 190, 22, 1);
    background: -moz-linear-gradient(top, rgba(148, 190, 22, 1) 0%, rgba(118, 157, 0, 1) 100%);
    background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(148, 190, 22, 1)), color-stop(100%, rgba(118, 157, 0, 1)));
    background: -webkit-linear-gradient(top, rgba(148, 190, 22, 1) 0%, rgba(118, 157, 0, 1) 100%);
    background: -o-linear-gradient(top, rgba(148, 190, 22, 1) 0%, rgba(118, 157, 0, 1) 100%);
    background: -ms-linear-gradient(top, rgba(148, 190, 22, 1) 0%, rgba(118, 157, 0, 1) 100%);
    background: linear-gradient(to bottom, rgba(148, 190, 22, 1) 0%, rgba(118, 157, 0, 1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#94be16', endColorstr='#769d00', GradientType=0);
    cursor: pointer;
    text-align: center;
    display: table-cell;
    padding: 1em;
    color: #FFF;
}
.button:focus, .button:hover {
    background: rgba(168, 216, 22, 1);
    background: -moz-linear-gradient(top, rgba(168, 216, 22, 1) 0%, rgba(130, 173, 0, 1) 100%);
    background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(168, 216, 22, 1)), color-stop(100%, rgba(130, 173, 0, 1)));
    background: -webkit-linear-gradient(top, rgba(168, 216, 22, 1) 0%, rgba(130, 173, 0, 1) 100%);
    background: -o-linear-gradient(top, rgba(168, 216, 22, 1) 0%, rgba(130, 173, 0, 1) 100%);
    background: -ms-linear-gradient(top, rgba(168, 216, 22, 1) 0%, rgba(130, 173, 0, 1) 100%);
    background: linear-gradient(to bottom, rgba(168, 216, 22, 1) 0%, rgba(130, 173, 0, 1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#a8d816', endColorstr='#82ad00', GradientType=0);
}
.last {
    border-color:#666;
    border-style:solid;
    border-width:0 0 0 1px
}
#bar {
    background-color: #3C0;
    border-top-style: none;
    border-right-style: none;
    border-bottom-style: none;
    border-left-style: none;
    height: 30px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
  <div id="cnt" class="question">this is content of page 1
    <br>
  </div>
  <div id="cnt2" class="question">this is content of page 2</div>
  <div id="cnt3" class="question">this is content of page 3</div>
  <div id="cnt4" class="question">this is content of page 4
    <br>
    <div id="bar"></div>
  </div>
  <div id="table">
    <div id="row">
      <div class="button">CLICK HERE</div>
      <div class="button last">CLICK HERE</div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

由于$(".button")的匹配次数超过1次,因此传递给$('.button').hide();的回调会多次运行,因此会将栏扩展到100%以外

只需移动代码以在回调之外扩展进度条就可以解决问题:

&#13;
&#13;
$('#cnt2, #cnt3, #cnt4, #cnt5').hide();
$('.button').click(function() {
  //code
  var curr = $(".question:visible");
  var next = curr.next(".question");
  next.delay(300).fadeIn(300);
  curr.fadeOut(300);
  if (!next.next(".question").length) {
    //Begin Bar Transition
    $('.button').hide();
    $("#bar").width(0);
    $("#bar").delay(500).animate({
      width: '+=50%'
    }, 'slow').delay(600).animate({
      width: '+=20%'
    }, 'slow').delay(60).animate({
      width: '+=10%'
    }).delay(2000).animate({
      width: '+=20%'
    }, 'slow');
  }
});
&#13;
* {
  font-family: Arial, Helvetica, sans-serif;
  margin: 0px;
  padding: 0px;
  border-top-style: none;
  border-right-style: none;
  border-bottom-style: none;
  border-left-style: none;
  list-style-type: none;
}
body {
  background-color: #333;
}
#wrapper {
  background-color: #666;
  margin: 1em;
  padding: 1em;
  border: 1px solid #757575;
  overflow: hidden;
}
.question {
  height: 300px;
  background-color: #FFF;
  padding-right: 1em;
  padding-left: 1em;
}
#table {
  display: table;
  width: 100%;
  margin-top: 0.5em;
}
#row {
  display: table-row;
}
.button {
  background: rgba(148, 190, 22, 1);
  background: -moz-linear-gradient(top, rgba(148, 190, 22, 1) 0%, rgba(118, 157, 0, 1) 100%);
  background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(148, 190, 22, 1)), color-stop(100%, rgba(118, 157, 0, 1)));
  background: -webkit-linear-gradient(top, rgba(148, 190, 22, 1) 0%, rgba(118, 157, 0, 1) 100%);
  background: -o-linear-gradient(top, rgba(148, 190, 22, 1) 0%, rgba(118, 157, 0, 1) 100%);
  background: -ms-linear-gradient(top, rgba(148, 190, 22, 1) 0%, rgba(118, 157, 0, 1) 100%);
  background: linear-gradient(to bottom, rgba(148, 190, 22, 1) 0%, rgba(118, 157, 0, 1) 100%);
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#94be16', endColorstr='#769d00', GradientType=0);
  cursor: pointer;
  text-align: center;
  display: table-cell;
  padding: 1em;
  color: #FFF;
}
.button:focus,
.button:hover {
  background: rgba(168, 216, 22, 1);
  background: -moz-linear-gradient(top, rgba(168, 216, 22, 1) 0%, rgba(130, 173, 0, 1) 100%);
  background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(168, 216, 22, 1)), color-stop(100%, rgba(130, 173, 0, 1)));
  background: -webkit-linear-gradient(top, rgba(168, 216, 22, 1) 0%, rgba(130, 173, 0, 1) 100%);
  background: -o-linear-gradient(top, rgba(168, 216, 22, 1) 0%, rgba(130, 173, 0, 1) 100%);
  background: -ms-linear-gradient(top, rgba(168, 216, 22, 1) 0%, rgba(130, 173, 0, 1) 100%);
  background: linear-gradient(to bottom, rgba(168, 216, 22, 1) 0%, rgba(130, 173, 0, 1) 100%);
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#a8d816', endColorstr='#82ad00', GradientType=0);
}
.last {
  border-color: #666;
  border-style: solid;
  border-width: 0 0 0 1px
}
#bar {
  background-color: #3C0;
  border-top-style: none;
  border-right-style: none;
  border-bottom-style: none;
  border-left-style: none;
  height: 30px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
  <div id="cnt" class="question">this is content of page 1
    <br>
  </div>
  <div id="cnt2" class="question">this is content of page 2</div>
  <div id="cnt3" class="question">this is content of page 3</div>
  <div id="cnt4" class="question">this is content of page 4
    <br>
    <div id="bar"></div>
  </div>
  <div id="table">
    <div id="row">
      <div class="button">CLICK HERE</div>
      <div class="button last">CLICK HERE</div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;