javascript - 滚动到另一个元素后从头开始动画

标签 javascript jquery html css

在我的页面上我有一些动画。当我滚动整个页面时,我想确保动画从头开始,而不是已经在我的页面上完成动画。 看一下这个: http://jsfiddle.net/ykto7uu9/49/

 $(document).ready(function(){
        $("button").click(function(){
            $("#animation").animate({left: '250px'});
        });
    });
html,body {
            height: 100%;
     }
    
    .first {
      background-color: green;
      height: 100%;
      width: 100%;
    }
    
    .second {
      background-color: yellow;
      height: 100%;
      width: 100%;
    }
    
    .third {
      background-color: red;
      height: 100%;
      width: 100%;
    }
    
    .button {
      position: absolute;
      margin-top: 50%;
    }
    
    .animation {
      background:#98bf21;
      height:100px;
      width:100px;
      position:absolute;
        margin-top: 20%;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="first" class="first"></div>
    <div id="second" class="second">
    <button id="button" class="button">Start Animation</button>
    Click on the button Start Animation!<br>
    Now every time I scroll up to green or red div, I want Block to be placed at the first position before animation.
    <div id="animation" class="animation">Block</div>
    </div>
    <div id="third" class="third"></div>
   

最佳答案

如果你想在有人滚动时重置动画,你需要像这样删除元素的样式属性:

$('#animation').removeAttr('style')

Check out the fiddle here

关于javascript - 滚动到另一个元素后从头开始动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42648082/

相关文章:

Javascript 或 jQuery 根据另一个输入值自动填充一个输入文本

javascript - 在 AngularJS 中更改 URL 而不重新加载

javascript - 自动将凭据传递到下一页的脚本

html - 移动网站 : Detecting when images have been disabled

javascript - 在音频元素中选择

javascript - jQuery 工具 : Tabs: Adding Current Class to List Item?

javascript - Ant Design中pageSize变化时如何重置Pagination的当前页?

javascript - js/jQuery Drag'n'Drop,重新计算放置目标

javascript - 如何使用 Angular 2/4 的鼠标滚轮事件测试指令

javascript - jQuery UI Datepicker 显示不正确的周数