標籤:定時 abs first style dex scroll margin 改變 url
▓▓▓▓▓▓ 大致介紹
下午看到了一個送聖誕禮物的小動畫,正好要快到聖誕節了,就動手模仿並改進了一些小問題
原地址:花式輪播----聖誕禮物傳送
思路:動畫中一共有五個禮物,他們平均分布在螢幕中,設定最外邊的兩個禮物旋轉一定的角度並隱藏,動畫開始,每個禮物向右移動一定的位置,然後再把第五個禮物添加到第一個禮物之前,這樣這五個禮物就重新排列了,在寫jQ時只管禮物位置的變化就行了,因為禮物的旋轉和隱藏在樣式中都已經設定好了,某個禮物如果成為第一個禮物就會自動隱藏旋轉
如果對其中的方法不熟悉的可以參考我寫的jQuery學習之路,裡面有講解
▓▓▓▓▓▓ 基本結構
代碼:
1 <div class="cr"> 2 <div class="cr-l"><img src="img/fatherCh.png" alt=""/></div> 3 <div class="cr-icon"> 4 <div id="cr-icon"> 5 <img style="left:5%" src="img/gift2.png" alt=""/> 6 <img style="left:25%" src="img/gift2.png" alt=""/> 7 <img style="left:45%" src="img/gift2.png" alt=""/> 8 <img style="left:65%" src="img/gift2.png" alt=""/> 9 <img style="left:85%" src="img/gift2.png" alt=""/>10 </div>11 </div>12 <div class="cr-r"><img src="img/family.png" alt=""/></div>13 </div>
▓▓▓▓▓▓ 樣式
在css中用到了:first 和 :last 屬性,這兩個屬性是動態,如果文檔的結構變了,這兩個屬性的值也會相應的改變,這樣我們就不必再麻煩的判斷哪個元素是最後一個元素(第一個元素),直接用這兩個屬性就會自動選擇第一個元素和最後一個元素
1 html, body { 2 height: 100%; 3 margin: 0; 4 padding: 0; 5 } 6 .cr{ 7 width: 100%; 8 position: relative; 9 background: url("../img/bg.png") no-repeat 0 0;10 -webkit-background-size: 100% 100%;11 background-size: 100% 100%;12 overflow: hidden;13 }14 .cr-l,.cr-r{15 position: absolute;16 bottom:10%;17 width: 20.8%;18 height: 70%;19 z-index:100;20 }21 .cr-l img,.cr-r img{22 width: 100%;23 position: absolute;24 top:50%;25 }26 .cr-l{27 left: 0;28 }29 .cr-r{30 right:0;31 }32 .cr-icon{33 bottom: 15%;34 left:0;35 position: absolute;36 z-index: 1000;37 width: 100%;38 height: 70%;39 text-align: center;40 }41 .cr-icon img{42 margin-right: 25px;43 width: 10%;44 vertical-align: top;45 position: absolute;46 bottom: 0;47 opacity: 1;48 transform:rotate(0deg);49 transition: all 1s;50 }51 .cr-icon img:first-child{52 transform:rotate(-90deg);53 -webkit-transform:rotate(-90deg);54 opacity: 0;55 width: 0;56 }57 .cr-icon img:last-child{58 transform:rotate(90deg);59 -webkit-transform:rotate(90deg);60 opacity: 0;61 width: 0;62 }
▓▓▓▓▓▓ jQuery代碼
在源碼中,作者將這個五個禮物的初始位置寫在了HTML結構中,我覺得不太好就在jQuery的代碼中實現了,代碼沒有什麼難的,就是對思路的實現
1 $(function() { 2 3 // 點擊禮物圖片,切換圖片 4 $(‘#cr-icon img‘).click(function(){ 5 if($(this).attr(‘src‘) == ‘img/gift2.png‘){ 6 $(this).attr(‘src‘,‘img/gift.png‘); 7 }else{ 8 $(this).attr(‘src‘,‘img/gift2.png‘); 9 }10 });11 12 var timer = null;13 var oImg = $(‘#cr-icon img‘);14 var end = document.body.clientWidth;15 var height = document.body.scrollHeight;16 // 設定高17 $(".cr").css("height",height);18 19 // 設定圖片的初始位置20 for(var i=0;i<oImg.length;i++){21 $(oImg[i]).css(‘left‘, 5+i*20+‘%‘);22 }23 24 // 圖片輪換函數25 function scrollLogo(){26 oImg.each(function(){27 var left = parseInt($(this).css(‘left‘));28 left += end * 0.2;29 $(this).css(‘left‘,left);30 });31 $(‘#cr-icon img:last‘).insertBefore(‘#cr-icon img:first‘).css(‘left‘,end * 0.05);32 }33 34 scrollLogo();35 36 // 定時器,開始輪換37 timer = setInterval(scrollLogo,1800);38 39 // 滑鼠移入清楚輪換40 oImg.mouseover(function(){41 clearInterval(timer);42 });43 // 滑鼠移出開始輪換44 oImg.mouseleave(function() {45 timer = setInterval(scrollLogo,1800);46 });47 48 });
jQuery動畫-聖誕節禮物