页面滚动效果无法正常工作

时间:2018-06-04 05:22:37

标签: javascript html

页面滚动效果无法正常工作当我向下滚动它直接带我到那个得分部分而不是显示上面的整个内容 内容高于此显示,但当我做一个滚动它直接带我到scrolable部分 我希望它应该显示在整个内容之上,当我来到那个部分它开始scroing

(function(){
	window.addEventListener("load", function(){
		let rows = document.querySelectorAll("[data-row]");
		let dataRowArray = []; // store all the data-row values
		rows.forEach(function(row){
			dataRowArray.push(row.dataset.row)
		});
		document.querySelector("[data-active-row]").dataset.activeRow = dataRowArray[0];
		let debouncedScrollEvent = _.debounce(pageElementScroll, 100, {
		  	'leading': true,
		  	'trailing': false
		});
		window.addEventListener("wheel", debouncedScrollEvent, { passive: true });
		// console.log(dataRowArray);
		function pageElementScroll(e) {
			let slidingDirection = e.deltaY;
			let currentActiveRow = document.querySelector("[data-active-row]").dataset.activeRow;
			let nextRow = null;
			if (slidingDirection >= 0) { // sliding down
				// when sliding down
				// left div will slide up 
				// right div will slide down
				if ( currentActiveRow !== _.last(rows).dataset.row ) {
					nextRow = dataRowArray[dataRowArray.indexOf(currentActiveRow) + 1];
					document.querySelector("[data-active-row]").dataset.activeRow = nextRow;
					if ( nextRow === _.last(rows).dataset.row ) { // if the next row is the last row, which is the map, then we need to change the opacity
						_.last(rows).style.opacity = "1";
					}
					slideIn(nextRow, slidingDirection);
				}
				console.log("down");
			} else { // sliding up
				// when sliding up
				// left div will slide down
				// right div will slide up
				if ( currentActiveRow !== _.first(rows).dataset.row ) {
					nextRow = dataRowArray[dataRowArray.indexOf(currentActiveRow) - 1];
					document.querySelector("[data-active-row]").dataset.activeRow = nextRow;
					if ( currentActiveRow === _.last(rows).dataset.row ) { 
					// if the current row is the last row, which is the map, then we need to change the opacity
						_.last(rows).style.opacity = "0";
					}
					slideIn(currentActiveRow, slidingDirection);		
				}
				console.log("up");
			}
		}
		function slideIn(rowName, slidingDirection) {
			let row = document.querySelector(`[data-row="${rowName}"]`);
			// console.log(row);
			if (row.querySelector(".left") && row.querySelector(".right")) {
				let rowLeft = document.querySelector(`[data-row="${rowName}"] > .left`);
				let rowRight = document.querySelector(`[data-row="${rowName}"] > .right`);
				if (slidingDirection >= 0){
					rowLeft.style.transform = 'translate3d(0, 0, 0)';
					rowRight.style.transform = 'translate3d(0, 0, 0)';
				} else {
					rowLeft.style.transform = 'translate3d(0, 100%, 0)';
					rowRight.style.transform = 'translate3d(0, -100%, 0)';			
				}
			}
		}
	})
})();
body { margin: 0; padding: 0; overflow: hidden;}
.full-page {
	height: 100vh;
	width: 100vw;
	position: fixed;
	top: 0;
	left: 0;
	display: flex;
	justify-content: center;
	align-items: center;
}
.half-page {
	height: 100vh;
	width: 50vw;
	position: fixed;
	top: 0;
	transition: transform 0.5s;
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
}
.left { transform: translate3d(0, 100%, 0); left: 0;}
.right { transform: translate3d(0, -100%, 0); right: 0;}

/* will be enabled again */
.map {opacity: 0; transition: opacity 0.5s;} 

.grey { background-color: #333; }
.green { background-color: darkgreen; }
.red { background-color: darkred; }
.blue { background-color: cadetblue; }
.white { background-color: white; }
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Full Page Scrolling Effect</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>
<div style="width:100%;height:500px;">

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).



</div>
<div style="width:100%;height:500px;">

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).



</div>
  <div id="app" data-active-row>
		<div class="row" data-row="hero-row">
			<h2>Hero row</h2>
		</div>
		<div class="row " data-row=0 >
			<div class="left half-page red"><h1>Row 0 left</h1></div>
			<div class="right half-page red"><h1>Row 0 right</h1></div>
		</div>
		<div class="row " data-row=1>
			<div class="left half-page white"><h1>Row 1 left</h1></div>
			<div class="right half-page white"><h1>Row 1 right</h1></div>
		</div>
		<div class="full-page " data-row=2>
			<div class="left half-page green"><h1>Row 2 left</h1></div>
			<div class="right half-page green"><h1>Row 2 right</h1></div>
		</div>
		<div class="full-page" data-row=3>
			<div class="left half-page red"><h1>Row 3 left</h1></div>
			<div class="right half-page red"><h1>Row 3 right</h1></div>
		</div>
		<div class="full-page" data-row=4>
			<div class="left half-page blue"><h1>Row 4 left</h1></div>
			<div class="right half-page blue"><h1>Row 4 right</h1></div>
		</div>
		<div class="full-page map grey" data-row="map-row">
			<h1>Map</h1>
		</div>				
	</div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>

  

  




</body>

</html>

0 个答案:

没有答案