javascript - 如何在滚动时弹起动画?

标签 javascript html

我有这个.png,我想让它在每次检测到滚动运动时弹跳一点,但我不擅长javascript和css。希望大家能够帮助我

<div class="arrownav bounce">
  <a href="" class="logo">
    <img src="{{ asset('assets/img/arrow down.png') }}" height="45">
  </a>
</div>

我现在使用CSS使图像反弹
这是代码:

  .bounce {
    -webkit-animation:bounce 1s infinite;
    -moz-animation:bounce 1s infinite;
    -o-animation:bounce 1s infinite;
    animation:bounce 1s infinite;

  }

  @-webkit-keyframes bounce {
    0%       { bottom:0px; }
    50%      { bottom:15px; }
    100%     {bottom:30;}
  }

   @-moz-keyframes bounce {
    0%       { bottom:0px; }
    50%      { bottom:15px; }
    100%     {bottom:30;}
  }

   @-o-keyframes bounce {
    0%       { bottom:0px; }
    50%      { bottom:15px; }
    100%     {bottom:30;}
  }

   @keyframes bounce {
    0%       { bottom:0px; }
    50%      { bottom:15px; }
    100%     {bottom:30;}
  }

最佳答案

我注意到的第一件事是所有@keyframes中缺少的单元,就在这里:

100%     {bottom:30;}

这应该是:

100%     { bottom:30px; }
<小时/>

您在动画中使用了 bottom 样式,这非常好,但为了使其正常工作,元素的 position 需要是 相对绝对固定(更多here)。

.bounce {
    position: relative;
    -webkit-animation: bounce 1s infinite;
    -moz-animation: bounce 1s infinite;
    -o-animation: bounce 1s infinite;
    animation: bounce 1s infinite;
}

这是一个工作 fiddle .

<小时/>

奖金 更改动画中元素位置的另一种方法是使用 transform 样式,而不是使用 bottom。当您使用transform时,您不需要position:relative;

@keyframes bounce {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-15px);
  }
  100% {
    transform: translateY(-30px);
  }
}

Fiddle

关于javascript - 如何在滚动时弹起动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40943401/

相关文章:

javascript - 如何将代码隐藏中的 ADO DataTable(在页面加载时可用)返回到 javascript?

javascript - 在 meteor 中显示日期

jquery - 如何使用 javascript 在 html 中添加甜蜜的警报弹出消息

Javascript 在函数内部使对象字面量等于 {},

javascript - 使用 Javascript 服务器端编码从经典 ASP 返回 JSONP

javascript - 分割字符串时,angularjs解析语法错误

html - 如何获取 html/css 表单以发布到 Gravity Forms?

php - 如何将基本静态网站转换为动态网站,以便为其提供 CMS? PHP/MySQL

javascript - 水平菜单重叠图像 slider

html - 响应式设计不适用于文本