javascript - How to add a class to only one div on click? -
so having 2 issues here , maybe poor execution in general please point me in right direction.
1.) regardless of how many black boxes there last 1 works correctly, meaning click black box , opens red box appears, can close box clicking dark area surrounding red box or red box itself. issue comes when click boxes before last box, opens expected when try close clicking red box opens instance of dark background, don't want happen.
2.) think deeper issue when click black box adding class "fart" .testthree divs instead of 1 area clicking , when click red box adding class "open" of other test divs.
so question is, there way contain classes added initial place click? want happen is:
i click workimg, test gets class of open, , testthree gets class of fart, workimg click on. when click anywhere closes nicely.
link fiddle:
http://jsfiddle.net/dkarasinski/l6gllyko/
html:
<div class="workcont"> <div class="workblock"> <div class="workimg"> <div class="test one"> <div class="testthree"></div> </div> <img src="/assets/images/piece1.jpg" /> </div> <div class="workname">project</div> </div> <div class="workblock"> <div class="workimg"> <div class="test one"> <div class="testthree"></div> </div> <img src="/assets/images/piece1.jpg" /> </div> <div class="workname">project</div> </div> </div>
css:
.workimg { background:#151515; width:330px; height:201px; display:inline-block; position: relative; } .test { position: fixed; top: 50%; left: 50%; z-index:100; width: 0; height: 0; -webkit-transition-duration: 300ms; -webkit-transition-property: all; -webkit-transition-timing-function: ease-in-out; text-align: center; background: white; color: white; font-family: sans-serif; /* 'cos */ } .test.open { top: 0; left: 0; width: 100%; height: 100%; position:fixed; color:black; background-color: rgba(0, 0, 0, 0.8); } .testthree { width:0; height:0; background-color: red; margin:auto; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); } .testthree.fart { width:50%; height:300px; } .testthree.close { display:none; } .workname { text-align:center; margin-top:17px; }
jquery / javascript:
$(document).ready(function(){ $(".workimg").click(function() { $(this).find(".test").toggleclass("open"); if ($(this).find(".test").hasclass("one")) { if($('.testthree').hasclass("fart")) { $(".testthree").removeclass("fart"); } else { settimeout(function(){ $( ".testthree" ).addclass( "fart" ); }, 500); } } }); });
replace code in else
block this:
var scope=$(this); settimeout(function(){ scope.find('.testthree').addclass('fart'); },500);
you needed scope work within , not apply fart
class all of .testthree
elements. hope find useful.
update: complete code may like:
$(document).ready(function () { $(".workimg").click(function () { var scope = $(this); var test = scope.find('.test'); var testthree = scope.find('.testthree'); test.toggleclass('open'); if (test.hasclass('one')) { if (testthree.hasclass('fart')) { testthree.removeclass('fart'); } else { settimeout(function () { testthree.addclass('fart'); }, 500); } } }); });
hope helps.
Comments
Post a Comment