javascript - 为什么我的动画在滚动时多次触发?

标签 javascript jquery html css

如何在滚动一次动画后停止动画?请解释为什么它的动画不止一次以及如何修复它!我对 javascript/jquery 很陌生,所以请简单解释一下。非常感谢!

$(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>

最佳答案

$(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>

这对你有用吗?

关于javascript - 为什么我的动画在滚动时多次触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39648178/

相关文章:

html - 脚本错误: 'c is not a function"

javascript - 如何删除 bootstrap tagsinput 中重复的标签字符

javascript - 滚动时更改 div 样式

javascript - 如何获取 iframe 的父元素(不仅仅是窗口)?

javascript - Android - 将javascript代码发送到WebView的内页

javascript - jQuery - 使用变量更改背景图像?

javascript - 选择带有某个孙子的项目。

html - 多级下拉菜单无法在悬停和 Bootstrap 上使用

jquery - 如何阻止 jQuery draggable 将其拖离屏幕?

javascript - 获取当前单击的相对于元素的坐标