简体   繁体   中英

2nd jQuery “scrollTop” Doesn't Work

Well I can't figure this issue out. Here's the problem.

I have scrollTop Jquery on page load, which after delay scrolls page down for user to take action. But after clicking the button "#b3,#b3b", i can't get another scrollTop to work and after delay scroll to the bottom of page.

It should scroll to .comp element at the bottom, but it doesn't or scrolls just a little bit down.

Can someone tell me how can i get the page to scroll to bottom element 2nd time after clicking the button?

 $(document).ready(function () { $('html, body').delay(1500).animate({ scrollTop: $('.button').offset().top }, 'slow'); $("#b1,#b1b").click(function(){ $("#a1").fadeOut('fast'),$("#a2").fadeIn('slow'); }); $("#b2,#b2b").click(function(){ $("#a2").fadeOut('fast'),$("#a3").fadeIn('slow'); }); $("#b3,#b3b").click(function(){ $("#a3").fadeOut('fast'),$("#a4").fadeIn('slow'); $("#bar").width(0); $("#bar").delay(150).animate({width: '+=40%'},'slow').delay(1200).animate({width: '+=20%'},'slow').delay(1000).animate({width: '+=20%'},'slow').delay(2000).animate({width: '+=20%'},'slow'); $('html, body').delay(10000).animate({ scrollTop: $('.comp').offset().top }, 'slow'); $('#r1').show(500).delay(2000).hide(500); $('#r2').delay(2000).show(500).delay(2000).hide(500); $('#r3').delay(4500).show(500).delay(2000).hide(500); $('#r4').delay(7000).show(500); $('#r5').delay(8500).show(500); $('#rb1').delay(2000).show(500); $('#rb2').delay(4500).show(500); $('#rb3').delay(7500).show(500); }); }); 
 * { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } p,h1,h2,h3,h4,h5{ padding-bottom: 1em; } body { background-color: #000; color: #FFF; } img { margin: 0px; padding: 0px; display: block; } #wrap { max-width: 1000px; margin: 10px; border: 1px solid #333; background-color: #292929; padding: 10px; border-radius: 8px; -moz-border-radius: 8px; -webkit-border-radius: 8px; } #header { font-family: Roboto, Helvetica, Arial, sans-serif; font-size: 1.6em; text-align: center; } #content { } #graphic { } #graphic img { width: 90%; height: auto; max-width: 400px; display: block; margin: auto; padding: 5px; border: 1px solid #525252; background-color: #3C3C3C; } #text { margin-top: 15px; } #text p { font-family: Arial, Helvetica, sans-serif; font-size: 0.9em; text-align: center; line-height: 1.2em; } #javascript_countdown_time { font-weight: bold; color: #F60; } .actionbox { border-top-width: 1px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #999; border-right-color: #999; border-bottom-color: #999; border-left-color: #999; padding-top: 20px; } .actionbox h2 { clear: both; display: block; font-family: Roboto, Helvetica, Arial, sans-serif; font-size: 1.3em; font-weight: 300; line-height: 1.1em; } .actionbox h2 strong { font-weight: bold; } .actionbox h3 { font-family: Helvetica, Roboto, Tahoma, sans-serif; font-weight: bold; font-size: 1em; color: #F90; } .actionbox h4 { text-align: center; font-size: 1em; font-weight: bold; font-family: Arial, Helvetica, sans-serif; padding-top: 30px; } .actionbox ul { margin-left: 10px; margin-top: 20px; } .actionbox ul li{ background-repeat: no-repeat; list-style-type: none; padding-left: 30px; background-size: 20px; padding-bottom: 1em; background-position: left top; padding-top: 3px; font-family: Arial, Helvetica, sans-serif; font-size: 0.9em; } .actionbox ol { margin-left: 30px; } .actionbox ol li { padding-bottom: 1em; font-family: Roboto, Helvetica, Arial, sans-serif; font-size: 0.9em; } .butbox { display: table; width: 100%; table-layout: fixed; } .butbox .button, .butbox .buttonr { display: table-cell; } .button a{ text-decoration: none; text-shadow: 1px 1px 0 rgba(0,0,0,0.3); background-color: #00CB00; background-image: -webkit-gradient(linear, left top, left bottom, from(#00CB00), to(#009A00)); background-image: -webkit-linear-gradient(top, #00CB00, #009A00); background-image: -moz-linear-gradient(top, #00CB00, #009A00); background-image: -ms-linear-gradient(top, #00CB00, #009A00); background-image: -o-linear-gradient(top, #00CB00, #009A00); background-image: linear-gradient(to bottom, #00CB00, #009A00); filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#00CB00, endColorstr=#009A00); display: block; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-width: 0px; border-right-width: 2px; border-bottom-width: 0px; border-left-width: 0px; border-top-color: #292929; border-right-color: #292929; border-bottom-color: #292929; border-left-color: #292929; -webkit-border-top-left-radius:5px; -moz-border-radius-topleft:5px; border-top-left-radius:5px; -webkit-border-top-right-radius:0px; -moz-border-radius-topright:0px; border-top-right-radius:0px; -webkit-border-bottom-right-radius:0px; -moz-border-radius-bottomright:0px; border-bottom-right-radius:0px; -webkit-border-bottom-left-radius:5px; -moz-border-radius-bottomleft:5px; border-bottom-left-radius:5px; } .button a:hover { background-color: #009800; background-image: -webkit-gradient(linear, left top, left bottom, from(#009800), to(#006700)); background-image: -webkit-linear-gradient(top, #009800, #006700); background-image: -moz-linear-gradient(top, #009800, #006700); background-image: -ms-linear-gradient(top, #009800, #006700); background-image: -o-linear-gradient(top, #009800, #006700); background-image: linear-gradient(to bottom, #009800, #006700);filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#009800, endColorstr=#006700); } .buttonr a { text-decoration: none; text-shadow: 1px 1px 0 rgba(0,0,0,0.3); background-color: #CC0000; background-image: -webkit-gradient(linear, left top, left bottom, from(#CC0000), to(#9b0000)); background-image: -webkit-linear-gradient(top, #CC0000, #9b0000); background-image: -moz-linear-gradient(top, #CC0000, #9b0000); background-image: -ms-linear-gradient(top, #CC0000, #9b0000); background-image: -o-linear-gradient(top, #CC0000, #9b0000); background-image: linear-gradient(to bottom, #CC0000, #9b0000); filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#CC0000, endColorstr=#9b0000); border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #292929; border-right-color: #292929; border-bottom-color: #292929; border-left-color: #292929; display: block; -webkit-border-top-left-radius:0px; -moz-border-radius-topleft:0px; border-top-left-radius:0px; -webkit-border-top-right-radius:5px; -moz-border-radius-topright:5px; border-top-right-radius:5px; -webkit-border-bottom-right-radius:5px; -moz-border-radius-bottomright:5px; border-bottom-right-radius:5px; -webkit-border-bottom-left-radius:0px; -moz-border-radius-bottomleft:0px; border-bottom-left-radius:0px; } .buttonr a:hover{ background-color: #990000; background-image: -webkit-gradient(linear, left top, left bottom, from(#990000), to(#680000)); background-image: -webkit-linear-gradient(top, #990000, #680000); background-image: -moz-linear-gradient(top, #990000, #680000); background-image: -ms-linear-gradient(top, #990000, #680000); background-image: -o-linear-gradient(top, #990000, #680000); background-image: linear-gradient(to bottom, #990000, #680000);filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#990000, endColorstr=#680000); } .buttxt a{ text-align: center; padding: 5px; font-family: Roboto, Helvetica, Arial, sans-serif; font-size: 2em; text-decoration: none; color: #FFF; } .buttonlast a { text-decoration: none; text-shadow: 1px 1px 0 rgba(0,0,0,0.3); background-color: #00CB00; background-image: -webkit-gradient(linear, left top, left bottom, from(#00CB00), to(#009A00)); background-image: -webkit-linear-gradient(top, #00CB00, #009A00); background-image: -moz-linear-gradient(top, #00CB00, #009A00); background-image: -ms-linear-gradient(top, #00CB00, #009A00); background-image: -o-linear-gradient(top, #00CB00, #009A00); background-image: linear-gradient(to bottom, #00CB00, #009A00); filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#00CB00, endColorstr=#009A00); display: block; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-width: 0px; border-right-width: 2px; border-bottom-width: 0px; border-left-width: 0px; border-top-color: #292929; border-right-color: #292929; border-bottom-color: #292929; border-left-color: #292929; border-radius: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px; margin-top: 10px; } .buttonlast a:hover { background-color: #009800; background-image: -webkit-gradient(linear, left top, left bottom, from(#009800), to(#006700)); background-image: -webkit-linear-gradient(top, #009800, #006700); background-image: -moz-linear-gradient(top, #009800, #006700); background-image: -ms-linear-gradient(top, #009800, #006700); background-image: -o-linear-gradient(top, #009800, #006700); background-image: linear-gradient(to bottom, #009800, #006700); filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#009800, endColorstr=#006700);} .comp { padding-top: 20px; padding-bottom: 20px; font-family: Arial, Helvetica, sans-serif; font-size: 0.7em; text-align: center; } #bar { background-color: #3C0; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; height: 30px; width: 100%; margin-bottom: 15px; } #percentage { font-family: Arial, Helvetica, sans-serif; font-size: 0.9em; text-align: center; line-height: 30px; } .orange { color: #F60; } #a2,#a3,#a4,#rb1,#r2,#rb2,#r3,#rb3,#r4,#r5 { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrap"> <div id="header"><h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. </h1> </div> <div id="content"> <div id="graphic"><img src="#" alt="" height="500px" width="300px"></div> <div id="text"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec.</p> <div class="actionbox" id="a3" style="display:block"> <h2><strong>Question 3:</strong></h2> <div class="butbox"> <div class="button buttxt" id="b3"><a href="#/">YES</a></div> <div class="buttonr buttxt" id="b3b"><a href="#/">NO</a></div> </div> </div> <div class="actionbox" id="a4"> <div id="bar"><div id="percentage"></div></div> <h3 id="r1">step1...</h3> <h3 id="r2">step2...</h3> <h3 id="r3">step3...</h3> <h3 id="r4">step4...</h3> <ul> <li id="rb1">xxx</li> <li id="rb2">xxx</li> <li id="rb3">xxx</li> </ul> <div id="r5" class="congrats"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec. <div class="buttonlast buttxt"><a href="#/">I AGREE</a></div> <div class="comp">xxxx</div> </div> </div> </div> </div> </div> 

Instead of trying to delay animations in absolute times, you can make them relative to one another by nesting functions in them that are executed when another animation has finished. That way you can also be relatively sure that the element to scroll to will be non-hidden when the right time arrives (which I think was your problem).

See this fiddle .

I botched the times, but it should give you an idea. The changed code would now look something like this:

$("#b3,#b3b").click(function(){
  $("#a3").fadeOut('fast'),$("#a4").fadeIn('slow');

  $("#bar").width(0);
  $('#r1').show(500);
  $("#bar").delay(150).animate({width: '+=40%'},2000, function() {
    $('#r1').hide(500);
    $('#r2').show(500);
    $('#rb1').show(500);
    $("#bar").animate({width: '+=20%'},2000, function() {
      $('#r2').hide(500);
      $('#r3').show(500);
      $('#rb2').show(500);
      $("#bar").animate({width: '+=20%'},2000, function() {
        $('#r3').hide(500);
        $('#r4').show(500);
        $('#rb3').show(500);
        $("#bar").animate({width: '+=20%'},2000, function() {
          $('#r4').show(500);
          $("#r5").show(); // show the element before you scroll to its child
          $('html, body').animate({
            scrollTop: $('.comp').offset().top
          }, 'slow');
        });
      });
    });
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM