3D бумажная открытка с помощью jQuery и CSS3
Ранее вы наверняка встречали поделки из бумаги которые представляли собой некий механизм, при определенном нажатии происходит разворот механизма. Но все новое это хорошо забытое старое, в сегодняшнем уроке мы бы хотели рассказать и наглядно показать как создать открытку с небольшой ручкой, при нажатии на которую происходит разворачивание открытки, создавая при этом иллюзию трехмерности. Добиться такого эффекта объемности нам помогут трансформации css, удачно подобранные тени.
Данный эффект отлично подойдет для оформления миниатюр на сайте, или создания оригинального портфолио. И так, приступим к первому шагу.
Шаг 1. HTML
Для начала мы рассмотрим разметку, в которой произведем разделение классом fc-flip который будет содержать передняя часть и задняя часть открытки. Задняя часть не будет видна, потому что мы поверните ее на 180 градусов по оси координат. Также мы добавим ручку-переключатель, которая будет приводить в действие данную открытку.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<div class="fc-wrapper"> <div class="fc-handle fc-handle-pull"></div> <div class="fc-perspective"> <div class="fc-overlay fc-overlay-reverse"></div> <div class="fc-handle fc-handle-out"><div></div></div> <div class="fc-bottom"> <div class="fc-bottom-bg"> <div class="fc-content"> <p>I can live with doubt, and uncertainty, and not knowing. <span>Richard Feynman</span></p> </div> </div> <div class="fc-overlay fc-overlay-solid"></div> </div> <div class="fc-flip"> <div class="fc-front"> <div class="fc-inner"> <div class="fc-overlay fc-overlay-left"></div> <!-- left-most part of handle --> <div class="fc-handle fc-handle-in"><div></div></div> <div class="fc-content"> <h3>Free revelations</h3> </div> </div> </div><!-- //fc-front --> <div class="fc-back"> <div class="fc-inner"> <div class="fc-content"> <div class="feynman"> <span>1918 – 1988</span> </div> </div> <div class="fc-overlay fc-overlay-right"></div> </div> </div> </div> </div> </div> |
Разметка не должна вызвать вопросов, так как все достаточно просто, мы не будем останавливаться на ней более детально и сразу перейдем к следующему шагу.
Шаг 2. CSS
Основная оболочка будет составлять 300 пикселей в высоту и ширину. Для всех элементов круга мы будем использовать текстуру в виде бумаги, которую мы будем накладывать цветовые оттенки, придавая ей реалистичный вид открытки из бумаги. Оболочка будет также в виде круга, так как мы даем ей границы радиусом 50%.
1 2 3 4 5 6 7 8 9 |
.fc-wrapper { width: 300px; height: 300px; position: relative; margin: 30px auto 0; background: #846aa7 url(../images/paper.png) repeat center center; border-radius: 50%; box-shadow: 0 0 3px rgba(0, 0, 0, 0.3); } |
Теперь зададим стили для параметров ручки, которой присвоен класс fc-handle:
1 2 3 4 5 6 7 8 9 10 |
.fc-handle { position: absolute; top: 50%; right: 0px; width: 80px; height: 30px; margin-top: -15px; background: #775b9d; box-shadow: 0 1px 1px rgba(0,0,0,0.1); } |
Следующий класс будет определять положение середины открытки:
1 2 3 4 |
.fc-handle-out { right: -65px; width: 65px; } |
Теперь зададим параметр для большой ручки:
1 2 3 |
.fc-handle-in { right: 80px; } |
Следующий псевдо-элемент будет использоваться для создания эффекта тени при открытии и закрытии открытки:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
.fc-handle-out::after { position: absolute; right: 0px; width: 4px; height: 31px; content: ''; background: linear-gradient( to right, rgba(0,0,0,0) 0%, rgba(0,0,0,0.15) 100% ); } |
Теперь стили для правого куска открытки, то есть тот, который вызовет открытие окружности буклета:
1 2 3 4 5 6 7 8 9 10 |
.fc-handle-pull { right: auto; left: 100%; margin-left: 5px; width: 30px; box-shadow: 1px 0 1px rgba(0,0,0,0.1), inset 3px 0 2px rgba(0,0,0,0.2); cursor: pointer; } |
Зададим некоторые градиенты для открытия:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
.fc-handle-pull::after { content: ''; position: absolute; top: 0px; right: 0px; width: 30px; height: 100%; background: linear-gradient( to right, rgba(0,0,0,0) 0%, rgba(0,0,0,0) 69%, rgba(0,0,0,0.08) 100% ); } |
В нижней части, которая будет содержать цитату, содержание будет иметь следующий стиль:
1 2 3 4 5 6 7 |
.fc-bottom { width: 220px; height: 100%; overflow: hidden; position: absolute; opacity: 0; } |
Мы ставим прозрачность на 0, так как мы не хотим, чтобы вызвать зубчатые края круга, когда она закрыта.
1 2 3 4 5 6 |
.fc-bottom-bg { background: #846aa7 url(../images/paper.png) repeat center center; border-radius: 50%; width: 300px; height: 100%; } |
Теперь давайте рассмотрим стиль содержания элементов которые будут находиться в открытке:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
.fc-content { width: 220px; padding: 20px; text-align: right; position: relative; height: 100%; } .fc-back .fc-content { margin-left: 80px; } .fc-bottom-bg .fc-content { padding-top: 40px; } .fc-content p { font-size: 12px; line-height: 22px; font-family: "Montserrat", sans-serif; text-shadow: 0 -1px 1px rgba(255,255,255,0.1); color: #3b2954; padding: 0 0 0 31px; } .fc-flip .fc-front h3, .fc-flip .fc-front p { position: absolute; text-align: right; width: 180px; text-shadow: 0 -1px 1px rgba(255,255,255,0.1); color: #3b2954; } .fc-flip .fc-front h3, .feynman span { font-family: "Montserrat", sans-serif; text-transform: uppercase; font-size: 17px; letter-spacing: 1px; font-weight: normal; } .fc-flip .fc-front h3 { top: 30px; left: 15px; } .feynman { width: 255px; height: 255px; position: absolute; overflow: hidden; top: 50%; left: -55px; border-radius: 50%; box-shadow: 2px 0 3px rgba(0,0,0,0.3); margin-top: -127px; background: transparent url(../images/feynman.png) no-repeat center right; } .feynman span { text-align: center; width: 100px; height: 5px; line-height: 30px; color: #fff; text-shadow: 1px 1px 1px rgba(0,0,0,0.2); bottom: 40px; right: 80px; font-size: 13px; position: absolute; } .fc-flip .fc-front h3 span{ font-size: 40px; } .fc-flip .fc-front p, .fc-bottom-bg .fc-content span { bottom: 50px; left: 15px; font-family: "Dancing Script", Georgia, serif; font-weight: 700; font-size: 22px; } .fc-bottom-bg .fc-content span { font-size: 18px; display: block; color: #fff; padding: 10px; text-shadow: 1px 1px 1px rgba(0,0,0,0.2); transform: rotate(-3deg); } |
Собираем вместе стили которые будут вызывать трансформации:
1 2 3 4 5 6 7 8 9 10 11 12 |
.fc-flip .fc-back .fc-inner, .fc-overlay, .fc-handle, .fc-handle div, .fc-flip, .fc-bottom{ transition: all 0.6s ease-in-out; } .fc-bottom{ transition-delay: 0.6s; } |
Следующие параметры отвечают за правильную работу элементов 3D:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
.fc-flip, .fc-flip .fc-inner, .fc-handle { transform-style: preserve-3d; } .fc-flip > div, .fc-flip .fc-inner, .fc-flip .fc-front h3, .fc-handle, .fc-handle div, .fc-overlay, .fc-flip .fc-front p, .fc-flip .fc-front span { backface-visibility: hidden; } |
Диагональные градиенты, которые будут служить в качестве теней для двух частей ручки:
1 2 3 4 5 6 7 |
.fc-wrapper.fc-wrapper-open .fc-handle-in div { height: 180px; } .fc-wrapper.fc-wrapper-open .fc-handle-out div { height: 100px; } |
Цвет фона первых двух частей ручки станет темнее:
1 2 3 |
.fc-wrapper.fc-wrapper-open .fc-flip .fc-back .fc-inner { background-color: #846aa7; } |
И конечно, самое главное, что должно случиться: вращение контейнера:
1 2 3 |
.fc-wrapper.fc-wrapper-open .fc-flip { transform: rotate3d(0,1,0,175deg); } |
Шаг 3. JavaScript
Когда мы нажимаем на открытку то происходит трансформация, мы будем применять класс fc-wrapper-open к основному контейнеру. Мы определим две простые функции для открытия и закрытия. Мы также добавим поддержку, используя скрипты Hammer.js и JQuery плагин:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
$(function() { var $wrapper= $( '#fc-wrapper' ), $handle = $wrapper.children( 'div.fc-handle-pull' ); $handle.on( 'click', function( event ) { ( $handle.data( 'opened' ) ) ? close() : open(); } ); $wrapper.hammer().bind( 'dragend', function( event ) { switch( event.direction ) { case 'right' : open(); break; case 'left' : close(); break; } }); function open() { $wrapper.addClass( 'fc-wrapper-open' ); $handle.data( 'opened', true ); } function close() { $wrapper.removeClass( 'fc-wrapper-open' ); $handle.data( 'opened', false ); } } ); |
Вот и все. Готово!
Материал взят из зарубежного источника. И представлен исключительно в ознакомительных целях.
Читайте также:
Опубликовал Cooper 22.07.2012 в 10:25, в категории CSS. Вы можете следить за комментариями через RSS 2.0. Вы можете перейти в конец записи и оставить комментарий. Пинги запрещены. |