首页 \ 问答 \ 如何使用JavaScript暂停和恢复CSS3动画?(How to pause and resume CSS3 animation using JavaScript?)

如何使用JavaScript暂停和恢复CSS3动画?(How to pause and resume CSS3 animation using JavaScript?)

我试图google和从这个论坛看一个解决我的问题,但没有运气到目前为止。 我想通过点击图片暂停我的CSS3动画(图像幻灯片),并通过点击图片恢复到相同的动画。

我知道如何暂停幻灯片放映,我也可以恢复一次,但如果尝试暂停和恢复一次以上,它将停止工作。 这是我的代码如何:

<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.pic {
    position: absolute;
    opacity: 0;
}
#pic1 {
    -webkit-animation: pic1 4s infinite linear;
}
#pic2 {
    -webkit-animation: pic2 4s infinite linear;
}
@-webkit-keyframes pic1 {
    0%   {opacity: 0;}
    5%   {opacity: 1;}
    45%  {opacity: 1;}
    50%  {opacity: 0;}
    100% {opacity: 0;}
}
@-webkit-keyframes pic2 {
    0%   {opacity: 0;}
    50%  {opacity: 0;}
    55%  {opacity: 1;}
    95%  {opacity: 1;}
    100% {opacity: 0;}
}
</style>
<script type="text/javascript">
function doStuff(){
    var pic1 = document.getElementById("pic1");
    var pic2 = document.getElementById("pic2");

    pic1.style.webkitAnimationPlayState="paused";
    pic2.style.webkitAnimationPlayState="paused";

    pic1.onclick = function(){
        pic1.style.webkitAnimationPlayState="running";
        pic2.style.webkitAnimationPlayState="running";
    }

    pic2.onclick = function(){
        pic1.style.webkitAnimationPlayState="running";
        pic2.style.webkitAnimationPlayState="running";
    }
}
</script>
</head>  
<body>
    <img id="pic1" class="pic" src="photo1.jpg" />
    <img id="pic2" class="pic" src="photo2.jpg" onclick="doStuff()" />
</body>                                                                 
</html>

我不想使用任何JS库(例如jQuery)或任何其他外部解决方案。

我的猜测是,我的doStuff函数中的函数仍在运行,这就是为什么pauseresume工作只有一次。

点击这些功能后,是否有办法清除这些功能? 或者我试图以完全错误的方式做到这一点? 帮助是赞赏 :)


I've tried to google and look from this forum a solution for my problem but no luck so far. I would like to pause my CSS3 animation (image slide show) by clicking a picture and also resume to the same animation by clicking a picture.

I know how to pause the slide show and I was also able to resume it once, but then it stops working if try to pause and resume more than one time. Here is how my code looks like:

<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.pic {
    position: absolute;
    opacity: 0;
}
#pic1 {
    -webkit-animation: pic1 4s infinite linear;
}
#pic2 {
    -webkit-animation: pic2 4s infinite linear;
}
@-webkit-keyframes pic1 {
    0%   {opacity: 0;}
    5%   {opacity: 1;}
    45%  {opacity: 1;}
    50%  {opacity: 0;}
    100% {opacity: 0;}
}
@-webkit-keyframes pic2 {
    0%   {opacity: 0;}
    50%  {opacity: 0;}
    55%  {opacity: 1;}
    95%  {opacity: 1;}
    100% {opacity: 0;}
}
</style>
<script type="text/javascript">
function doStuff(){
    var pic1 = document.getElementById("pic1");
    var pic2 = document.getElementById("pic2");

    pic1.style.webkitAnimationPlayState="paused";
    pic2.style.webkitAnimationPlayState="paused";

    pic1.onclick = function(){
        pic1.style.webkitAnimationPlayState="running";
        pic2.style.webkitAnimationPlayState="running";
    }

    pic2.onclick = function(){
        pic1.style.webkitAnimationPlayState="running";
        pic2.style.webkitAnimationPlayState="running";
    }
}
</script>
</head>  
<body>
    <img id="pic1" class="pic" src="photo1.jpg" />
    <img id="pic2" class="pic" src="photo2.jpg" onclick="doStuff()" />
</body>                                                                 
</html>

I don't want to use any JS libraries (e.g. jQuery) or any other external solution.

My guess is that my functions inside doStuff function are still running and that's why pause and resume works only once.

Is there a way to clear these functions after I have clicked them once? Or am I trying to do this in a totally wrong way? Help is appreciated. :)


原文:https://stackoverflow.com/questions/5804444
更新时间:2023-03-26 20:03

最满意答案

对此还有一个更清洁的解决方案

has_many :line_items, :dependent => :restrict # raises ActiveRecord::DeleteRestrictionError

这将引发您可以捕获的错误。

在任何情况下,您都需要指定dependent选项以避免在数据库中留下孤儿。 开发人员有责任记住这些事情并在Rails中设置适当的条件。

has_many :line_items, :dependent => :delete # Will remove all child records

我从未使用任何额外的宝石来处理外键。


There is also a cleaner solution for this

has_many :line_items, :dependent => :restrict # raises ActiveRecord::DeleteRestrictionError

This will raise an error that you can catch.

In any case, you need to specify the dependent option to avoid having orphans left in your database. It is the developers responsibility to remember these things and set the proper conditions in Rails.

has_many :line_items, :dependent => :delete # Will remove all child records

I never used any additional gems to deal with Foreign keys.

相关问答

更多
  • 对此还有一个更清洁的解决方案 has_many :line_items, :dependent => :restrict # raises ActiveRecord::DeleteRestrictionError 这将引发您可以捕获的错误。 在任何情况下,您都需要指定dependent选项以避免在数据库中留下孤儿。 开发人员有责任记住这些事情并在Rails中设置适当的条件。 has_many :line_items, :dependent => :delete # Will remove all child ...
  • 数据库控制杆上的检查是保护数据的最后一个完整性障碍。 对于性能问题,您可能希望删除外键,但最终可能需要维护部分损坏的数据库,这最终会成为一场噩梦。 外键可以提高性能 外键约束在读取数据时提高了性能,但同时在插入/修改/删除数据时降低了性能。 在读取查询的情况下,优化器可以使用外键约束来创建更有效的查询计划,因为外键约束是预先声明的规则。 这通常涉及跳过查询计划的某些部分,因为例如优化器可以看到由于外键约束,因此不必执行计划的特定部分。 Checks at the Database lever are the ...
  • 如果表B和表A之间已经有一个外键,以确保表B只包含表A中存在的a_id值的条目,那么a_id上表C和表A之间的额外FK是不必要的。 当然,这要求表B和表A之间的FK关系被加密,激活,并且不以任何方式禁用或规避。 在表C和表B之间建立FK链接已经保证TableC.a_id只能引用a_id的有效值(因为表B中通过表B和表A之间的FK关系保证了这一点)。 If you already have a foreign key between table B and table A to make sure table ...
  • 唯一没有关系的是AUser和ACountry之间的ACountry 。 目前,您在AUser上AUser 。 那应该是ACountry ,因为那是ACountry的主键。 另外, StateID不应该有StateID而不是State吗? 如果是这种情况,则不需要在该表上同时具有StateID和StateID ,您可以通过查看StateID列中引用的相应State来确定用户的Country。 The only relationship you haven't got right is between AUse ...
  • 它们显示实体完整性和参照完整性。 概念 超级密钥(如主键(PK))用于将每个记录独立地标识为唯一行。 超级密钥是通过组合多个列而形成的,同时仍然保持唯一,这可以包括多于最小列数以创建唯一的区别。 候选键是一组列,它们使用区分唯一键所需的最小列。 本质上,超级密钥,候选密钥和主密钥用于区分具有相似或相同值的记录。 辅助密钥是未选择作为主密钥的候选密钥。 外键用于“引用”单独的表以防止数据重复。 如果所有外键都有效,则数据库具有参照完整性。 例如,如果TRUCK表将FK存储到不存在的BASE_CODE,则数据库 ...
  • 我个人不喜欢在使用Eloquent时在数据库中保留外键关系,因为我想在一个地方(应用层)处理所有依赖逻辑。 拥有外键在性能方面没有任何理由,这纯粹是一种架构/设计决策。 I personally don't like to keep foreign key relationships in the database when using Eloquent as I want to handle all the dependency logic in one place (the app layer). Th ...
  • 默认情况下,Rails不会创建外键引用。 Rails使用从模型中的关联派生的信息来管理外键值本身。 Rails 4.2在迁移中 支持add_foreign_key 。 但是你不能在SQLite中使用它,因为SQLite不支持Rails用来创建外键的特定ALTER TABLE语句。 SQLite 对ALTER TABLE的支持非常有限 。 省略了其他类型的ALTER TABLE操作,例如DROP COLUMN,ALTER COLUMN,ADD CONSTRAINT等。 此外,add_foreign_key仅支 ...
  • 没有必要指定外键关系。 这只是一个好主意。 指定关系时,数据库可确保关系完整性。 也就是说,它确保外键列中的值是合法值。 此外,在更新或删除值时,外键上的cascade选项是一个很大的帮助。 It is not necessary to specify foreign key relationships. It is just a good idea. When you specify the relationship, the database ensures relational integrity. ...
  • 我会用下面的表格设计来接近它: a)主题 - 主题 b)主题 - 所有可用主题的列表,每个主题都有一个FK主题 c)学生 - 学生的详细信息 d)student_subject - 向学生分配主题,其他元数据包括级别或级别,开始日期或结束日期(因为学生可能重复) e)成绩 - FK到student_subject组合,附加元数据,日期,成绩,评论等。 Actually, for the moment I am content with the solution given in How to provide ...
  • 1)您应该更改id列的类型。 应该避免使用varchar作为id列,因为这会破坏性能(+可能发生的一些其他问题)。 使用任何类型的int(int,tinyint,..)会好得多。 2)表“ProjectType”应该有一列“ProjectType_ID”而不是“Project_ID” - 同样适用于PaymentType(“PaymentType_ID”)。 3)重命名“项目”和“付款”表中的字段。 4)外键的代码如下: ALTER TABLE Project ADD CONSTRAINT Project_ ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)