Yadda za a yi Amfani da CSS3 Nishaɗi: Tutorial, Samfura Codes, da Misalan

Mataki na ashirin da ya rubuta ta:
  • Shafin Yanar Gizo
  • An sabunta: Aug 28, 2013

Lokacin da muka yi amfani da JS da jQuery muna da cikakken iko a kan rayarwa kuma za mu iya ƙirƙirar wani sakamako mai ban mamaki, amma farashin yana da yawa. Lokacin tafiyarwa, daidaitattun giciye (na'urorin hannu, alal misali, suna da bambanci idan yazo ga JS) kuma ƙaddamarwar code kanta maƙasudai ne cewa ya kamata mu tuna yayin ƙirƙirar haɗi.

Saboda haka, a yau zamu ga yadda za mu guje wa JS ta amfani da CSS Animations da Transitions. Za mu tattauna daga matakan matakai ga wasu abubuwa masu ban mamaki, kamar bangarorin da aka haɗe da kuma abubuwan da ke cikin raye-raye.

Ɗauki wurin zama, kwarewa da kuma ainihin mai bincike (wani abu sai IE) kuma bari mu fara.

Warming sama

Muna da wasu abũbuwan amfãni (da rashin amfani kamar duk abin da ke cikin rayukan mu) ta amfani da abubuwan da ake gudanarwa na CSS. Idan kana buƙatar sayar da su zuwa ga maigidanka ko abokin ciniki, wannan shine abin da ya kamata ka tuna:

  • Suna da sauri, tun da yake suna iya amfani da matakan gaggawa (azaman ayyukan HTML5)
  • Za suyi mafi kyau a cikin na'urori na hannu kuma bazai buƙatar takamaiman lambar don yin waƙa da abubuwan da suka faru ba
  • JS ya kamata a fassara shi ta hanyar mai bincike da kuma yiwuwar karyawar mai bincike. To, a lokacin da CSS ta kasa, sai ta kasa shiru yayin da JS zai iya karya duka shafi
  • Suna da goyon baya mai kyau na bincike (wannan shafin zai taimaka maka duba takamaimai akan wannan: http://caniuse.com/#search )

Misalan CSS3 Abubuwa

Kafin mu fara kan naman wannan post ɗin, bari mu kalli wasu kyawawan raye-raye da aka yi cikin tsarkakakken CSS.

CSS Twitter Fail Whale

An yi wa Whale ɓoye dabba

Made by Steven Dennis, duba wannan a cikin aikin.

CSS mai sa ido ga Coke Can
CSS 3 Misali Misalai: Gungura zuwa Coke Can

An yi ta Roman Cortes, duba wannan a cikin aikin.

Mai CSS Walking

CSS 3 Misali Misali: Mutumin Walking

Ya sanya Andrew Hoyer, duba wannan a cikin aikin.

Samun hannunka

Bari mu fara lambar. Za mu yi amfani da kundin CSS da yawa don faɗakar da tashin hankali. Don gaskiya, mai yawa masu ci gaba suna ba da shawarar yin amfani da JS don kunna da kuma kunna motsa jiki, amma a nan za mu ga hanya mafi sauki:

#test {baya: ja; } #: hover {baya: kore; } #test: aiki {baya: blue; } #test: manufa {baya: baki; }

Muna da wasu ƙananan jeri-jinsin don amfani, amma kuna da ra'ayin! Saboda haka a nan ne abin da ya faru idan ka danna nau'in #test (ɗauka yana da hanyar haɗi):

  • Yanayi na al'ada: Bayanin zai zama ja
  • Tsutsawa: Lokacin da linzamin kwamfuta ya shiga kashi yanki za a sami wani bayan kore
  • Ayyukan aiki: Lokacin da ka danna mabudin akan shi kuma yayin da maballin linzamin kwamfuta ya ci gaba da kasancewa launin launi ya zama blue
  • Target: Lokacin da shafi na yanzu yana da #test a cikin URL ɗin wannan kashi zai zama baki

Kowane ɗayan waɗannan ana iya amfani dasu don abubuwan CSS, alal misali za ka iya ƙirƙirar haɗin 2 don kunna da kuma kashe aikin CSS da ake yin amfani da wannan nau'i mai mahimmanci tare da wannan lambar:

<a href='#test'> kunna </a> <a href='#'> ya kashe </a>

CSS Transitions

Tsarin CSS zai canza daga farko har zuwa karshen jihar da sannu-sannu. Don haka za ku ayyana a cikin babban zaɓin ta amfani da '' miƙa mulki 'dukiya da lokaci da kowace dukiya da za a shafa da kuma yadda za a yi tashin hankali. Bari mu ga misali:

.% {/ * lokaci-lokaci-lokaci-lokaci-lokaci na kayan aiki, * / launi: blue; Tsarin mulki: launi 2s, nau'ikan 2s-nau'in-nau'ikan sauƙi; } maimaita: hover {launi: ja; }:: aiki {font-size: 200%; }

Yayin da kake kwantar da ragowar gwargwadon motsawa zai canza launin launi daga launin blue zuwa ja (abin da ke da kyau palette, huh?). Lokacin da ka danna maɓallin, girman nauyin zai karu zuwa hankali zuwa 200% na girman matakan tsoho.

Har ila yau, muna da dukiyar "lokaci-lokaci", da aka saita a matsayin mai sauƙi, yadda za a kashe lokacin "lokaci" da ake gudanarwa. Anan akwai dabi'u mai yiwuwa:

  • Linear: Same gudun daga farkon zuwa ƙarshe
  • Bawa cikin: Slow fara
  • Bazuwa: Slow karshen
  • Ƙarshen: Sannu a hankali, da sauri a tsakiyar, sannan jinkirin jinkirin
  • Rashin ƙwaƙwalwa: Slow fara, jinkirin ƙarewa
  • Cubic-bezier (a, b, c, d): Gwanin al'adu

Ayyukan Bezier mai siffar sukari zai haifar da halayyar al'ada tare da lambobin 4 da suka bambanta daga 0 zuwa 1, wakiltar madauran lissafi don gudun-radin gudu na X.

Domin mafi dacewa da kwarewa ta intanet ya kamata ka yi la'akari da yin amfani da prefixes mai sayarwa ga wasan kwaikwayo, Firefox, da kuma yanar gizo kamar haka:

Nisa sashe: 400px; -o-miƙawa: nisa 2s; -moz-matsakaici: nisa 2s; -dodin-miƙawa: nisa 2s; Tsarin mulki: Nisan 2s; }

Har ila yau, za ka iya amfani da tambayoyin kafofin watsa labarun don bayyana bambancin canje-canjen dangane da nuni na bincike (na'urorin hannu, Allunan). Wannan misali ne mai sauki:

jiki {font-size: 1em; } allon allon da (max-width: 800px) {jiki {font-size: 0.8em; }} allon allon da (max-width: 400px) {jiki {Font-size: 0.7em; }}

A nan za a sauya yawan nauyin rubutu ba zato ba tsammani idan ka ƙara girman nesa. Wannan lambar za ta hana wannan daga faruwa, ta ba da matsakaici mai sauƙi:

tsarin jiki {-o-transition: font-size .5s linear; -moz-matsakaici: font-size .5s linear; -dabar-tsauraran matakan: font-size .5s linear; Tsarin mulki: font-size .5s linear; }

Hakanan zaka iya amfani da wannan kuma idan kuna da nau'i daban-daban ko masu girma ga hoto / wuri mai faɗi, idan kuna so ku canza nisa, launuka, paddings, nuni na menu.

CSS Animation - Gwanin Real fara

Rawanin ya zama jerin jerin fassarar da aka bayyana a cikin guda zaɓi. Don ƙayyade abubuwan da ke cikin CSS za ku buƙaci bi ka'idojin 2.

An yi amfani da mulkin @keyframe don tsara wani tsari na matakan rai, kuma an bayyana shi ta wani suna na musamman da kuma salon da ke kwatanta irin yadda wannan motsin yake aiki. Kamar yadda muka saba muna buƙatar wasu takaddun mai sayarwa, kamar misalin wannan misali:

/ * wannan lambar don kowane mai siye * / @ -o-keyframe animation {... @ -moz-keyframe my-animation {... @ -webkit-keyframe my-animation {... / * sunan mahaɗi * / @keyframe zane-zane {/ * mai zabin yanayi * / 0% {/ * Tsarin tsarin * / Hagu: 0px; Top: 0px; } 25% \ Hagu: 200px; Top: 0px; } 50% \ Hagu: 200px; Top: 200px; } 75% \ Hagu: 0px; Top: 200px; } 100% \ Hagu: 0px; Top: 0px; }}

Saboda haka, kowane launi an bayyana ta hanyar filayen / lokaci (kamar waɗannan ɓangarori daga radiyo) a matsayin kashi da kuma tsarin da za'a yi amfani da shi a can. Wannan makullin, misali misali cewa kashi zai matsa zuwa hagu, sannan sama, to, dama, to kasa.

Bayan da ka bi mataki na 1 da kuma kirkirar maɓallin wayarka da kanka zaka iya amfani da shi zuwa wani kashi. Sa'an nan kuma za mu yi amfani da irin wannan ma'anar kamar yadda muka yi tare da CSS, bambancin shine cewa yanzu "sauyawa" wani abu ne mai rikitarwa.

Don amfani da shi zamu yi amfani da kayan haɗi kuma yana da abubuwan mallaka na 7:

  • Sunan: wannan mai ganowa na musamman
  • Duration: Yaya tsawon lokaci za a ɗauka daga 0% zuwa 100%
  • Lokaci-lokaci: kyawawan nau'i daidai da aiki na lokaci-lokaci
  • Tsayawa: Yaya tsawon lokacin zai fara don fara 0%
  • Ƙididdigar ƙididdiga: Nawaitawa da yawa za mu sami ("iyaka" don iyaka mara iyaka)
  • Jagora: al'ada ko sauye (baya)
  • Jiha-jiha: idan tashin hankali yana gudana ko dakatarwa

Wannan zai yi amfani da motsinmu zuwa nau'in #test lokacin da ya kasance manufa ta shafi:

#test: manufa {/ * mai gabatarwa-suna | duration | aikin lokaci-lokaci | jinkirta | ƙididdiga-lissafi | shugabanci | wasanni-jihohi * / rayewa: 10s 0s na layi na radiyo na ƙa'ida na al'ada; }

Da wannan a zuciyarmu zamu iya ƙirƙirar wasu misalai masu kyau.

CSS kawai Accordion

Za mu kirkiro bangarori masu rarraba da suke amfani da su na CSS. A nan ne ainihin tsarin HTML:

<div class = "daidaitawa>> <a href="#tab1"> Tab 1 </a> <div id =" tab1 "> <p> TEXT 1 </ p> </ div> <a href =" # tab2 "> Tab 2 </a> <div id =" tab2 "> <p> TEXT 2 </ p> </ div> <a href="#tab3"> Tab 3 </a> <div id =" tab3 "> <p> TEXT 3 </ p> </ div> </ div>

Wannan zai haifar da bangarori da haɗin da zasu jawo kowane ɗayansu. Kuma a nan ne inda sihiri ya faru:

/ * kowane sashin da ke ciki cikin yarjejeniyar * / .accordion div {/ * an ɓoye ta tsoho * / tsawo: 0; ambaliya: boye; / * sihiri sihiri * / miƙa mulki: tsawo 1s; } / * lokacin da aka ambata dashi shine manufa * / .accordion div: manufa {/ * tsawo: auto ba zai aiki ba, amma wannan zai yi aiki mai kyau * / tsawo: 80px; }

M kyakkyawa, huh? Kuma kuna amfani da rayuwarku ta amfani da JS don wannan? :)

CSS kawai Menu tare da Menu

Kuma wannan wata hanya ce mai sauki. Kuna da jerin abubuwan da ke cikin shafukan yanar gizonku, kuma sau da yawa muna buƙatar amfani da wasu menus a can. Hanyar mafi kyau ta nunawa da ɓoye abubuwa tana amfani da jQuery, dama? To, yi tunani sake bayan ka gwada wannan lambar:

<nav> <ul> <li> <a href='#item1'> Item 1 </a> <div> <ul> <li> <a href='#item11'> Nashi 1.1 </a> </ li> <li> <a href='#item12'> Item 1.2 </a> </ li> </ ul> </ div> </ li> <li> <a href='#item2'> Item 2 </a> <div> <ul> <li> <a href='#item21'> Item 2.1 </a> </ li> <li> <a href='#item22'> Wajen 2.2 </a> </ li> </ ul> </ div> </ li> </ ul> </ nav>

Kuma wizardry fara a nan:

a {/ * kawai yin alaƙa da tad mafi kyau * / nuni: toshe; Nunawa: 4px; } nav {rubutu-align: cibiyar; } / * kowane menu (ciki har da maɓallin farko) * / taswirar [alamomin: launi mai launi; jerin-jerin: babu; } nav> ul> li {/ * a kwance abubuwa (tsaye zai yi aiki sosai) * / taso kan ruwa: hagu; } nav li div {/ * ya rushe kowane tashar menu * / tsawo: 0; ambaliya: boye; / * Zuwan Houdini * / miƙa mulki: tsawo 1s; } nav li: hover> rarraba tsawo: 56px; }

Summing Up

Wannan hakika kawai jagora ne mai farawa. Akwai abubuwa masu yawa da za a iya yi ta hanyar amfani da CSS kawai da kuma abubuwa masu yawa da za su zo.

Don haka, kun yi amfani da wannan kafin? Kuna iya tunanin wani aikace-aikacen kirki na CSS? Ka raba tunaninka ta amfani da comments!

Game da Rochester Oliveira

Ni dan zanen yanar gizo da dan kasuwa daga Itajubá (MG), Brasil. Ina son yin rubutu game da batutuwa masu ban mamaki da kuma yin wasu abubuwa mai ban sha'awa.

n »¯