js和CSS3實現卡牌旋轉轉場效果

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了js CSS3實現卡牌旋轉轉場效果,具有一定的參考價值,感興趣的小夥伴們可以參考一下

我們經常會在遊戲裡看到一些幾張卡牌左右切換的效果,中間的一張最突出醒目,向左或向右滑動可切換到另一張,今天我們就用CSS3來實現下這種效果。

我們先來看個demo,具體的樣式各位可以自己調整:

(PC下可點擊按鈕切換,移動端可左右滑動切換)

從效果上我們可以看到,這5個p可以左右的切換,每次切換時總會有一個在中間顯眼的位置進行展示。在切換時,看起來是p進行了移動,進行了DOM的增刪操作。但是如果審查下元素,就能看到,DOM元素沒有變換位置,它依然在那個位置,我們只是切換了每個元素上的class,於是頁面上的位置看起來是發生了變化。

其實原理就是這樣的: 不進行DOM的增刪,為每個位置上的p都寫上特定的樣式,每個p都進行絕對位置,然後進行樣式的輪播。 每次切換都有個0.6s過渡過程:

-webkit-transition: all 0.6s;transition: all 0.6s;

比如從左往右的class分別為:item_0, item_1, item_cur, item_3, item_4,每個class都是當前所在p的定位,向左滑動時,右邊的p會切換到中間,這樣class從左往右就變成了item_1, item_cur, item_3, item_4, item_0。

var egg_change = function(type){ var $demo = $('.demo'),  index = parseInt( $demo.attr('index_cur')||2 ),  $item = $('.demo .item'),  len = $item.length; if( type=='left' ){  index = (index+1)%len; }else{  index = (index-1+len)%len; } $demo.attr('index_cur', index); $item.removeClass('item_0 item_1 item_3 item_4 item_cur');  $item.eq( (index-2+len)%len ).addClass('item_0'); $item.eq( (index-1+len)%len ).addClass('item_1'); $item.eq(index).addClass('item_cur'); $item.eq( (index+1)%len ).addClass('item_3'); $item.eq( (index+2)%len ).addClass('item_4');}

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.