javascript - 使用 100vh 定位 div

标签 javascript html css

在这段代码中,我保存了两个 div 元素,每个元素都设置在页面的整个长度 (100vh) 上,但不是一个在另一个之上,就好像我想重叠一样。部分问题得到解决,而不是在 keywords-js 中使用“绝对”,我将“静态”放在“静态”中,然后它们不会彼此重叠,但将动画粒子强加在带有 Content 的 div 上

HTML

<div id="particles-js">
</div>

<div class="content">
<h1>Content</h1>
</div>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/particles.js/2.0.0/particles.min.js"></script>

CSS

#particles-js {
  position: absolute;
    width: 100%;
    height: 100vh;
    z-index: -1;
  background-color: #222;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 50% 50%;
}


canvas{
    position: absolute;
    width: 100%;
    height: 500px;
    z-index: -1;
}

.content
{
  position: relative;
    height: 100vh;
    background-color: #F4F7F5;
    color: #222222;
  }

和JS

particlesJS("particles-js", 
{
  "particles": {
    "number": {
      "value": 50,
      "density": {
        "enable": true,
        "value_area": 800
      }
    },
    "color": {
      "value": "#ffffff"
    },
    "shape": {
      "type": "circle",
      "stroke": {
        "width": 0,
        "color": "#000000"
      },
      "polygon": {
        "nb_sides": 5
      },
      "image": {
        "src": "img/github.svg",
        "width": 100,
        "height": 100
      }
    },
    "opacity": {
      "value": 0.5,
      "random": false,
      "anim": {
        "enable": false,
        "speed": 1,
        "opacity_min": 0.1,
        "sync": false
      }
    },
    "size": {
      "value": 3,
      "random": true,
      "anim": {
        "enable": false,
        "speed": 40,
        "size_min": 0.1,
        "sync": false
      }
    },
    "line_linked": {
      "enable": true,
      "distance": 150,
      "color": "#ffffff",
      "opacity": 0.4,
      "width": 1
    },
    "move": {
      "enable": true,
      "speed": 3,
      "direction": "none",
      "random": false,
      "straight": false,
      "out_mode": "out",
      "bounce": false,
      "attract": {
        "enable": false,
        "rotateX": 600,
        "rotateY": 1200
      }
    }
  },
  "interactivity": {
    "detect_on": "canvas",
    "events": {
      "onhover": {
        "enable": false,
        "mode": "repulse"
      },
      "onclick": {
        "enable": false,
        "mode": "push"
      },
      "resize": true
    },
    "modes": {
      "grab": {
        "distance": 527.4388051113614,
        "line_linked": {
          "opacity": 1
        }
      },
      "bubble": {
        "distance": 599.3622785356379,
        "size": 63.93197637713472,
        "duration": 5.354303021585032,
        "opacity": 0.30367688779138985,
        "speed": 3
      },
      "repulse": {
        "distance": 271.7108996028225,
        "duration": 0.4
      },
      "push": {
        "particles_nb": 4
      },
      "remove": {
        "particles_nb": 2
      }
    }
  },
  "retina_detect": true
});

最佳答案

这是你想要的吗?

particlesJS("particles-js", {
  "particles": {
    "number": {
      "value": 50,
      "density": {
        "enable": true,
        "value_area": 800
      }
    },
    "color": {
      "value": "#ffffff"
    },
    "shape": {
      "type": "circle",
      "stroke": {
        "width": 0,
        "color": "#000000"
      },
      "polygon": {
        "nb_sides": 5
      },
      "image": {
        "src": "img/github.svg",
        "width": 100,
        "height": 100
      }
    },
    "opacity": {
      "value": 0.5,
      "random": false,
      "anim": {
        "enable": false,
        "speed": 1,
        "opacity_min": 0.1,
        "sync": false
      }
    },
    "size": {
      "value": 3,
      "random": true,
      "anim": {
        "enable": false,
        "speed": 40,
        "size_min": 0.1,
        "sync": false
      }
    },
    "line_linked": {
      "enable": true,
      "distance": 150,
      "color": "#ffffff",
      "opacity": 0.4,
      "width": 1
    },
    "move": {
      "enable": true,
      "speed": 3,
      "direction": "none",
      "random": false,
      "straight": false,
      "out_mode": "out",
      "bounce": false,
      "attract": {
        "enable": false,
        "rotateX": 600,
        "rotateY": 1200
      }
    }
  },
  "interactivity": {
    "detect_on": "canvas",
    "events": {
      "onhover": {
        "enable": false,
        "mode": "repulse"
      },
      "onclick": {
        "enable": false,
        "mode": "push"
      },
      "resize": true
    },
    "modes": {
      "grab": {
        "distance": 527.4388051113614,
        "line_linked": {
          "opacity": 1
        }
      },
      "bubble": {
        "distance": 599.3622785356379,
        "size": 63.93197637713472,
        "duration": 5.354303021585032,
        "opacity": 0.30367688779138985,
        "speed": 3
      },
      "repulse": {
        "distance": 271.7108996028225,
        "duration": 0.4
      },
      "push": {
        "particles_nb": 4
      },
      "remove": {
        "particles_nb": 2
      }
    }
  },
  "retina_detect": true
});
body {
margin:0;
}
#particles-js {
  position:relative;
  width: 100%;
  height: 100vh;
  background-color: #222;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 50% 50%;
}

canvas {
  position: absolute;
  width: 100%;
  height: 100%;
}

.content {

  background:#000;
  height: 100vh;
  color: #fff;
}
.content h1 {
  margin-top:0;
}
<div id="particles-js">
</div>

<div class="content">
  <h1>Content</h1>
</div>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/particles.js/2.0.0/particles.min.js"></script>

关于javascript - 使用 100vh 定位 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48104292/

相关文章:

javascript - 在 Wordpress 中随机选择颜色

javascript - 具有成功外观的 jQuery 验证组

javascript - 创建具有某些属性的表的 JQuery 函数

javascript - 在浏览器中显示 mp4 流媒体视频

html - 具有唯一子元素的父元素具有高度,即使唯一子元素的所有填充、边距、高度和宽度都设置为零

html - 表元素中允许的表单元素在哪里?

html - 如何使 <li> 根据其内容自动调整大小?

javascript - 如何过滤 GET 请求?

javascript - 如何倾斜/旋转在javascript拖放中被拖动的项目?

JavaScript - 获取 csv 列值