Scroll down when I click

1

I want the scroll to go to the bottom of the site (or at least 100px) when I click on the calendar, to be able to see the reservation options.

I'm using the following code, but it does not do anything and in the Chrome debugger it does not show me any errors. Any ideas? I'm putting the code in the header.php (Wordpress)

Site: link

$('.ui-state-default').click(function () {
   $('html, body').animate({
       scrollTop: $(document).height()
   }, 'slow');
   return false;
});
    
asked by Gaston Coroleu 13.12.2016 в 19:37
source

2 answers

1
  

Error:

     

When you include it in header.php your script is running when the page has not finished loading completely and the element with class .ui-state-default does not yet exist.

Solution:

You've used $(function(){}) so that your script runs in onload .

For example:

$(function(){
  $('.ui-state-default').click(function () {
    $('html, body').animate({
       scrollTop: $(document).height()
    }, 'slow');
    return false;
  });
});
.ui-state-default {
  position: absolute;
  top: 20px;
  left: 50%;
  padding: 5px 10px;
  background: #f00;
  color: #fff;
  cursor: pointer
}
.ui-state-default:hover {
  text-decoration: none;
  background: #f99;
  color: #fff;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <a class="ui-state-default">Ir al fondo</a>
    <div class="container">

      <div class="blog-header">
        <h1 class="blog-title">The Bootstrap Blog</h1>
        <p class="lead blog-description">The official example template of creating a blog with Bootstrap.</p>
      </div>

      <div class="row">

        <div class="col-sm-8 blog-main">

          <div class="blog-post">
            <h2 class="blog-post-title">Sample blog post</h2>
            <p class="blog-post-meta">January 1, 2014 by <a href="#">Mark</a></p>

            <p>This blog post shows a few different types of content that's supported and styled with Bootstrap. Basic typography, images, and code are all supported.</p>
            <hr>
            <p>Cum sociis natoque penatibus et magnis <a href="#">dis parturient montes</a>, nascetur ridiculus mus. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Sed posuere consectetur est at lobortis. Cras mattis consectetur purus sit amet fermentum.</p>
            <blockquote>
              <p>Curabitur blandit tempus porttitor. <strong>Nullam quis risus eget urna mollis</strong> ornare vel eu leo. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
            </blockquote>
            <p>Etiam porta <em>sem malesuada magna</em> mollis euismod. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur.</p>
            <h2>Heading</h2>
            <p>Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
          </div><!-- /.blog-post -->

        </div><!-- /.blog-main -->

        <div class="col-sm-3 col-sm-offset-1 blog-sidebar">
        </div><!-- /.blog-sidebar -->

      </div><!-- /.row -->

    </div><!-- /.container -->

    <footer class="blog-footer">
      <p>Blog template built for <a href="http://getbootstrap.com">Bootstrap</a> by <a href="https://twitter.com/mdo">@mdo</a>.</p>
    </footer>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
answered by 13.12.2016 / 20:00
source
0

I leave this code

$(function () {
            $('#verMas').bind("click", function () {
                var dest = $("#one").offset().top;
                $("html, body").animate({scrollTop: dest},600);
            });

        });

"# verMas" is the id of the button where you click "#one" is the div you want to reach

    
answered by 13.12.2016 в 19:43