Home › Forums › JavaScript › Click effect
- This topic is empty.
-
AuthorPosts
-
May 3, 2013 at 12:05 pm #44534
Kuzyo
ParticipantHi everyone.
Some times ago I saw the similar effect was implemented on jQuery and for learning goals (I am only beginner in JS) I decide to make it work on raw JS – http://codepen.io/Kuzyo/pen/LwyAEBut found some problems which I can’t solve alone: 1) how can I make circle to move back when I click again on orange circle 2) or when you click somewhere on blank place( window for example).
I aprreciate any advice. ThanksMay 3, 2013 at 12:32 pm #133974CrocoDillon
ParticipantQuick and dirty:
function toggleClass(e) {
if (e.target.classList.contains(“round”)) {
blue.classList.toggle(“blueRight”);
red.classList.toggle(“redLeft”);
green.classList.toggle(“greenDown”);
} else {
blue.classList.remove(“blueRight”);
red.classList.remove(“redLeft”);
green.classList.remove(“greenDown”);
}
}document.onclick = toggleClass;
I don’t like using `.onclick = …;`, rather use `.addEventListener(‘click’, …);`. Keep in mind that’s IE9+ though. But `.classList` is IE10+ so that’s no big deal here. I would probably restructure a thing or two to make the JS more robust and keep the styling in CSS (so no classes like `blueRight` or `redLeft`, but maybe just one class for all `expanded`)
May 3, 2013 at 5:05 pm #134010Kuzyo
ParticipantHi, CrocoDillon.
I know about unsemantic classes, it’s only for practicing. Your solving is cool (without variables and so on). I know that **classList** works only in newest browsers, but I don’t know how to remove class without **classList** :(((P.s. I owe for you help.
May 3, 2013 at 6:31 pm #134017CrocoDillon
Participant> I don’t know how to remove class without classList
Something with a RegEx replace on `.className` but I need to improve my RegEx skills to give you a more useful answer. You can Google it, but there are a lot wrong implementations out there.
> P.s. I owe for you help.
Yeap!
May 4, 2013 at 1:56 am #134029Kuzyo
ParticipantThanks, man!
I found thisfunction removeClass(ele,cls) {
if (hasClass(ele,cls)) {
var reg = new RegExp('(\s|^)'+cls+'(\s|$)');
ele.className=ele.className.replace(reg,' ');
}
}http://rockycode.com/blog/addremove-classes-raw-javascript/
and try in this way
function removeAllClases() {
function removeClass(ele,cls) {
if (hasClass(ele,cls)) {
var reg = new RegExp('(\s|^)'+cls+'(\s|$)');
ele.className=ele.className.replace(reg,' ');
}
}
removeClass( blue, "blueRight");
removeClass( red, "redLeft");
removeClass( blue, "greenDown")
}But it didn’t work.
May 4, 2013 at 3:49 am #134030Chris Coyier
KeymasterThe user and all related content has been deleted.
May 4, 2013 at 5:17 am #134037CrocoDillon
Participantfunction removeClass(ele,cls) {
if (hasClass(ele,cls)) {
var reg = new RegExp(‘(\s|^)’+cls+'(\s|$)’);
ele.className=ele.className.replace(reg,’ ‘);
}
}is using another function `hasClass`, you need that one too… or just leave that part out:
function removeClass(ele,cls) {
var reg = new RegExp(‘(\s|^)’+cls+'(\s|$)’);
ele.className=ele.className.replace(reg,’ ‘);
}May 4, 2013 at 5:24 am #134039Kitty Giraudel
ParticipantMay 4, 2013 at 10:11 am #134051Kuzyo
Participantthanks everything works fine :)
CrocoDillon, thanks for this link – http://www.learnstreet.com/, I went through their exercise, very helpful
May 4, 2013 at 12:27 pm #134054Kuzyo
ParticipantWhat is most important points in JS for web developer, on which I have to pay special attention in studying?
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.