Ivan Ivan - 2 months ago 5
CSS Question

Why is my animation firing more then once on scroll?

How do I stop the animation after it animated once on scroll? Please explain why it's animating more then once and how to fix it! I'm very new to javascript/jquery so please make explanations simple. Much appreciated!



$(document).ready(function(){

//MENU
$('.menu-icon').click(function(){
$('.menu-nav').animate({
height: 'toggle'
}, 200
);
});


//SKILLS
$(window).scroll(function() {
if ($(this).scrollTop() > 40) {
// apply effects and animations
$('.bar').animate({
width: 'toggle'
}, 2000
);
}
});

});

/*
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com
Twitter: @rich_clark
*/
html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
/*background:transparent;*/
}

body {
line-height: 1;
}

article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}

nav ul {
list-style: none;
}

blockquote, q {
quotes: none;
}

blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}

a {
margin: 0;
padding: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}

header {
background-color: #37474F;
}

header .menu-icon {
padding: 5px 0 5px 10px;
cursor: pointer;
width: 50px;
}

header .menu-icon img {
width: 50px;
height: 50px;
}

header .menu {
width: 100%;
border-bottom: solid #18FFFF 1px;
border-width: 70%;
}

header .menu:before {
content: " ";
display: block;
width: 90%;
height: 5px;
position: absolute;
margin: 0 auto;
left: 0;
right: 0;
background-color: #18FFFF;
}

header .menu .menu-nav {
z-index: 2;
position: absolute;
background-color: #18FFFF;
width: 90%;
margin: 0 auto;
left: 0;
right: 0;
border-bottom-radius: 5px;
display: none;
}

header .menu .menu-nav ul {
margin-top: 20px;
}

header .menu .menu-nav ul li {
text-align: center;
padding: 10px;
list-style: none;
border-radius: 5x;
font-size: 2em;
cursor: pointer;
}

header .menu .menu-nav ul li:hover {
/*background-color: #607D8B;*/
background-color: #37474F;
color: #18FFFF;
}

header .menu h1 {
text-align: center;
padding-top: 20px;
}

header .menu .menu-social {
width: 170px;
margin: 100px auto;
margin-bottom: 20px;
}

header .menu .menu-social img {
cursor: pointer;
margin: 0 1px 0 1px;
}

header .menu .menu-social img:hover {
border: solid #37474F 1px;
}

header .bio {
width: 70%;
margin: 20px auto;
margin-bottom: 0;
}

header .bio .hello h1 {
font-size: 4em;
color: #18FFFF;
text-align: center;
padding-bottom: 20px;
}

header .bio .desc p {
color: white;
text-align: center;
line-height: 25px;
}

header .bio .desc .skills {
padding-top: 20px;
padding-bottom: 80px;
}

.skills-2 {
width: 100%;
background-color: #18FFFF;
overflow: hidden;
}

.skills-2 h1 {
text-align: center;
font-size: 2em;
}

.skills-2 .my-skills {
width: 40%;
padding-top: 20px;
float: left;
}

.skills-2 .my-skills ul li {
text-align: center;
}

.skills-2 .graph {
float: left;
width: 60%;
padding-top: 20px;
}

.skills-2 .graph .html {
width: 100px;
}

.skills-2 .graph .css {
width: 70px;
}

.skills-2 .graph .javascript {
width: 50px;
}

.skills-2 .graph .bar {
height: 10px;
background-color: red;
margin-bottom: 9px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div class="wrapper">
<header>
<div class="menu-icon">
<img src="images/Menu-64.png">
</div><!--menu-icon-->
<div class="menu">
<div class="menu-nav">
<ul>
<li>Home</li>
<li>Work</li>
<li>Contact</li>
</ul>

<div class="menu-social">
<img src="images/Instagram.png" alt="instagram" />
<img src="images/Twitter.png" alt="twitter" />
<img src="images/LinkedIn.png" alt="linkedin" />
</div>
</div><!--menu-nav-->
</div>

<div class="bio">

<div class="hello">
<h1>Hello...</h>
</div><!--Hello-->

<div class="desc">
<p>Lorem ipsum dolor sit amet, gravida urna cras a iaculis rhoncus. Nunc arcu eget nec posuere quam duis, euismod nunc cras fermentum, nibh pellentesque</p>
<div class="skills">
<p>Lorem ipsum dolor sit amet, gravida urna cras a iaculis rhoncus.</p>
</div><!--Skills-->
</div><!--Desc-->

</div><!--Bio-->
</header>

<div class="skills-2">
<h1>Skills</h1>
<div class="my-skills">
<ul>
<li>Lorem ipsum</li>
<li>Lorem ipsum</li>
<li>Lorem ipsum</li>
</ul>
</div><!--My SKills-->
<div class="graph">
<div class="html bar">
</div>
<div class="css bar">
</div>
<div class="javascript bar">
</div>
</div><!--Graph-->
</div><!--SKills 2-->

</div><!--Wrapper-->
</body>




Answer

$(document).ready(function(){

//MENU
    $('.menu-icon').click(function(){
       $('.menu-nav').animate({
            height: 'toggle'
          }, 200
       );
  });


//SKILLS
  flagScroll = true;
  $(window).scroll(function() {
    if ($(this).scrollTop() > 40 && flagScroll) {
        // apply effects and animations
        flagScroll= false;
        $('.bar').animate({
          width: 'toggle'
        }, 2000
      );
    }
});

});
/*
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com
Twitter: @rich_clark
*/
html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
  outline: 0;
  font-size: 100%;
  vertical-align: baseline;
  /*background:transparent;*/
}

body {
  line-height: 1;
}

article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
  display: block;
}

nav ul {
  list-style: none;
}

blockquote, q {
  quotes: none;
}

blockquote:before, blockquote:after,
q:before, q:after {
  content: '';
  content: none;
}

a {
  margin: 0;
  padding: 0;
  font-size: 100%;
  vertical-align: baseline;
  background: transparent;
}

header {
  background-color: #37474F;
}

header .menu-icon {
  padding: 5px 0 5px 10px;
  cursor: pointer;
  width: 50px;
}

header .menu-icon img {
  width: 50px;
  height: 50px;
}

header .menu {
  width: 100%;
  border-bottom: solid #18FFFF 1px;
  border-width: 70%;
}

header .menu:before {
  content: " ";
  display: block;
  width: 90%;
  height: 5px;
  position: absolute;
  margin: 0 auto;
  left: 0;
  right: 0;
  background-color: #18FFFF;
}

header .menu .menu-nav {
  z-index: 2;
  position: absolute;
  background-color: #18FFFF;
  width: 90%;
  margin: 0 auto;
  left: 0;
  right: 0;
  border-bottom-radius: 5px;
  display: none;
}

header .menu .menu-nav ul {
  margin-top: 20px;
}

header .menu .menu-nav ul li {
  text-align: center;
  padding: 10px;
  list-style: none;
  border-radius: 5x;
  font-size: 2em;
  cursor: pointer;
}

header .menu .menu-nav ul li:hover {
  /*background-color: #607D8B;*/
  background-color: #37474F;
  color: #18FFFF;
}

header .menu h1 {
  text-align: center;
  padding-top: 20px;
}

header .menu .menu-social {
  width: 170px;
  margin: 100px auto;
  margin-bottom: 20px;
}

header .menu .menu-social img {
  cursor: pointer;
  margin: 0 1px 0 1px;
}

header .menu .menu-social img:hover {
  border: solid #37474F 1px;
}

header .bio {
  width: 70%;
  margin: 20px auto;
  margin-bottom: 0;
}

header .bio .hello h1 {
  font-size: 4em;
  color: #18FFFF;
  text-align: center;
  padding-bottom: 20px;
}

header .bio .desc p {
  color: white;
  text-align: center;
  line-height: 25px;
}

header .bio .desc .skills {
  padding-top: 20px;
  padding-bottom: 80px;
}

.skills-2 {
  width: 100%;
  background-color: #18FFFF;
  overflow: hidden;
}

.skills-2 h1 {
  text-align: center;
  font-size: 2em;
}

.skills-2 .my-skills {
  width: 40%;
  padding-top: 20px;
  float: left;
}

.skills-2 .my-skills ul li {
  text-align: center;
}

.skills-2 .graph {
  float: left;
  width: 60%;
  padding-top: 20px;
}

.skills-2 .graph .html {
  width: 100px;
}

.skills-2 .graph .css {
  width: 70px;
}

.skills-2 .graph .javascript {
  width: 50px;
}

.skills-2 .graph .bar {
  height: 10px;
  background-color: red;
  margin-bottom: 9px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <body>
    <div class="wrapper">
      <header>
        <div class="menu-icon">
          <img src="images/Menu-64.png">
        </div><!--menu-icon-->
        <div class="menu">
          <div class="menu-nav">
            <ul>
              <li>Home</li>
              <li>Work</li>
              <li>Contact</li>
            </ul>

            <div class="menu-social">
              <img src="images/Instagram.png" alt="instagram" />
              <img src="images/Twitter.png" alt="twitter" />
              <img src="images/LinkedIn.png" alt="linkedin" />
            </div>
          </div><!--menu-nav-->
        </div>

        <div class="bio">

          <div class="hello">
            <h1>Hello...</h>
          </div><!--Hello-->

          <div class="desc">
            <p>Lorem ipsum dolor sit amet, gravida urna cras a iaculis rhoncus. Nunc arcu eget nec posuere quam duis, euismod nunc cras fermentum, nibh pellentesque</p>
            <div class="skills">
               <p>Lorem ipsum dolor sit amet, gravida urna cras a iaculis rhoncus.</p>
            </div><!--Skills-->
          </div><!--Desc-->

        </div><!--Bio-->
      </header>

      <div class="skills-2">
        <h1>Skills</h1>
        <div class="my-skills">
          <ul>
            <li>Lorem ipsum</li>
            <li>Lorem ipsum</li>
            <li>Lorem ipsum</li>
          </ul>
        </div><!--My SKills-->
        <div class="graph">
          <div class="html bar">
          </div>
          <div class="css bar">
          </div>
          <div class="javascript bar">
          </div>
      </div><!--Graph-->
      </div><!--SKills 2-->

    </div><!--Wrapper-->
  </body>

Does this work for you?