Nearlyallwebsitesneed a littlebitofdragondropfunctionality.
Sointhisvideo, I'm gonnashowyouhowtobuild a reallysimplesoredoubledragondropsystemusingjusthtmlCSSandbenevolentJavaScript.
Solet's getstartednow.
Butbeforewegetstarted, I wanttotellyouhowyoucangetanentireyearofhostingforfree.
Byusingtoday's videosponsoratlanticdotnethostingatlanticdotnetisgivingyouanentireyearoffreehostingontheirserversandtheseairpowerfulserversthataremorepowerfulthantheservers I hostmyownwebsiteon.
Soinordertogetstarted, I justhaveanemptyprojectOpenupinvisualstudiocode.
We'regoingtocreateanindexfilecalledindexdothtml.
We'regonnahave a stylesheetcalledStilesNaziSSjustforthesereallybasicstyles.
Andthenwe'realsogoingtohave a scriptdotJsfileforallofourdifferentJavaScriptAndnowinourindexdothtml, wejusthadexclamationpointhittaborenterisgoingtofilloutallofthisbullyof a plateforusandwecancomeinhereandlinkourstylesheet, whichiscalledStilesNaziSS.
Sowe'regonnahave a divwiththeclassofcontainer, whichisgoingtobethisblackbox.
Sowe'regonnahavetwoofthosedibsandtheninsideofthesedibs, we'regoingtohave a bunchofdifferent P tags, andthese P tagsaregoingtobe 123 orfour.
Sowe'regonnagivethem a classofdragonballsothatweknowthattheseairdrag a bleelementsandtheninordertomakesomethingdrag a bleusingJavascriptthere's anattributecalledDrag a Bowlandwejustneedtosetthistotrue.
Andthenwewanttogiveit a littlebitofPattyandsoulsayone R E M, andwe'llgiveit a littlemarginonthetopofone r e m justtospacetheseblackcontainersoutfromoneanother.
Andnowwecansliparedifferent, drag a bleelementsandinsideofourDragonballelements, we'regonnagivethem a littlepaddingontheirown, a backgroundcolor, whichisgoingtobewhite.
Wecandragallofthesedifferent p tagssowecanjustgetanelementcalledDragonBalls, whichisgoingtobeequaltodocumentdotqueryselectorofall.
Andweknowthattheseallhave a classofdragonball, sowecanjustusethatDragonballclassNext, we'regonnaneedtogetourdifferentcontainersbecausethecontainersarewherewe'reabletodropourdifferentelements.
Sowhenyou'redoingdraganddropwithHTmelonsJavaScript, youneedtoknowwhatyourdrag a bleelementsareinourcase, thesedifferent P tagsandyoualsoneedtoknowwhereyoucandroptheseelements.
Basedonthis, a Y positionthatwepassinandwecangetthisbypositionfromourevent.
So, actually, let's justsayconstantafterelementuphere, whichisgoingtobethereturnofthisgetdragafterelement, we'regonnasetthatequaltocallingthatfunctionpassinginourcurrentcontainerandthenthe e dotclient.
Ifyou'renotfamiliarwithit, I have a videocoveringit, whichyouconfinedinthecardsinthedescriptiondownbelow, butessentiallythisisgoingtotake a function, andthenitisalsogoingtotake a secondparameter, whichisessentiallytheinitialvalueofourreduce.
AndthenthenextperimeterisgoingtobeeachoneoftheseDragonballelementswilljustcallthischildbecauseeachoneofthemare a childofthecontainerwereinsideofSoforeachoneofourdragonelements, dysfunctionisgoingtobecalled.
Andthisisjustsothatinitiallywehave a numberthatiswaytoolarge, andeverysingleotherelementinthelistisgoingtobecloserthanthisnumber.
NotpositiveInfinity.
Nowinsideofthisfunction, weneedtodeterminetheactualpositionoftheelementsonourscreeninrelationtoourmousesowecangetwhat's calledtheboundingboxbyjustcallingherechilddotGetboundingclientwrecked, whichis a function, andthisisgoingtogiveus a rectanglefor a box.
Wecanjustseewegotdraggedherbalelements, gettingthatfromourqueryselector, andyounotice I forgottoput a periodhereforourclassselectorforDragonBall.
Sowhen I addthatinwhat's againInspectorpageoverourcounselAndifwedragthisyounowsee, we'regetting a bunchofthingsprintedouttheseair, therectanglesofourChildren.