使用CSS

时间:2017-11-10 14:36:51

标签: html css

我正在尝试为我的网页创建全屏部分,每个部分都有不同的颜色,事情是当我滚动时,没有办法显示部分以便颜色填满屏幕,我能做什么?改变,以便每个部分填满整个屏幕?

HTML:

<section class="first"> 

</section>

<section class="second">  

</section>
<section class="third">  

</section>

CSS:

body, html {
    height: 100%;
    margin: 0;
    overflow-x:hidden;
}

* {
  box-sizing: border-box;
}

section {
  width: 100%;
  display: table;
  margin: 0;
  max-width: none;
  height: 100vh;
}
.first {
  background-color:#834940;
}

.second {
  background-color:#291411;
}
.third {
  background-color:#834940;
}
/* Hide scrollbars */

html {
    overflow: scroll;
    overflow-x: hidden;
}
::-webkit-scrollbar {
    width: 0px;  
    background: transparent; 
}
::-webkit-scrollbar-thumb {
    background: #FF0000;
}

Example

2 个答案:

答案 0 :(得分:0)

DEMO https://codepen.io/ckor/pen/lBnxh

HTML

<section class="intro">  
  <div class="content">
    <h1>You can create full screen sections without javascript.</h1>
    <p>The height is set to 90vh, that means 90% height.</p>
 </div>
</section>

<section>  
  <div class="content">
    <h1>Resize your browser and see how they adapt.</h1>
 </div>
</section>

<section>  
  <div class="content">
    <h1>It's amazing and fast.</h1>
 </div>
</section>

<section>  
  <div class="content">
    <h1>See the <a href="http://caniuse.com/#feat=viewport-units">browser support.</a></h1>
 </div>
</section>

<强> SCSS

* {
  box-sizing: border-box; 
}

body {
  margin: 0; 
  font-weight: 500;
  font-family: 'HelveticaNeue';
}

section {
  width: 100%;
  padding: 0 7%;
  display: table;
  margin: 0;
  max-width: none;
  background-color: #373B44;
  height: 100vh;

  &:nth-of-type(2n) {
    background-color: #FE4B74;
  }
}

.intro {
  height: 90vh;
}

.content {
  display: table-cell;
  vertical-align: middle;
}

h1 {
  font-size: 3em;
  display: block;
  color: white;
  font-weight: 300;
}

p {
  font-size: 1.5em;
  font-weight: 500;
  color: #C3CAD9;
}

a {
  font-weight: 700;
  color: #373B44;
  position: relative;

  &:hover{ 
    opacity: 0.8;
  }

  &:active {
    top: 1px;
  }
}

footer {
  padding: 1% 5%;
  text-align:center;
  background-color: #373B44;
  color: white;

  a {
    color: #FE4B74;
    font-weight: 500;
    text-decoration: none;
  }
}

答案 1 :(得分:0)

它显示了一点点颜色,因为滚动量与屏幕高度不匹配,所以你滚动120px每&#34;滚动&#34;,3倍它的360px,如果屏幕是400px,它将显示第一部分的40px,另一个滚动和它的480px,现在你看到第三部分的80px。

通常,改变滚动行为并不是一个好主意,因此在使用此解决方案时请记住这一点。每次滚动它都会显示不同的部分。

它会侦听滚动事件,它会阻止它,然后滚动到下一部分。如果您的内容多于部分内容,请不要使用此功能。

&#13;
&#13;
window.onload = function () {
	var currentSectionIndex = 0;
	var sections = document.getElementsByClassName('section');

	function onScroll(e) {
		e.preventDefault();

		var scrollDir = (e.wheelDelta || -e.detail);
		if (scrollDir < 0) {
			currentSectionIndex++;
		} else {
			currentSectionIndex--;
		}

		if (currentSectionIndex < 0) {
			currentSectionIndex = 0;
		}
		if (currentSectionIndex >= sections.length) {
			currentSectionIndex = sections.length-1;
		}

		var section = sections[currentSectionIndex];
		if (section) {
			section.scrollIntoView();
		}
	}

	window.onmousewheel = onScroll;
};
&#13;
* {
    box-sizing: border-box;
}

body, html {
    height: 100%;
    margin: 0;
    overflow-x:hidden;
}

section {
    width: 100%;
    display: table;
    margin: 0;
    max-width: none;
    height: 100vh;
}

.first {
    background-color:#834940;
}
.second {
    background-color:#291411;
}
.third {
    background-color:#834940;
}


/* Hide scrollbars */
html {
    overflow: scroll;
    overflow-x: hidden;
}
::-webkit-scrollbar {
    width: 0px;  
    background: transparent; 
}
::-webkit-scrollbar-thumb {
    background: #FF0000;
}
&#13;
<section class="section first"></section>

<section class="section second"></section>

<section class="section third"></section>
&#13;
&#13;
&#13;