Skip to content Skip to sidebar Skip to footer

Jquery: Nesting Letter Transform Function

This is the code: If you click the first div, the letters from #one and #two start moving. That's exactly how it should look like. But then: If you want to get the previous state,

Solution 1:

You can add click event on #two to do that as below -

$('#two').click(function() {
if (isRandom) {
  isRandom = false;
  $('span').each(function() {
    $(this).css({
      "position": "relative"
    });
    $(this).animate({
      left: 0,
      top: 0,
    });
  });
}
});

Final code (I have extracted into functions for better understanding) -

$(document).ready(function() {
  var isRandom = false;
  $('#one, #two').html(function(i, html) {
    var chars = $.trim(html).split("");
    return'<span>' + chars.join('</span><span>') + '</span>';
  });

  $('#one').click(function() {
    isRandom = !isRandom;
    if (isRandom) {
      fly();
    } else {
      restore();
    }
  });

  $('#two').click(function() {
    if (isRandom) {
      isRandom = false;
      restore();
    }
  });

  functionfly() {
    $('span').each(function() {
      $(this).css({
        "position": "absolute"
      });
      $(this).animate({
        left: Math.random() * window.outerWidth / 2,
        top: Math.random() * window.outerHeight / 2,
      });
    });
  }

  functionrestore() {
    $('span').each(function() {
      $(this).css({
        "position": "relative"
      });
      $(this).animate({
        left: 0,
        top: 0,
      });
    });
  }
});
html,
body {
  width: 100%;
  height: 100%;
}

div {
  font-family: Arial;
  font-size: 20px;
  text-transform: uppercase;
}
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><divid="one">Click me</div><divid="two">Flying letters based on #one</div><div>No flying letters</div>

Solution 2:

You can simply change your selector to some class which is same to all divs then inside event handler you just need to check if the div which is clicked has id has one using && if yes then only if part will get executed .

Demo Code :

$(document).ready(function() {
  var isRandom = false;
  $('#one, #two').html(function(i, html) {
    var chars = $.trim(html).split("");
    return'<span>' + chars.join('</span><span>') + '</span>';
  });
  //chnage slector
  $('div.sameclass').click(function() {
    isRandom = !isRandom;
    //check if the div which is clicked has id one..if (isRandom && $(this).attr("id") == "one") {
      $('span').each(function() {
        $(this).css({
          "position": "absolute"
        });
        $(this).animate({
          left: Math.random() * window.outerWidth / 2,
          top: Math.random() * window.outerHeight / 2,
        });
      });
    } else {
      $('span').each(function() {
        $(this).css({
          "position": "relative"
        });
        $(this).animate({
          left: 0,
          top: 0,
        });
      });
    }
  });
});
html,
body {
  width: 100%;
  height: 100%;
}

div {
  font-family: Arial;
  font-size: 20px;
  text-transform: uppercase;
}
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><divid="one"class="sameclass">Click me</div><divid="two"class="sameclass">Flying letters based on #one</div><divclass="sameclass">No flying letters</div>

Post a Comment for "Jquery: Nesting Letter Transform Function"