如何从这些粒子中驱除我的鼠标

时间:2015-07-20 14:02:34

标签: javascript jquery particles

所以我在我的网站上有这些粒子,而且它都是Js。但是,我想做的事情,因为它与它背后的图像相互作用导致它们不可点击,就是当我将鼠标移动到它们附近时击退粒子。

我有以下粒子的代码,身体旁边是黑色,因为粒子只能在黑暗的图像中查看。

https://jsfiddle.net/sarumonin/60e1dmr5

function Particle() {
this.path = 'http://files.enjin.com/692771/Particles/';
this.images = ['particle1.png', 'particle2.png', 'particle3.png', 'particle4.png'];

//  Randomly Pick a Particle Model
    this.image = this.images[randomInt(this.images.length)];
    this.file = this.path + this.image;

//  Create a Particle DOM
    this.element = document.createElement('img');

    this.speed().newPoint().display().newPoint().fly();
};

//  Generate Random Speed
Particle.prototype.speed = function() {
    this.duration = (randomInt(10) + 5) * 1100;

    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(' pointer-events', 'none')
        .css('top', this.pointY)
        .css('left', this.pointX);
    $(document.body).append(this.element);

    return this;
};

//  Animate Particle Movements
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 <= randomInt < max)
    return Math.floor(Math.random() * max);
}

$(function(){
    var total = 50;
    var particles = [];

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

1 个答案:

答案 0 :(得分:3)

以下不是您要求的解决方案......但它无论如何都能解决您的问题:)

更改一个JS函数,如下所示:

//  Display the Particle
Particle.prototype.display = function() {
    $(this.element)
        .attr('src', this.file)
        .attr('class', 'ParticleNoMouse')
        .css('top', this.pointY)
        .css('left', this.pointX);
    $(document.body).append(this.element);
    return this;
};

...并将其添加到您的CSS文件中:

.ParticleNoMouse {
    position:absolute;
    -webkit-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;
    pointer-events:none;
}