简体   繁体   English

绝对元素粒子与其他元素重叠

[英]Absolute elements particles overlapping other elements issue

I placed this nifty particle script in my page for an interesting look and also to play with more intricate JS but have found that some of the particles which I made larger to to look like bokeh motes are floating over the image links causing them to be un-clickable which, while not a huge problem can be annoying. 我在页面上放置了这个漂亮的粒子脚本,使它看起来很有趣,也可以与更复杂的JS一起玩,但是发现我放大后看起来像散景微粒的一些粒子漂浮在图像链接上,导致它们变得不清晰。 -clickable,这虽然不是一个大问题,但可能会很烦人。

I'm wondering how to modify this script to have the particles be behind the image button links. 我想知道如何修改此脚本以使粒子位于图像按钮链接的后面。 There is no css except in the one section where it is applied by the script in the Display the particle section. 除了在“显示粒子”部分中的脚本所应用的那一部分外,没有css。

On a side note, I tried giving the image and link a z-index of 10000 just to see if that would help but still no go. 附带说明一下,我尝试给出图像并链接10000的z索引,以查看是否有帮助,但仍然行不通。

<script type="text/javascript">
  function Particle() {
   this.path = '../static/images/';
   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('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 = 30;
var particles = [];

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

Use .prepend (instead of .append ) 使用.prepend (而不是.append

$(document.body).prepend( this.element );

that way your absolute particles will be positioned underneath other positioned page elements due to the natural Z order DOM precedence. 这样,由于自然的Z阶DOM优先级,您的绝对粒子将位于其他定位的页面元素下方。

From the above you can understand that you also need to add position to your main page wrapper in order to make this work: 通过上面的内容,您可以了解到,还需要在首页包装器中添加position 才能完成此工作:

http://jsbin.com/cizut/1/edit?html,css,js,output http://jsbin.com/cizut/1/edit?html,css,js,output


Conclusion: 结论:

#mainPageWrapper{
  position:relative;
  /* .....other styles */
}

$(document.body).prepend( this.element );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM