user3632882
user3632882

Reputation: 51

jQuery - Animate scroll to section page

I have been working on my personal site, but had a problem with the menu. Web site in progress

Using jQuery I have tried to animate the main menu of the site, it is assumed that when clicking on one of the links, the contents of the main container should be moved to the corresponding section, but this is not happening, it only links correctly when clicking In one of the links for the first time, then everything fails.

I know the problem is with jQuery, but I'm really new to this library and I can not identify the problem.

I would appreciate your help.

$(document).ready(function() {
    $('.scroll-to').on('click', function(event){
      event.preventDefault();
      var sectionID = $(this).attr("data-id");
      scrollToID('#' + sectionID, 750);
    });
});
// scroll function
function scrollToID(id, speed){
    var offSet = 150;
    var targetOffset = $(id).offset().top - offSet;
    $('.main_content, .principal_content').animate({scrollTop:targetOffset}, speed);
}
if (typeof console === "undefined") {
    console = {
        log: function() { }
    };
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="css/resume.css" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400" rel="stylesheet">
  <script src="https://use.fontawesome.com/0d6e13b87e.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  <script lenguage="javascript" src="js/script.js" type="text/javascript"></script>-->
  <title>Jorge Cortés - Front-end Development</title>
</head>
<body>
  <div class="main_content">
    <header class="header_content">
    </header>

    <nav class="navigation_content">
      <div class="profile_card">
        <div class="profile_img">
          <img src="img/image.png" alt="Jorge">
        </div>
      </div>
      <ul class="menu">
        <li><a class="scroll-to" data-id="about_me" href="#about_me"><i class="icon fa fa-user-circle"></i>About me</a></li>
        <li><a class="scroll-to" data-id="experience" href="#experience"><i class="icon fa fa-briefcase"></i>Experience</a></li>
        <li><a class="scroll-to" data-id="education" href="#education"><i class="icon fa fa-graduation-cap"></i>Education</a></li>
        <li><a class="scroll-to" data-id="skill" href="#skill"><i class="icon fa fa-grav"></i>Skills</a></li>
      </ul>
    </nav>

    <main class="principal_content">
      <section class="about_me" id="about_me">
        <h1>Hello, I am <span>Jorge</span><br> Web Designer and Front-end</h1>
        <div class="separator_light"></div>
        <p>I have two years experience as a web/interface developer.
           I have a taste <br> of clean, elegant styling, and I have lots of experience in the
           production of HTML <br> and CSS code for websites.
        </p>
        <!--<p>Futhermore I have experience using JS libraries and writing JS code,
           and I have a reasonable grasp of using CMS, specifically Joomla and WordPress.
        </p>-->

        <h2>Personal Info</h2>
        <div class="separator_bold"></div>
        <ul class="personal_info">
          <li><em>Name</em><span>Jorge Cortés Álvarez</span></li>
          <li><em>Date of Birth</em><span>May 26, 1993</span></li>
          <li><em>e-mail</em><span>[email protected]</span></li>
          <li><em>Phone</em><span>(+57) 300 433 8040</span></li>
          <li><em>Address</em><span>Cartagena de Indias - Colombia</span></li>
        </ul>
      </section>

      <section class="experience" id="experience">
        <h2>Work History</h2>
        <div class="separator_bold"></div>
        <h3>Platform Administrator OJS</h3>
        <h4><i class="fa fa-thumb-tack"></i>Palobra Journal - University of Cartagena <br>
            <i class="fa fa-calendar"></i>2015 - 2016</h4>
        <p>Customization of the user's interface, configuration and support
          of the platform Open Journal Systems (OJS) for the virtual publication
          of the Palobra Journal in the University of Cartagena.
        </p>

        <h3>Front-end Web Developer</h3>
        <h4><i class="fa fa-thumb-tack"></i>Freelance/Self-Employed <br>
            <i class="fa fa-calendar"></i>2014 - Today</h4>
        <p>Experience in the design and development of web sites using
          techonologies as HMTL5, CSS3, JS and content management systems
          as Joomla and WordPress.
        </p>
      </section>

      <section class="education" id="education">
        <h2>Academic History</h2>
        <div class="separator_bold"></div>
        <h3>Systems Engineer</h3>
        <h4><i class="fa fa-graduation-cap"></i>Bachelor's degree - University of Cartagena <br>
            <i class="fa fa-calendar-o"></i>2016</h4>
        <p></p>
        <h3>Secondary Education</h3>
        <h4><i class="fa fa-graduation-cap"></i>High School degree - Institute Promoción Social <br>
            <i class="fa fa-calendar-o"></i>2009</h4>
      </section>
      <section class="skill" id="skill">
        <h2>Development Skills</h2>
        <div class="separator_bold"></div>
        <ul>
          <li><p>HTML5</p><div class="skill_bar"><span style="width: 50%;"></span></div></li>
          <li><p>CSS3</p><div class="skill_bar"><span style="width: 50%;"></span></div></li>
          <li><p>JavaScript</p><div class="skill_bar"><span style="width: 50%;"></span></div></li>
          <li><p>UX/UI</p><div class="skill_bar"><span style="width: 50%;"></span></div></li>
          <li><p>Usability Testing</p><div class="skill_bar"><span style="width: 50%;"></span></div></li>
        </ul>

      </section>
    </main>
    <div class="social_media">
      <p>© 2017 Jorge Cortés Álvarez. All Rights Reserved. </p>
      <a href="https://www.instagram.com/jorgecortesalvarez/" target="_blank"><i class="fa fa-instagram"></i></a>
      <a href="https://twitter.com/JorgeCortesR" target="_blank"><i class="fa fa-twitter"></i></a>
      <a href="https://www.linkedin.com/in/jorgecortesalvarez/" target="_blank"><i class="fa fa-linkedin"></i></a>
    </div>
  </div>
</body>
</html>

Upvotes: 0

Views: 1154

Answers (2)

ElChiniNet
ElChiniNet

Reputation: 2902

Some styles are missing in your code, I've changed a bit your code and I've also added some styles to make the principal_content div scrollable. Your code works just the first time because the value of the scroll in that moment is 0, you need to add the value of the current scroll value to your calculation, this is the formula:

section offset top - container offset top + scrollTop value of the container

Here is your code working, but here you have a jsfiddle example that is more concise:

$(document).ready(function() {
  
    var cont = $('.main_content .principal_content');
    var contOffset = cont.offset();
  
    // scroll function
    function scrollToID(id, speed){
        var targetOffset = $(id).offset().top - contOffset.top + cont.scrollTop();
        cont.animate({scrollTop: targetOffset}, speed);
    }
  
    $('.scroll-to').on('click', function(event){
        event.preventDefault();
        var sectionID = $(this).attr("data-id");
        scrollToID('#' + sectionID, 750);
    });
});
html, body {
    height: 100%;
}

body {
    margin: 0;
    padding: 0;
}

.menu {
    background: #CCC;
    height: 25px;
    margin: 0;
    padding: 0;
}

.menu li {
    display: inline-block;
}

.main_content {
    height: 100%;
}

.principal_content {
    height: 100%;
    overflow-y: auto;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Jorge Cortés - Front-end Development</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400" rel="stylesheet">
    <script src="https://use.fontawesome.com/0d6e13b87e.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script lenguage="javascript" src="js/script.js" type="text/javascript"></script>      
</head>
<body>
    <div class="main_content">

        <nav class="navigation_content">
            <ul class="menu">
                <li><a class="scroll-to" data-id="about_me" href="#about_me"><i class="icon fa fa-user-circle"></i>About me</a></li>
                <li><a class="scroll-to" data-id="experience" href="#experience"><i class="icon fa fa-briefcase"></i>Experience</a></li>
                <li><a class="scroll-to" data-id="education" href="#education"><i class="icon fa fa-graduation-cap"></i>Education</a></li>
                <li><a class="scroll-to" data-id="skill" href="#skill"><i class="icon fa fa-grav"></i>Skills</a></li>
            </ul>
        </nav>

        <main class="principal_content">
            <section class="about_me" id="about_me">
                <h1>Hello, I am <span>Jorge</span><br> Web Designer and Front-end</h1>
                <div class="separator_light"></div>
                <p>I have two years experience as a web/interface developer.
           I have a taste <br> of clean, elegant styling, and I have lots of experience in the production of HTML <br> and CSS code for websites.
                </p>
                <h2>Personal Info</h2>
                <div class="separator_bold"></div>
                <ul class="personal_info">
                    <li><em>Name</em><span>Jorge Cortés Álvarez</span></li>
                    <li><em>Date of Birth</em><span>May 26, 1993</span></li>
                    <li><em>e-mail</em><span>[email protected]</span></li>
                    <li><em>Phone</em><span>(+57) 300 433 8040</span></li>
                    <li><em>Address</em><span>Cartagena de Indias - Colombia</span></li>
                </ul>
            </section>

            <section class="experience" id="experience">
                <h2>Work History</h2>
                <div class="separator_bold"></div>
                <h3>Platform Administrator OJS</h3>
                <h4><i class="fa fa-thumb-tack"></i>Palobra Journal - University of Cartagena<br><i class="fa fa-calendar"></i>2015 - 2016</h4>
                <p>Customization of the user's interface, configuration and support of the platform Open Journal Systems (OJS) for the virtual publication of the Palobra Journal in the University of Cartagena.
                </p>
                <h3>Front-end Web Developer</h3>
                <h4><i class="fa fa-thumb-tack"></i>Freelance/Self-Employed<br><i class="fa fa-calendar"></i>2014 - Today</h4>
                <p>Experience in the design and development of web sites using techonologies as HMTL5, CSS3, JS and content management systems as Joomla and WordPress.
                </p>
            </section>

            <section class="education" id="education">
                <h2>Academic History</h2>
                <div class="separator_bold"></div>
                <h3>Systems Engineer</h3>
                <h4><i class="fa fa-graduation-cap"></i>Bachelor's degree - University of Cartagena<br><i class="fa fa-calendar-o"></i>2016</h4>
                <p></p>
                <h3>Secondary Education</h3>
                <h4><i class="fa fa-graduation-cap"></i>High School degree - Institute Promoción Social<br><i class="fa fa-calendar-o"></i>2009</h4>
            </section>

            <section class="skill" id="skill">
                <h2>Development Skills</h2>
                <div class="separator_bold"></div>
                <ul>
                    <li><p>HTML5</p><div class="skill_bar"><span style="width: 50%;"></span></div></li>
                    <li><p>CSS3</p><div class="skill_bar"><span style="width: 50%;"></span></div></li>
                    <li><p>JavaScript</p><div class="skill_bar"><span style="width: 50%;"></span></div></li>
                    <li><p>UX/UI</p><div class="skill_bar"><span style="width: 50%;"></span></div></li>
                    <li><p>Usability Testing</p><div class="skill_bar"><span style="width: 50%;"></span></div></li>
                </ul>
            </section>

        </main>

        <div class="social_media">
            <p>© 2017 Jorge Cortés Álvarez. All Rights Reserved. </p>
            <a href="https://www.instagram.com/jorgecortesalvarez/" target="_blank"><i class="fa fa-instagram"></i></a>
            <a href="https://twitter.com/JorgeCortesR" target="_blank"><i class="fa fa-twitter"></i></a>
            <a href="https://www.linkedin.com/in/jorgecortesalvarez/" target="_blank"><i class="fa fa-linkedin"></i></a>
        </div>

    </div>

</body>
</html>

Upvotes: 1

Hajji Tarik
Hajji Tarik

Reputation: 1082

use the $(body) instead of $('.main_content, .principal_content') try this:

$(document).ready(function() {
       $('.scroll-to').on('click', function(event){
         event.preventDefault();
         var sectionID = $(this).attr("data-id");
         scrollToID('#' + sectionID, 750);
       });
       function scrollToID(id, speed){
              var offSet = 150;
              var targetOffset = $(id).offset().top - offSet;

              $('body').stop().animate({scrollTop:targetOffset},speed, 'swing');
          }
          if (typeof console === "undefined") {
              console = {
                  log: function() { }
              };
          }
    });

Upvotes: 0

Related Questions