Javascript - 当左侧的 div 为 100% 时,如何获得警报?

标签 javascript css

当类名为“earth”的 div 位于左侧 100% 时,如何获得警报?请不要使用 jQuery,谢谢!我尝试使用 setTimeout,但我希望有另一种方式,不必依赖于时间,而是依赖于位置

<!DOCTYPE html>
<html>
<head>
    <style>
    .earth {
        position :relative;
        animation:move 10s linear;
        background:red;
        height:20px;
        width:20px;
    }

    @-webkit-keyframes move
    {
        from { left: 0%;  }
        to { left: 100%; }
    }
    </style>
    <script>
    function changetext () {
        document.getElementById("demo").style.color = "red";
        animation();
    }

    function animation() {
        document.getElementById('ghost').className ='earth';
    }

    function repeat() {
        var sd = document.getElementById("ghost").className = 'earth';
        sd.style.display = 'none';
    }
    </script>
</head>
<body>
    <div class="something"></div>
    <div id="ghost"> </div>
    <p onclick="changetext();" id="demo"> HELLO WORLD </p>
</body>
</html>

最佳答案

您可以使用 animationend事件:

var anim = document.querySelector(".earth");
anim.addEventListener("animationend", function() {
  console.log('end !');
});
.earth {
  position: relative;
  animation: move 10s linear;
  background: red;
  height: 20px;
  width: 20px;
}

@-webkit-keyframes move {
  from {
    left: 0%;
  }
  to {
    left: 100%;
  }
}
<div class="earth"></div>

关于Javascript - 当左侧的 div 为 100% 时,如何获得警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49214077/

相关文章:

c# - 我怎样才能显示两个asp :Panel controls side by side?

javascript - 如何停止 Bootstrap-3-Typeahead 下拉列表按排序顺序显示元素

javascript - 文件系统 API - 从目录拖放中递归列出文件

javascript - 为什么从 PDF 复制时缺少格式

javascript - "DataTable"加载 jquery 后 CSS 样式消失

javascript - 禁用文本框时如何在 jquery 中禁用此上下文菜单

css - 如何在CSS中分配数据属性?

html - 用于触发 Google 跟踪代码管理器的 CSS 选择器

javascript - Vue中没有定义axios

javascript - TypeScript 将 bool 值转换为字符串