设置元素动画jquery
Animating elements jquery
我有一个幻灯片,它在图像中循环播放,然后在这些图像上方我有一些文本,然后是两个图像。我想做的是弄清楚我试图制作哪些图像的动画,但我想在每个动画之间有一个延迟。
我的困难在于,我有3张幻灯片,每张幻灯片可以有2张图像,这些图像需要分别设置为背景动画,幻灯片是根据用户的偏好排列的,所以从前端的角度来看,我永远无法100%确定哪两张图像将被分组在一起,因此,我写了以下内容,
if($(".current .iphone").length) {
$(".current .iphone").animate({
opacity : 1,
left : "840px"
}, 800);
}
if($(".current .blackberry").length) {
$(".current .blackberry").animate({
opacity : 1,
left : "1119px"
}, 800);
}
if($(".current .samsung").length) {
$(".current .samsung").animate({
opacity : 1,
left : "783px"
}, 800);
}
if($(".current .htc").length) {
$(".current .htc").animate({
opacity : 1,
left : "900px"
}, 800);
}
if($(".current .nokia").length) {
$(".current .nokia").animate({
opacity : 1,
left : "823px"
}, 800);
}
if($(".current .nokia").length) {
$(".current .nokia").animate({
opacity : 1,
left : "823px"
}, 800);
}
这是我的HTML,
<div id="slideshow" role="main" style="position: relative; overflow: hidden; ">
<section data-background="_/images/elements/parralex-1.jpg">
<img src="_/images/iphone-blue.png" alt="iPhone mybunjee" class="iphone foreground phone" />
<img src="_/images/blackberry-pink.png" alt="Blackberry mybunjee" class="blackberry background phone" />
</section>
<section data-background="http://www.samtordoffracing.com/wp-content/uploads/2012/07/Tordoff-14.jpg">
<img src="_/images/iphone-blue.png" alt="iPhone mybunjee" class="iphone foreground phone" />
<img src="_/images/blackberry-pink.png" alt="Blackberry mybunjee" class="blackberry background phone" />
</section>
<section data-background="http://www.samtordoffracing.com/wp-content/uploads/2012/07/Tordoff-14.jpg">
<div class="samsung foreground"></div>
<div class="nokia foreground"></div>
</section>
</div>
基本上,我正在做的是试图找出当前幻灯片中存在哪些图像,然后进行动画制作,但目前两个图像都同时进行动画制作。我希望在一个图像进行动画制作和下一个图像之间有一个随机延迟。
有没有更好的方法来做我正在做的事情?
您可以尝试以下操作:
$.each($('#slideshow').find('img'), function(i, img){
if($(img).hasClass('iphone')){
setTimeout(
function(){
$(img).animate({
opacity : .5,
'margin-left' : "+=40px"
}, 800)
}, Math.random() * i *800);
}
if($(img).hasClass('blackberry')){
setTimeout(
function(){
$(img).animate({
opacity : .5
'margin-left' : "-=40px"
}, 800)
}, Math.random() * i *800);
}
});
无论如何,这里有一些例子,看看这个例子。
我不完全理解你想做什么,但我改变了你的jQuery结构。
您需要为此操作定义一个trrigger/事件,如悬停()或单击()
使用这个来减少代码:
$('.current').hover(function() {//mouse over event
var currentClass = $(this).children().attr('class');//takes mouse overed element's chlid's class
if($('.current .'+currentClass).length) {
$(".current ."currentClass).animate({
'opacity' : '1',
'left' : '840px'
}, 800);
}
});
如果不想定义触发事件,可以将each()方法与setInterval()一起用于定时动画。
相关文章:
- 剑道网格jQuery动画()问题
- jquery动画可以通过编程链接吗
- JQuery动画延长容器不起作用
- 如何正确编程jQuery动画与平滑(导航栏)
- jQuery动画-边框宽度和颜色
- 如何抽象JQuery动画方法
- jQuery动画标题滚动
- JQuery - 为什么 JQuery 动画是同时进行的
- 如何用jquery动画改变背景颜色,就像一个过渡
- 正在等待jQuery动画完成
- 如何优化jquery动画代码
- jQuery动画的持续时间就像一个延迟
- 创建一个类似Jquery动画的Flipboard弹出窗口
- 应用jQuery动画时出现意外的抖动效果
- 如何设置'auto'JQuery动画中的高度
- jQuery动画缩放需要我点击主体
- 如何将jquery动画应用于单个项目
- 如何简化jquery动画函数代码
- 使用 jquery 动画幻灯片使用角度中继器切换
- Jquery动画和when函数