Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Adding jQuery to DIV element?

I've got this bit of code I'd like to add and restrict to a single DIV classed .third However, whenever I link the bit of code, it doesn't work. If I embed the code within the div, it works but it's restricted to the top of the page. Here's the HTML:

<div class="third">
</div>

And here's the jQuery:

function Particle() {
    this.path = 'images/';
    this.images = ['particle1.svg', 'particle2.svg'];
    //Randomly pick a particle model
    this.image = this.images[randomInt(this.images.length)];
    this.file = this.path + this.image;
    //Create a DOM for this model
    this.element = document.createElement('img');
    //A sequence of Core Actions to Take:
    this.speed().newPoint().display().newPoint().fly();
  }
  //Generate a random speed
  Particle.prototype.speed = function() {
    this.duration = (randomInt(10) + 5) * 3000;
    return this;
  };
  //Generate a random position
  Particle.prototype.newPoint = function() {
    this.pointX = randomInt(window.innerWidth - 100);
    this.pointY = randomInt(window.innerHeight - 100);
    return this;
  };
  //Display the particle
  Particle.prototype.display = function() {
    $(this.element)
        .attr('src', this.file)
        .css('position', 'absolute')
        .css('top', this.pointY)
        .css('left', this.pointX);
    $(document.body).append(this.element);
    return this;
};
  //Animate the particle movement
  Particle.prototype.fly = function() {
      var self = this;
      $(this.element).animate({
          'top': this.pointY,
          'left': this.pointX,
      }, this.duration, 'linear', function(){
          self.speed().newPoint().fly();
      });
  };


  function randomInt(max) {
    // Generate a random integer (0 <= random < max)
    return Math.floor(Math.random() * max);
  }

$(function() {
  var total = 20;
  var particles = [];
  for (i = 0; i < total; i++) {
    particles[i] = new Particle();
  }


})

This awesome effect was brought to you by Piao Yishi.

2 Answers

Steven Parker
Steven Parker
243,318 Points

I'm not sure what you mean by "whenever I link the bit of code, it doesn't work." Maybe this would be best demonstrated if you make a snapshot of your workspace and provide the link.

:point_right: But to restrict particle movement to within your third div, you could do this:

Particle.prototype.newPoint = function() {
  this.pointX = randomInt($(".third").width() - 5) + $(".third").position().left;
  this.pointY = randomInt($(".third").height() - 5) + $(".third").position().top;
  return this;
};

Then, you just need to give your div explicit dimensions (an empty div will have no height by default).

I reduced the size offsets to 5, assuming your particles are the same size as the first two in the "Firefly Effect" article. Readjust them if not.

Steven Parker has a good solution there. Without seeing all of your code and structure, it's difficult to tell what might work best for you.

As another option, you could use offset() and inner dimension values. In contrast to position(), offset() is relative to the document as opposed to the offset parent. Depending on your div styles, you may want to use innerWidth() and innerHeight() if you want to include padding.

// Keep in mind the subtraction number is related to your particle sizes, so adjust accordingly.
this.pointX = $('.third').offset().left + randomInt($('.third').innerWidth() - 32);
this.pointY = $('.third').offset().top + randomInt($('.third').innerHeight() - 32);
return this;

Hopefully with two different options you'll feel better prepared to customize your code to work with your document. Happy coding! :)

Edit: See comments below for some updates.

Steven Parker
Steven Parker
243,318 Points

:information_source: The reason for using position is that the particles are positioned using position: absolute, and this keeps the relationship to the positioning context ("offset parent") consistent.

Using offset is not a good option because the particles will no longer be inside the third div if a new positioning context is created.

I didn't run into that issue, but you are probably right. :thumbsup: