简体   繁体   中英

scrollTo plugin doesn't seem to identify div

The following is my code. I am trying to scroll from once the submit button is clicked to new-products . However , the below script doesn't seem to work.

<input type="submit" value="Get Ratings" class="main-search-submit" id="go">
    </div>
    <div id="new-products">

    </div>
    <script>
        $(document).ready(function(){  
      $(".main-search-submit").click(function() {
        $.scrollTo($("#new-products"), { duration: 0});
      });
    </script>
    <!-- Javascipt Libraries-->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/1.4.11/jquery.scrollTo.min.js"></script>
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>

Also css of my new-products class for test purposes is :

#new-products {
    height: 2000px;

}

Am I doing something wrong ?

EDIT: Console shows ->

index.html:7 GET file://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css net::ERR_FILE_NOT_FOUND
index.html:9 GET file://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js net::ERR_FILE_NOT_FOUND
index.html:22 GET file://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js net::ERR_FILE_NOT_FOUND
index.html:23 GET file://cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/1.4.11/jquery.scrollTo.min.js net::ERR_FILE_NOT_FOUND
index.html:24 GET file://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js net::ERR_FILE_NOT_FOUND
index.html:26 Uncaught ReferenceError: $ is not defined

UPD 1: Adding http: before links helped eliminate console errors. But the div still doesn't scroll .

Your script has to go after jQuery and the plugins, otherwise jQuery is not available (nor are the plugins)

<input type="submit" value="Get Ratings" class="main-search-submit" id="go">

<div id="new-products"></div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/1.4.11/jquery.scrollTo.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>

<script type="text/javascript">

      $(document).ready(function(){  
          $(".main-search-submit").click(function() {
              $.scrollTo($("#new-products"), { duration: 0});
          });
      });

</script>

Also, you forgot to close the document.ready function !

DEMO

 $(document).ready(function(){  
    $(document).on('click',".main-search-submit",function() {
       $('body').animate({scrollTop : $("#new-products").offset().top}, 100);
    });
 });

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