jQuery实现圣诞节礼物传送(花式轮播)
大致介绍
下午看到了一个送圣诞礼物的小动画,正好要快到圣诞节了,就动手模仿并改进了一些小问题
原地址:花式轮播----圣诞礼物传送
思路:动画中一共有五个礼物,他们平均分布在屏幕中,设置最外边的两个礼物旋转一定的角度并隐藏,动画开始,每个礼物向右移动一定的位置,然后再把第五个礼物添加到第一个礼物之前,这样这五个礼物就重新排列了,在写jQ时只管礼物位置的变化就行了,因为礼物的旋转和隐藏在样式中都已经设置好了,某个礼物如果成为第一个礼物就会自动隐藏旋转
基本结构
代码:
<divclass="cr"> <divclass="cr-l"><imgsrc="img/fatherCh.png"alt=""/></div> <divclass="cr-icon"> <divid="cr-icon"> <imgstyle="left:5%"src="img/gift2.png"alt=""/> <imgstyle="left:25%"src="img/gift2.png"alt=""/> <imgstyle="left:45%"src="img/gift2.png"alt=""/> <imgstyle="left:65%"src="img/gift2.png"alt=""/> <imgstyle="left:85%"src="img/gift2.png"alt=""/> </div> </div> <divclass="cr-r"><imgsrc="img/family.png"alt=""/></div> </div>
样式
在css中用到了:first和:last属性,这两个属性是动态的,如果文档的结构变了,这两个属性的值也会相应的改变,这样我们就不必再麻烦的判断哪个元素是最后一个元素(第一个元素),直接用这两个属性就会自动选择第一个元素和最后一个元素
html,body{
height:100%;
margin:0;
padding:0;
}
.cr{
width:100%;
position:relative;
background:url("../img/bg.png")no-repeat00;
-webkit-background-size:100%100%;
background-size:100%100%;
overflow:hidden;
}
.cr-l,.cr-r{
position:absolute;
bottom:10%;
width:20.8%;
height:70%;
z-index:100;
}
.cr-limg,.cr-rimg{
width:100%;
position:absolute;
top:50%;
}
.cr-l{
left:0;
}
.cr-r{
right:0;
}
.cr-icon{
bottom:15%;
left:0;
position:absolute;
z-index:1000;
width:100%;
height:70%;
text-align:center;
}
.cr-iconimg{
margin-right:25px;
width:10%;
vertical-align:top;
position:absolute;
bottom:0;
opacity:1;
transform:rotate(0deg);
transition:all1s;
}
.cr-iconimg:first-child{
transform:rotate(-90deg);
-webkit-transform:rotate(-90deg);
opacity:0;
width:0;
}
.cr-iconimg:last-child{
transform:rotate(90deg);
-webkit-transform:rotate(90deg);
opacity:0;
width:0;
}
jQuery代码
在源码中,作者将这个五个礼物的初始位置写在了HTML结构中,我觉得不太好就在jQuery的代码中实现了,代码没有什么难的,就是对思路的实现
$(function(){
//点击礼物图片,切换图片
$('#cr-iconimg').click(function(){
if($(this).attr('src')=='img/gift2.png'){
$(this).attr('src','img/gift.png');
}else{
$(this).attr('src','img/gift2.png');
}
});
vartimer=null;
varoImg=$('#cr-iconimg');
varend=document.body.clientWidth;
varheight=document.body.scrollHeight;
//设置高
$(".cr").css("height",height);
//设置图片的初始位置
for(vari=0;i<oImg.length;i++){
$(oImg[i]).css('left',5+i*20+'%');
}
//图片轮换函数
functionscrollLogo(){
oImg.each(function(){
varleft=parseInt($(this).css('left'));
left+=end*0.2;
$(this).css('left',left);
});
$('#cr-iconimg:last').insertBefore('#cr-iconimg:first').css('left',end*0.05);
}
scrollLogo();
//定时器,开始轮换
timer=setInterval(scrollLogo,1800);
//鼠标移入清楚轮换
oImg.mouseover(function(){
clearInterval(timer);
});
//鼠标移出开始轮换
oImg.mouseleave(function(){
timer=setInterval(scrollLogo,1800);
});
});
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!