JS and CSS3 achieve card rotation switching effect

Source: Internet
Author: User
This article is mainly for everyone to introduce the JS CSS3 realize card rotation switching effect, with a certain reference value, interested in small partners can refer to

We often see in the game a few cards around the effect of switching, one of the most prominent in the middle, swipe left or right to switch to another one, today we use CSS3 to achieve this effect.

Let's take a look at the demo, the specific style you can adjust yourself:

(PC can click the button to switch, the mobile can be left and right sliding switch)

From the effect we can see, these 5 p can be left and right switch, each time there will always be a prominent position in the middle of the display. When switching, it appears that P has moved, and the DOM has been deleted. But if we examine the element, we can see that the DOM element is not transformed, it is still in that position, we just switch the class on each element, and the position on the page appears to have changed.

In fact, the principle is this: do not do the DOM additions and deletions, for each position p is written on a specific style, each p is absolutely positioned, and then the style of the carousel. Each switchover has a 0.6s transition process:

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

For example, from left to right class is: Item_0, Item_1, Item_cur, Item_3, Item_4, each class is the current position of P, swipe left, the right p will switch to the middle, so that class from left to right into the 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 ');}

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.