如何将缓动纳入滚动绑定功能?

时间:2018-10-18 11:40:28

标签: javascript html css animation scroll

我正在尝试进行垂直滚动。在此HTML上:

<div class="flex-container">
<div class="flex-caption">  
    <div>
        <p>.........</p>
        <h1>..........</h1>
    </div>
</div>
<div class="flex-caption">  
    <div>
        <p>.....</p>
    <h1>...........</h1>
    </div>
</div>
<div class="flex-caption">  
    <div>
        <p>.............</p>
        <h1>........../h1>
    </div>
</div>
</div>

所以我把CSS像这样:

.caption-2 {
overflow: hidden;
position: fixed;
}


.flex-caption {
float: left;
}

.flex-container {
display: inline-flex;
width: 300vw;
transition-timing-function: cubic-bezier(.84,.01,.25,1.01);
transform: translateX(0vw);
}

然后我用他的代码更改transform: translateX(0vw);

var captionCont, captionItem;

captionCont = document.querySelector(".flex-container");

window.onscroll = function() {scrollPost()};

function scrollPost () {

var y = window.scrollY;
var x = y / 20;
var i = x * 2;
animator(i);

}

function animator (y){
var captionCont;
captionCont = document.querySelector(".flex-container");
captionCont.style.transform = `translateX(${-y}vw)`
}

但是,我想尝试使这些元素滚动而不是滚动,所以它看起来更像是一种动画,而不仅仅是滚动。但是,我不希望触发动画,而是希望从1%到100%的动画受滚动约束。

我该怎么做?三次贝塞尔曲线似乎没什么作用。

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<script
  src="https://code.jquery.com/jquery-3.3.1.js"
  integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
  crossorigin="anonymous"></script>
	<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
	<link href="animation-library.css" rel="stylesheet">
	<title>Wubbe Site</title>
</head>
<body>



<!-- Page no.2 -->

<div class="full-page" id="page-2">
	<video autoplay muted loop id="myVideo">
  <source src="fleuropdemo.mp4" type="video/mp4">
	</video>

<div class="caption-2">
	<div class="flex-container">
	<div class="flex-caption">	
		<div>
			<p>FLEUROP / BERZORGT EEN GOED GEVOEL</p>
			<h1> HET GEVOEL VAN BLOEMEN ANDERS VERTELD</h1>
		</div>
	</div>
	<div class="flex-caption">	
		<div>
			<p>FLEUROP</p>
		<h1>TITEL VAN PROJECT VRAAG EN OPLOSSING IN EEN MOOIE ZIN</h1>
		</div>
	</div>
	<div class="flex-caption">	
		<div>
			<p>FLEUROP / BERZORGT EEN GOED GEVOEL</p>
			<h1> HET GEVOEL VAN BLOEMEN ANDERS VERTELD</h1>
		</div>
	</div>
	</div>

	<div class="btn-container">
		<button>BEKIJK DE CASE</button>
		<button>AL ONZE CASES</button>
	</div>
	</div>
</div>

<div class="full-page">
	
</div>

<div class="full-page">
	
</div>
	

<style type="text/css">

.caption-2 {
	overflow: hidden;
	position: fixed;
}


.flex-caption {
	float: left;
}

.flex-container {
	display: inline-flex;
	width: 300vw;
	transition-timing-function: cubic-bezier(.84,.01,.25,1.01);
	transform: translateX(0vw);
}

@import url('https://fonts.googleapis.com/css?family=Open+Sans:300,400,600');

body {	
	background-size: 100%, 100%;
  	text-align: center;
  	max-width: none;
  	font-family: 'Open Sans', sans-serif;
  	color: black;
  	margin: 0;
  	background-color: black;
}

* {
	box-sizing: border-box;
}

.margin-left {
	margin-left: 6.25vw;
}

.margin-right {
	margin-right: 6.25vw;
}

.margin-top {
	top: 5vw;
}

.full-page {
	width: 100vw;
	height: 100vh;
}

button {
	width: 15%;
	background-color: white;
	height: 4rem;
	color: black;
	font-size: 18px;
	font-weight: bold;
}


#myVideo {
    position: fixed;
    right: 0;
    bottom: 0;
    min-width: 100%; 
    min-height: 100%;
}


#page-2 {
	background-image: url(page-2.png);
	background-size: 100%, 100%;
	background-repeat: no-repeat;
	background-attachment: fixed;
	background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    display: flex;
	justify-content: center;
	align-items: center;

}


.caption-2 {
	color: white;
	height: 70vh;
	width: 100vw;

    top: 10vw;
    z-index: 1;

}

.caption-2 p {
	font-size: 30px;
	font-weight: bolder;
}

.caption-2 h1 {
	font-size: 80px;
	margin-left: 15%;
	margin-right: 15%;
}

.caption-2 button {
	top: 16vh;
	position: relative;
}




</style>

<script type="text/javascript">

var captionCont, captionItem;

captionCont = document.querySelector(".flex-container");

window.onscroll = function() {scrollPost()};
	
function scrollPost () {
	
	var y = window.scrollY;
	var x = y / 20;
	var i = x * 2;
	animator(i);
	
	}

function animator (y){
	var captionCont;
	captionCont = document.querySelector(".flex-container");
	captionCont.style.transform = `translateX(${-y}vw)`
}
	


</script>


</body>
</html>

2 个答案:

答案 0 :(得分:1)

我不明白您真正想要的是什么,因为您尚未添加过渡,因此片段中的立方贝塞尔曲线不起作用

var captionCont, captionItem;

captionCont = document.querySelector(".flex-container");

window.onscroll = function() {scrollPost()};
	
function scrollPost () {
	
	var y = window.scrollY;
	var x = y / 20;
	var i = x * 2;
	animator(i);
	
	}

function animator (y){
	var captionCont;
	captionCont = document.querySelector(".flex-container");
	captionCont.style.transform = `translateX(${-y}vw)`
}
	
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<script
  src="https://code.jquery.com/jquery-3.3.1.js"
  integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
  crossorigin="anonymous"></script>
	<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
	<link href="animation-library.css" rel="stylesheet">
	<title>Wubbe Site</title>
</head>
<body>



<!-- Page no.2 -->

<div class="full-page" id="page-2">
	<video autoplay muted loop id="myVideo">
  <source src="fleuropdemo.mp4" type="video/mp4">
	</video>

<div class="caption-2">
	<div class="flex-container">
	<div class="flex-caption">	
		<div>
			<p>FLEUROP / BERZORGT EEN GOED GEVOEL</p>
			<h1> HET GEVOEL VAN BLOEMEN ANDERS VERTELD</h1>
		</div>
	</div>
	<div class="flex-caption">	
		<div>
			<p>FLEUROP</p>
		<h1>TITEL VAN PROJECT VRAAG EN OPLOSSING IN EEN MOOIE ZIN</h1>
		</div>
	</div>
	<div class="flex-caption">	
		<div>
			<p>FLEUROP / BERZORGT EEN GOED GEVOEL</p>
			<h1> HET GEVOEL VAN BLOEMEN ANDERS VERTELD</h1>
		</div>
	</div>
	</div>

	<div class="btn-container">
		<button>BEKIJK DE CASE</button>
		<button>AL ONZE CASES</button>
	</div>
	</div>
</div>

<div class="full-page">
	
</div>

<div class="full-page">
	
</div>
	

<style type="text/css">

.caption-2 {
	overflow: hidden;
	position: fixed;
}


.flex-caption {
	float: left;
}

.flex-container {
	display: inline-flex;
	width: 300vw;
  transition: transform 0.3s linear;
	transform: translateX(0vw);
}

@import url('https://fonts.googleapis.com/css?family=Open+Sans:300,400,600');

body {	
	background-size: 100%, 100%;
  	text-align: center;
  	max-width: none;
  	font-family: 'Open Sans', sans-serif;
  	color: black;
  	margin: 0;
  	background-color: black;
}

* {
	box-sizing: border-box;
}

.margin-left {
	margin-left: 6.25vw;
}

.margin-right {
	margin-right: 6.25vw;
}

.margin-top {
	top: 5vw;
}

.full-page {
	width: 100vw;
	height: 100vh;
}

button {
	width: 15%;
	background-color: white;
	height: 4rem;
	color: black;
	font-size: 18px;
	font-weight: bold;
}


#myVideo {
    position: fixed;
    right: 0;
    bottom: 0;
    min-width: 100%; 
    min-height: 100%;
}


#page-2 {
	background-image: url(page-2.png);
	background-size: 100%, 100%;
	background-repeat: no-repeat;
	background-attachment: fixed;
	background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    display: flex;
	justify-content: center;
	align-items: center;

}


.caption-2 {
	color: white;
	height: 70vh;
	width: 100vw;

    top: 10vw;
    z-index: 1;

}

.caption-2 p {
	font-size: 30px;
	font-weight: bolder;
}

.caption-2 h1 {
	font-size: 80px;
	margin-left: 15%;
	margin-right: 15%;
}

.caption-2 button {
	top: 16vh;
	position: relative;
}




</style>




</body>
</html>

答案 1 :(得分:0)

包含jQuery.UI脚本,那么您的代码应类似于:

$('a').click(function(){
    var Top = 
$('body').find($(this).attr('href')).offset().top;
    $('html, body').animate({
        scrollTop: Top
    },300, 'easeOutExpo');

    return false;
});