Anda di halaman 1dari 19

7/2/2015

SlidingImagePanelswithCSS3

Codrops

InTutorialsbyMaryLou January17,2012 69Comments

SlidingImagePanelswithCSS3
Todaywe'llshowyouhowtocreatesomeneatslidingimagepanelswithCSSonly.The
ideaistousebackgroundimagesforthepanelsandanimatethemwhenclickingona
label.We'lluseradiobuttonswithlabelsandtargettherespectivepanelswiththegeneral
siblingselector.

http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

1/21

7/2/2015

SlidingImagePanelswithCSS3

TodaywellshowyouhowtocreatesomeneatslidingimagepanelswithCSSonly.Theideaisto
usebackgroundimagesforthepanelsandanimatethemwhenclickingonalabel.Welluseradio
buttonswithlabelsandtargettherespectivepanelswiththegeneralsiblingselector.
ThebeautifulimagesarebyJoannaKustraandtheyarelicensedundertheAttribution
NonCommercial3.0UnportedCreativeCommonsLicense.
YoumightaswellbeinterestedinFilterFunctionalitywithCSS3wherewehaveusedasimilar
techniqueforfilteringelementsbasedontheirtype.
http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

2/21

7/2/2015

SlidingImagePanelswithCSS3

Pleasenote:theresultofthistutorialwillonlyworkasintendedinbrowsersthatsupport
CSStransitionsandanimations.

TheMarkup
TheHTMLwillconsistofthreemajorparts:theradiobuttonsandthelabels,thecontainerwiththe
panelsandtheirslicesforeachimage,andthetitles.Thecontainerwiththeclasscrbgimgpart
willhaveadivisionforeachofthepanelsandinsidewellplacespansforeveryimagewiththe
rightbackgroundposition.So,thefirstpanelwillhavefourslices,eachhavingtheoneofthe
imagesasbackgroundwiththeleftmostposition.Thesecondpanelwillhaveagainfourslicesbut
nowthebackgroundpositionwillbemovedtobeshowingthenextpartoftherespectiveimage:
<sectionclass="crcontainer">
<!radiobuttonsandlabels>
<inputid="selectimg1"name="radioset1"type="radio"class="crselectorimg1"checked
<labelfor="selectimg1"class="crlabelimg1">1</label>
<inputid="selectimg2"name="radioset1"type="radio"class="crselectorimg2"/>
<labelfor="selectimg2"class="crlabelimg2">2</label>
<inputid="selectimg3"name="radioset1"type="radio"class="crselectorimg3"/>
<labelfor="selectimg3"class="crlabelimg3">3</label>
http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

3/21

7/2/2015

SlidingImagePanelswithCSS3

<inputid="selectimg4"name="radioset1"type="radio"class="crselectorimg4"/>
<labelfor="selectimg4"class="crlabelimg4">4</label>
<divclass="clr"></div>
<!panels>
<divclass="crbgimg">
<div>
<span>Slice1Image1</span>
<span>Slice1Image2</span>
<span>Slice1Image3</span>
<span>Slice1Image4</span>
</div>
<div>
<span>Slice2Image1</span>
<span>Slice2Image2</span>
<span>Slice2Image3</span>
<span>Slice2Image4</span>
</div>
<div>
<span>Slice3Image1</span>
<span>Slice3Image2</span>
<span>Slice3Image3</span>
<span>Slice3Image4</span>
</div>
<div>
<span>Slice4Image1</span>
<span>Slice4Image2</span>
http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

4/21

7/2/2015

SlidingImagePanelswithCSS3

<span>Slice4Image3</span>
<span>Slice4Image4</span>
</div>
</div>
<!titles>
<divclass="crtitles">
<h3>
<span>Serendipity</span>
<span>Whatyou'vebeendreamingof</span>
</h3>
<h3>
<span>Adventure</span>
<span>Wherethefunbegins</span>
</h3>
<h3>
<span>Nature</span>
<span>Unforgettableeperiences</span>
</h3>
<h3>
<span>Serenity</span>
<span>Whensilencetouchesnature</span>
</h3>
</div>
</section>

http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

5/21

7/2/2015

SlidingImagePanelswithCSS3

Theh3elementsforthetitleswillhavetwospans,oneforthemainheadlineandoneforthesub
headline.
Letsstylethisbaby.

TheCSS
Iwillomitallthevendorprefixes,butyouwill,ofcourse,findtheminthefiles.
Wellbegoingthroughthestyleofdemo1.
Ouraimistofirsthidethoseradiobuttonsbycoveringthemupwithlabels.Inwebbrowsers,
clickingonalabelwillmaketherespectivecheckboxorradiobuttonselected.GivinganIDtothe
input,wecanusethefor=idrefattributeofthelabeltoreferencetherespectiveinput.
Second,wewanttoplaceallthebackgroundimagescorrectlyandthird,wewanttoshowthe
respectiveimageslicesandtitleswhenclickingonalabel.
So,letsfirstsylethesectionandgiveitawhiteborderwithsomesubtleboxboxshadow:

http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

6/21

7/2/2015

SlidingImagePanelswithCSS3

.crcontainer{
width:600px;
height:400px;
position:relative;
margin:0auto;
border:20pxsolid#fff;
boxshadow:1px1px3pxrgba(0,0,0,0.1);
}

Sincewewanttousethegeneralsiblingselectorinordertoreachtherightimageslicesand
titles,weneedtoplacethelabelsbeforethosecontainers.Letsmakesurethattheyareontopas
alayer(zindex)andpushitspositiondownbyaddingatopmarginof350px

http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

7/21

7/2/2015

SlidingImagePanelswithCSS3

.crcontainerlabel{
fontstyle:italic;
width:150px;
height:30px;
cursor:pointer;
color:#fff;
lineheight:32px;
fontsize:24px;
float:left;
position:relative;
margintop:350px;
zindex:1000;
}

Letsprettifythelabelbyaddingalittlecircle.Wellcreateapseudoelementandplaceitcentered
behindthelabeltext:

http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

8/21

7/2/2015

SlidingImagePanelswithCSS3

.crcontainerlabel:before{
content:'';
width:34px;
height:34px;
background:rgba(130,195,217,0.9);
position:absolute;
left:50%;
marginleft:17px;
borderradius:50%;
boxshadow:0px0px0px4pxrgba(255,255,255,0.3);
zindex:1;
}

Inordertocreateaseparationbetweenthepanelswellusealittletrick.Wellcreateanother
pseudoelementforthelabelandextendittostretchoverthepanel.Usingagradient,wellmake
thelinefadeoutatthetop:

http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

9/21

7/2/2015

SlidingImagePanelswithCSS3

.crcontainerlabel:after{
width:1px;
height:400px;
content:'';
background:lineargradient(top,rgba(255,255,255,0)0%,rgba(255,255,255,1)100%);
position:absolute;
bottom:20px;
right:0px;
}

Thelastpanelshouldnothavethatlinesowesimplygiveit0width:
.crcontainerlabel.crlabelimg4:after{
width:0px;
}

Now,thatwevetakencareofthelabellook,wecanhidetheinputs:
.crcontainerinput{
display:none;
}

Wheneverweclickonalabel,therespectiveinputgetschecked.Nowwecantargetthe
http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

10/21

7/2/2015

SlidingImagePanelswithCSS3

respectivelabelusingthegeneralsiblingselector.So,wewillchangethecolortheselected
label:
.crcontainerinput.crselectorimg1:checked~label.crlabelimg1,
.crcontainerinput.crselectorimg2:checked~label.crlabelimg2,
.crcontainerinput.crselectorimg3:checked~label.crlabelimg3,
.crcontainerinput.crselectorimg4:checked~label.crlabelimg4{
color:#68abc2;
}

Andwellalsochangethebackgroundcolorandboxshadowofitsciclepseudoelement:
.crcontainerinput.crselectorimg1:checked~label.crlabelimg1:before,
.crcontainerinput.crselectorimg2:checked~label.crlabelimg2:before,
.crcontainerinput.crselectorimg3:checked~label.crlabelimg3:before,
.crcontainerinput.crselectorimg4:checked~label.crlabelimg4:before{
background:#fff;
boxshadow:0px0px0px4pxrgba(104,171,194,0.6);
}

Thecontainerfortheimagepanelswilloccupyallthewidthanditwillbepositionedabsolutely.
Thiscontainerwillbeusedlaterinordertosetthebackgroundimagetothecurrentlyselected
image.Weneedtodothisinordertohaveanimagevisiblebydefault.Sowellalsoaddsome
backgroundpropertiesalready:
http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

11/21

7/2/2015

SlidingImagePanelswithCSS3

.crbgimg{
width:600px;
height:400px;
position:absolute;
left:0px;
top:0px;
zindex:1;
backgroundrepeat:norepeat;
backgroundposition:00;
}

Sincewehavefourpanels/images,onepanelwillhavethewidthof150pixels(600dividedby4).
Thepanelswillbefloatingleftandwellhidetheiroverflowsincewedontwanttoseetheslices
comingoutwhenweslidethem:
.crbgimgdiv{
width:150px;
height:100%;
position:relative;
float:left;
overflow:hidden;
backgroundrepeat:norepeat;
}

Eachslicespanwillbepositionedabsolutelyandinitially,theywillbehiddenbyplacingthemout
http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

12/21

7/2/2015

SlidingImagePanelswithCSS3

ofthepanelwithaleftof150px:
.crbgimgdivspan{
position:absolute;
width:100%;
height:100%;
top:0px;
left:150px;
zindex:2;
textindent:9000px;
}

Now,letstakecareofthebackgroundoftheimagecontainerandtherespectiveimageslices:

http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

13/21

7/2/2015

SlidingImagePanelswithCSS3

.crcontainerinput.crselectorimg1:checked~.crbgimg,
.crbgimgdivspan:nthchild(1){
backgroundimage:url(../images/1.jpg);
}
.crcontainerinput.crselectorimg2:checked~.crbgimg,
.crbgimgdivspan:nthchild(2){
backgroundimage:url(../images/2.jpg);
}
.crcontainerinput.crselectorimg3:checked~.crbgimg,
.crbgimgdivspan:nthchild(3){
backgroundimage:url(../images/3.jpg);
}
.crcontainerinput.crselectorimg4:checked~.crbgimg,
.crbgimgdivspan:nthchild(4){
backgroundimage:url(../images/4.jpg);
}

Wealsoneedtogivetherightbackgroundpositiontotheslicesdependingonthepanel:

http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

14/21

7/2/2015

SlidingImagePanelswithCSS3

.crbgimgdiv:nthchild(1)span{
backgroundposition:0px0px;
}
.crbgimgdiv:nthchild(2)span{
backgroundposition:150px0px;
}
.crbgimgdiv:nthchild(3)span{
backgroundposition:300px0px;
}
.crbgimgdiv:nthchild(4)span{
backgroundposition:450px0px;
}

Whenweclickonalabelwewillsimplyslidealltheslicesouttotheright:

http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

15/21

7/2/2015

SlidingImagePanelswithCSS3

.crcontainerinput:checked~.crbgimgdivspan{
animation:slideOut0.6seaseinout;
}
@keyframesslideOut{
0%{
left:0px;
}
100%{
left:150px;
}
}

allexceptthesliceswiththerespectivebackgroundimage.Thosewillslideinfrom150pxto
0px:
.crcontainerinput.crselectorimg1:checked~.crbgimgdivspan:nthchild(1),
.crcontainerinput.crselectorimg2:checked~.crbgimgdivspan:nthchild(2),
.crcontainerinput.crselectorimg3:checked~.crbgimgdivspan:nthchild(3),
.crcontainerinput.crselectorimg4:checked~.crbgimgdivspan:nthchild(4)
{
transition:left0.5seaseinout;
animation:none;
left:0px;
zindex:10;
}
http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

16/21

7/2/2015

SlidingImagePanelswithCSS3

Last,butnotleast,wewanttostyletheh3titleelementsandtheirspans.Theh3willhavea
opacitytransitionandonceweselecttherespectivelabel/inputtheopacitywillincreasefrom0to
1:
.crtitlesh3{
position:absolute;
width:100%;
textalign:center;
top:50%;
zindex:10000;
opacity:0;
color:#fff;
textshadow:1px1px1pxrgba(0,0,0,0.1);
transition:opacity0.8seaseinout;
}
.crtitlesh3span:nthchild(1){
fontfamily:'BebasNeueRegular','ArialNarrow',Arial,sansserif;
fontsize:70px;
display:block;
letterspacing:7px;
}
.crtitlesh3span:nthchild(2){
letterspacing:0px;
display:block;
background:rgba(104,171,194,0.9);
fontsize:14px;
padding:10px;

http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

17/21

7/2/2015

SlidingImagePanelswithCSS3

padding:10px;
fontstyle:italic;
fontfamily:Cambria,Palatino,"PalatinoLinotype","PalatinoLTSTD",Georgia,serif;
}
.crcontainerinput.crselectorimg1:checked~.crtitlesh3:nthchild(1),
.crcontainerinput.crselectorimg2:checked~.crtitlesh3:nthchild(2),
.crcontainerinput.crselectorimg3:checked~.crtitlesh3:nthchild(3),
.crcontainerinput.crselectorimg4:checked~.crtitlesh3:nthchild(4){
opacity:1;
}

Ifyoudontwanttousethelabeltrickonmobiledevicesyoucould,forexample,useamedia
query:
@mediascreenand(maxwidth:768px){
.crcontainerinput{
display:inline;
width:24%;
margintop:350px;
zindex:1000;
position:relative;
}
.crcontainerlabel{
display:none;
}
}
http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

18/21

7/2/2015

SlidingImagePanelswithCSS3

Thisisjustaquicksolutionanditmightbebettertocheck,ifthelabeltrickissupported.
Andthatsit!Therearemanypossibilitiesofanimationsthatcanbedonehere.Checkoutthe
demostoseemore.

Demos
1. Demo1:Slidetoright
2. Demo2:Odd/evenslidetoleft/right
3. Demo3:Odd/evenslideup/down
4. Demo4:Scaleup/down
Andthatsit!Ihopeyouenjoyedthistutorialandfinditinspiring!

RelatedArticles

http://tympanus.net/codrops/2012/01/17/slidingimagepanelswithcss3/

19/21

Anda mungkin juga menyukai