jQuery知道第一个转换已经完成,然后进行第二个转换
jQuery know first transition has been done then make the 2nd one
在Jquery中,我正在做一个小动画。在这方面,我参加了两次潜水。所以基本上逻辑是这样的,当鼠标悬停时,它会显示一个带有转换的隐藏div。第二个div也有同样的概念。但我的问题是,当我在第一个div上悬停时,它会显示转换中隐藏的div。但是当我在另一个div上悬停时,第一个隐藏的div被隐藏起来,第二个隐藏的divs显示在第二个div中。所以我希望当我将悬停在第二个子div上时,应该先隐藏第一个隐藏div,然后显示第二个隐藏div。
这是我到目前为止的代码
<div id="wrapper">
<div class="courses-method-left-wrapper">
<div class="courses-method-wrap left">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem </p>
</div>
<div class="online-course-price-wrap">
<h3>Left content wrap</h3>
<h6>Left content text</h6>
</div>
</div>
<div class="courses-method-right-wrapper">
<div class="courses-method-wrap right">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem </p>
</div>
<div class="offline-course-price-wrap">
<h3>Right content wrap</h3>
<h6>Right content text</h6>
</div>
</div>
</div>
到目前为止我的css
#wrapper {
width: 100%;
clear: both;
overflow: hidden;
background: #D7DFE6;
position: relative;
}
.courses-method-left-wrapper, .courses-method-right-wrapper {
width: 45%;
padding: 10px;
overflow: hidden;
float: left;
position: relative;
}
.courses-method-wrap.left {
float: left;
position: relative;
left: 0px;
}
.courses-method-wrap.right {
position: relative;
}
.online-course-price-wrap {
width: 230px;
background: #1C2C39;
position: absolute;
right: -230px;
height: 200px;
}
.offline-course-price-wrap {
left: -200px;
z-index: 0;
width: 200px;
position: absolute;
height: 100%;
top: 0px;
background: #ccc;
height: 200px;
}
.hovered .online-course-price-wrap { right: 0px; }
.hovered .offline-course-price-wrap { left: 0px; }
#wrapper * {
-webkit-transition: all 0.7s ease-out;
transition: all 0.7s ease-out;
-moz-transition: all 0.7s ease-out;
}
js代码就像这个
jQuery('body').on('hover','.courses-method-left-wrapper, .courses-method-right-wrapper', function(){
jQuery(this).toggleClass('hovered');
});
这是小提琴链接
那么,有人能告诉我如何完成一个过渡,然后开始另一个过渡吗?或者我如何检查第一个动画已经完成,以便开始第二个动画?任何帮助和建议都将非常可观。感谢
问题的快速解决方案是使用700ms的延迟来匹配转换中的0.7s,如下所示:
jQuery(this).toggleClass('hovered', function(){
setTimeout(function(){
alert('done');
}, 700);
});
http://jsfiddle.net/aym037ge/2/
这不是一个优雅的解决方案,但它仍然是一个。
另一种选择是使用前面和这里提到的转换事件:
CSS转换上的回调
您可以利用jQuery的transitioned
事件来捕获转换何时结束。
jQuery('.courses-method-left-wrapper').mouseenter(function ()
{
//If the previous div is already hovered...
if($('.courses-method-right-wrapper').hasClass('hovered'))
{
$('.courses-method-right-wrapper').removeClass('hovered');
$('.courses-method-right-wrapper').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function () {
$('.courses-method-left-wrapper').addClass('hovered');
});
}
else // The previous div isn't hovered (i.e. on page load...)
{
$('.courses-method-left-wrapper').addClass('hovered');
}
});
jQuery('.courses-method-right-wrapper').mouseenter(function ()
{
//If the previous div is already hovered...
if($('.courses-method-left-wrapper').hasClass('hovered'))
{
$('.courses-method-left-wrapper').removeClass('hovered');
$('.courses-method-left-wrapper').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function () {
$('.courses-method-right-wrapper').addClass('hovered');
});
}
else // The previous div isn't hovered (i.e. on page load...)
{
$('.courses-method-right-wrapper').addClass('hovered');
}
});
添加供应商前缀以实现完全兼容性,包括Opera 的两个前缀
你可以在我准备的小提琴中看到代码的作用。它可以改进(如果你移动鼠标太多,两个隐藏的div可能会显示出来,直到你再次悬停),但它应该会给你一个很好的开端。
相关文章:
- 如何将 Oracle 数据转换为 JSON,然后在 JavaScript 中使用
- 将普通的 PNG 图像转换为 JSON 数据,然后存储到对象
- JavaScript将人类可读的日期转换为unix时间戳,然后再转换回来
- jQuery知道第一个转换已经完成,然后进行第二个转换
- 有没有一种方法可以制作一个prezi,然后以某种方式将其转换为html和css类型的代码
- 通过regex匹配ahref标记,然后转换为字符串
- 将JSON字符串转换为JavaScript对象,然后创建一个HTML表
- Metor:将html保存为字符串,然后将其转换为DOM节点
- 使用 Json 获取 Twitter 提要,然后使用 PHP 将输出转换为 PHP 变量?- 推特-2-json脚本
- 取消设置元素可见性,更改 innerHTML,然后转换回来
- 调用math.rand,然后在函数argumnet中缩放并转换为int
- 在 Javascript 中将带有空格的字符串数组转换为小写,然后在 Webkit 中搜索它们失败
- 如何获取段落的文本,然后将其转换为 HTML 中的整数
- 使用 babel 将 es6 转换为 es5,然后与 Browserify 捆绑
- 将用户输入从 javascript 中的提示转换为数组,然后将其推送到对象上
- 将不可变.js地图列表转换为列表列表,然后将其用作Google图表的数据
- 从表单中提取值,然后将文本转换为整数
- 将一个电话号码数组转换为单个号码,然后用javascript比较这些号码
- 将字符串转换为十六进制,然后再转换回字符串
- 使用momentjs将日期转换为epoch,然后再转换回日期