定位div为100vh

时间:2018-01-04 22:07:17

标签: javascript html css

在这段代码中,我保存了两个div元素,每个元素都设置在页面的整个长度上(100vh),而不是一个在另一个之上,就好像我想要重叠一样。问题的一部分被解决而不是粒子-js中的“绝对”我把“静态”然后它们彼此不重叠,但动画粒子强加于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
});

2 个答案:

答案 0 :(得分:0)

这是你想要的吗?

&#13;
&#13;
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
});
&#13;
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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:-2)

对于js不好,但对于CSS来说,定位两个对象是错误的,因为它们都不能占用整个空间。我建议你使用绝对值和两个相对值的相对值,而不是使用它们来定位它们:left,right,top,bottom