'How do I make a text fade in animation for each div on scroll down page?
The problem is the animation keeps flashing or I have to implement $(this).off('scroll'); but that can not work as there are more div's below of which need a fade in animation. What is the workaround? I need a fade in animation for the bootstrap cards and id's "ux", "da", and "hs". (option 1, option 2, option 3)
$(window).scroll(function () {
console.log($(window).scrollTop()); // 0
var topDivHeight = $(".image").height();
console.log(topDivHeight); // 794
var viewPortSize = $(window).height();
console.log(viewPortSize); //844
var triggerAt = 709;
var triggerHeight = (topDivHeight - viewPortSize) + triggerAt; // 794 - 844 = -50 +709 = 659
if ($(window).scrollTop() >= triggerHeight) {
$('.noimage').css('visibility', 'visible').hide().fadeIn();
// $(this).off('scroll');
}
});
$(window).scroll(function () {
console.log($(window).scrollTop());
var topDivHeight2 = $(".noimage").height() + $(".image").height();
console.log(topDivHeight2);
var viewPortSize2 = $(window).height();
console.log(viewPortSize2);
var triggerAt2 = 700;
var triggerHeight2 = (topDivHeight2 - viewPortSize2) + triggerAt2;
if ($(window).scrollTop() >= triggerHeight2) {
$('#ux').css('visibility', 'visible').hide().fadeIn();
// $(this).off('scroll');
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Website</title>
<link rel="stylesheet" href="styles.css">
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
/>
</head>
<body>
<!-- As a heading -->
<nav class="navbar fixed-top navbar-light bg-light">
<div class="container-fluid">
<span class="navbar-brand mb-0 h1">Navbar</span>
</div>
</nav>
<div class="image container-fluid cont">
</div>
<div class = "noimage container-fluid d-flex align-items-center justify-content-around cont">
<div class="row cards justify-content-around">
<div class="card col-sm-3 col-md-12" style="width: 18rem;">
<img class="card-img-top" src="/images/desk.jpg" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card</h5>
<p class="card-text">Lorem ipsum dolor sit amet consectetur</p>
<a href="#ux" class="btn btn-primary">Go</a>
</div>
</div>
<div class="card col-sm-3 col-md-12" style="width: 18rem;">
<img class="card-img-top" src="/images/data.jpg" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
<p class="card-text">Lorem ipsum dolor sit amet consectetur</p>
<a href="#da" class="btn btn-primary">Go</a>
</div>
</div>
<div class="card col-sm-3 col-md-12" style="width: 18rem;">
<img class="card-img-top" src="/images/hosting.jpg" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card 3</h5>
<p class="card-text">lorem</p>
<a href="#hs" class="btn btn-primary">Go</a>
</div>
</div>
</div>
</div>
<div class="container-fluid d-flex justify-content-center overflow-auto align-items-center jumb cont" id="ux">
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-4">Option 1</h1>
<p class="lead">Lorem ipsum dolor sit, amet consectetur adipisicing elit.</p>
</div>
</div>
</div>
<div class="container-fluid d-flex justify-content-center overflow-auto align-items-center jumb cont" id="da">
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-4">Option 2</h1>
<p class="lead">Lorem ipsum dolor sit amet, amet consectetur adipisicing elit.</p>
</div>
</div>
</div>
<div class="container-fluid d-flex justify-content-center overflow-auto align-items-center jumb cont" id="hs">
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-4">Option 3</h1>
<p class="lead">Lorem ipsum dolor sit amet, amet consectetur adipisicing elit.</p>
</div>
</div>
</div>
<div class="container">
<footer class="py-0 my-0 bg-light fixed-bottom">
<ul class="nav justify-content-center border-bottom pb-0 mb-0">
<li class="nav-item"><a href="#" class="nav-link px-2 text-muted">Home</a></li>
<li class="nav-item"><a href="#ux" class="nav-link px-2 text-muted">Link 1</a></li>
<li class="nav-item"><a href="#da" class="nav-link px-2 text-muted">Link 2</a></li>
<li class="nav-item"><a href="#hs" class="nav-link px-2 text-muted">Link 3</a></li>
<!-- <li class="nav-item"><a href="#" class="nav-link px-2 text-muted">FAQs</a></li> -->
<li class="nav-item"><a href="contact.html" class="nav-link px-2 text-muted">Link</a></li>
</ul>
<p class="text-center text-muted py-4 mb-0">© 2021 Company, Inc</p>
</footer>
</div>
<script src="index.js"></script>
</body>
</html>
html {
height: 100%;
padding: 0;
margin: 0;
}
body {
min-height: 100%;
padding: 0;
margin: 0;
padding-top: 50px;
box-sizing: border-box;
overflow-x: hidden;
}
div.card {
width: 28% !important;
}
div.image {
padding-top: 50px;
min-height: 100vh;
background-image:url('/images/webdesign.jpg');
background-attachment:fixed;
background-repeat: no-repeat;
background-size: cover;
opacity: 0.8;
}
div.noimage {
min-height: 100vh;
background-image: none;
padding-top: 0px;
}
div.jumb {
min-height: 100vh;
/* margin: 0; */
/* padding: 0; */
margin-bottom: 145px;
/* margin-top: 50px; */
}
div#da {
/* background-color: var(--bs-secondary); */
background-color: #eee;
min-height: 100vh;
padding-bottom: 75px;
}
div#ux {
min-height: 100vh;
padding-bottom: 75px;
}
div#hs {
min-height: 100vh;
padding-bottom: 75px;
}
div.contact {
margin-top: -100px;
padding-top: 0px;
height: 700px;
}
footer {
bottom: 0;
/* padding-bottom: 145px; */
}
/* Small devices (landscape phones, less than 768px) */
@media (max-width: 767.98px) {
div.image {
display: none;
}
div.card {
width: 100vw !important;
}
footer {
position: relative;
}
.nav-item {
font-size: 10px;
}
div.contact {
margin-top: -100px;
height: 1100px;
}
div.icons {
display: none;;
padding-top: 30px;
}
/* div.cards {
display: flex;
flex-direction: column !important;
} */
}
Solution 1:[1]
All you have to do is make a function in javascript for fading text
slider();
function slider(){
document.getElementById("fadingtext").style.color = "rgba(0, 0, 0,"+ document.getElementById("fader").value + ")";
document.getElementById("fadingtext").style.top = document.getElementById("faderup").value +"px";
document.getElementById("fadingtext").style.position ="absolute";
setTimeout(slider,0);
}
#fadingtext{
color: rgba(0, 0, 0, 0.5);
}
<div id="fadingtext">Hello World!</div><br/>
Opacity:<input id="fader" value="1" min="0.0" max="1.0" step="0.01"type="range"/>
<br/>
Move:<input id="faderup" value="0" min="0" max="100" step="1"type="range"/>
This is an example of an opacity changer that you could possibly use. probably make it go up through a variable though
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RagDev |
