Goal:
Make a transition from non visibly to visibly when you scroll up.
Problem:
I have retrieved some info that it is not possibly due to syntax 'display'.
Is there another solution in order to achieve the goal?
Jsbin:
https://jsbin.com/nafarabimi/edit?css,js,output
Thank you!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="container">
<div id="top-content" >
<div id="testtest">
<img src="https://www.logolynx.com/images/logolynx/1f/1fa1c2a2ea0d69a85215d878f3a0652a.jpeg" width="100px" height="45px" />
</div>
<ul id="iconmenu" >
<li >Coffee</li>
<li >Tea</li>
<li >Milk</li>
</ul>
</div>
<div id="content" >
content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> <br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> <br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> c
</html>
body {
color: white;
text-align: center;
box-sizing: content-box;
margin: 0;
}
.palette-1 {
background-color: #83B2FF;
}
.palette-2 {
background-color: #8BF18B;
}
.palette-4 {
background-color: #FF8650;
}
.palette-5 {
background-color: #FF555E;
}
#content {
grid-area: content;
}
#footer {
grid-area: footer;
}
@media only screen and (min-width: 1000px) {
#container {
background-color: #FFE981;
width: 100%;
display: grid;
grid-template-areas: "left-header header right-header" "left-content content right-content" "left-footer footer right-footer";
row-gap: 1rem;
grid-template-columns: 1fr 1000px 1fr;
grid-template-rows: auto auto auto;
grid-gap: 5px;
box-sizing: border-box;
height: 100%;
background-color: #8cffa0;
}
#top-content {
overflow: hidden;
position: fixed;
width: 100%;
height: 50px; /* changed this from 50 to bigger height */
grid-area: header;
display: flex;
align-items: center;
justify-content: center;
}
#content {
margin-top: 50px;
}
#asdfasdf {
display: none;
}
ul.nav-menu {
margin: 0;
}
ul.nav-menu>li {
display: inline-block;
padding: 0 10px 0 10px;
margin: 0;
line-height: 70px;
border-top: 1px solid #83B2FF;
}
ul.nav-menu>li:hover {
background-color: #2779BF;
border-top: 1px solid #425c62;
}
}
@media only screen and (max-width: 999px) {
#container {
background-color: #FFE981;
width: 100%;
display: grid;
grid-template-areas: "header" "content" "footer";
row-gap: 1rem;
grid-template-columns: 1fr;
grid-template-rows: auto auto auto;
grid-gap: 5px;
box-sizing: border-box;
height: 200px;
background-color: #8cffa0;
}
#top-content {
grid-area: header;
display: flex;
align-items: center;
justify-content: center;
border-top: 3px solid #425c62;
}
#asdfasdf {
display: none;
}
ul.nav-menu {
margin: 0;
}
ul.nav-menu>li {
display: inline-block;
padding: 0 10px 0 10px;
margin: 0;
line-height: 70px;
border-top: 1px solid #83B2FF;
}
ul.nav-menu>li:hover {
background-color: #2779BF;
border-top: 1px solid #425c62;
}
}
window.onscroll = function() {
scrollFunction();
};
// get initial pageYOffset value
let originOffset = window.pageYOffset;
function scrollFunction() {
// current pageYOffset value
const curOffset = window.pageYOffset;
if (curOffset - originOffset > 0 && document.documentElement.scrollTop > 100) {
// down
document.getElementById('top-content').style.display = 'none';
document.getElementById("top-content").style.marginTop = "-50px";
} else {
// up
document.getElementById('top-content').style.display = 'flex'; // change this to flex
document.getElementById('top-content').style.transition = 'all 0.5s ease'; // change this to flex
document.getElementById("top-content").style.marginTop = "0";
}
// change originOffset value every time
originOffset = window.pageYOffset;
}
CodePudding user response:
display: none does not support transitions to another type.
Use opacity: 0 instead, which can transit to opacity: 1
CodePudding user response:
No You cannot transition with display:none
because you cannot transition from nothing to something. So Instead I have Used The Height of The Top-content
initially setting it to 0 and adding 50px on scroll because height can be transitioned.
i also removed document.getElementById("top-content").style.marginTop = "-50px";
you don't really need to do it..
note*:@media only screen and (min-width: 300px)
i changed it to 300 so we can see the transition as snippets are small by default.
window.onscroll = function() {
scrollFunction();
};
// get initial pageYOffset value
let originOffset = window.pageYOffset;
function scrollFunction() {
// current pageYOffset value
const curOffset = window.pageYOffset;
if (curOffset - originOffset > 0 && document.documentElement.scrollTop > 100) {
// down
document.getElementById('top-content').style.height = '0';
} else {
// up
document.getElementById('top-content').style.height = '50px'; // change this to flex
document.getElementById('top-content').style.transition = 'all 0.5s ease'; // change this to flex
}
// change originOffset value every time
originOffset = window.pageYOffset;
}
body {
color: white;
text-align: center;
box-sizing: content-box;
margin: 0;
}
.palette-1 {
background-color: #83B2FF;
}
.palette-2 {
background-color: #8BF18B;
}
.palette-4 {
background-color: #FF8650;
}
.palette-5 {
background-color: #FF555E;
}
#content {
grid-area: content;
}
#footer {
grid-area: footer;
}
@media only screen and (min-width: 300px) {
#container {
background-color: #FFE981;
width: 100%;
display: grid;
grid-template-areas: "left-header header right-header" "left-content content right-content" "left-footer footer right-footer";
row-gap: 1rem;
grid-template-columns: 1fr 1000px 1fr;
grid-template-rows: auto auto auto;
grid-gap: 5px;
box-sizing: border-box;
height: 100%;
background-color: #8cffa0;
}
#top-content {
overflow: hidden;
position: fixed;
width: 100%;
height: 50px;
/* changed this from 50 to bigger height */
grid-area: header;
display: flex;
align-items: center;
justify-content: center;
}
#content {
margin-top: 50px;
}
#asdfasdf {
display: none;
}
ul.nav-menu {
margin: 0;
}
ul.nav-menu>li {
display: inline-block;
padding: 0 10px 0 10px;
margin: 0;
line-height: 70px;
border-top: 1px solid #83B2FF;
}
ul.nav-menu>li:hover {
background-color: #2779BF;
border-top: 1px solid #425c62;
}
}
@media only screen and (max-width: 999px) {
#container {
background-color: #FFE981;
width: 100%;
display: grid;
grid-template-areas: "header" "content" "footer";
row-gap: 1rem;
grid-template-columns: 1fr;
grid-template-rows: auto auto auto;
grid-gap: 5px;
box-sizing: border-box;
height: 200px;
background-color: #8cffa0;
}
#top-content {
grid-area: header;
display: flex;
align-items: center;
justify-content: center;
border-top: 3px solid #425c62;
}
#asdfasdf {
display: none;
}
ul.nav-menu {
margin: 0;
}
ul.nav-menu>li {
display: inline-block;
padding: 0 10px 0 10px;
margin: 0;
line-height: 70px;
border-top: 1px solid #83B2FF;
}
ul.nav-menu>li:hover {
background-color: #2779BF;
border-top: 1px solid #425c62;
}
}
<body>
<div id="container">
<div id="top-content" >
<div id="testtest">
<img src="https://www.logolynx.com/images/logolynx/1f/1fa1c2a2ea0d69a85215d878f3a0652a.jpeg" width="100px" height="45px" />
</div>
<ul id="iconmenu" >
<li >Coffee</li>
<li >Tea</li>
<li >Milk</li>
</ul>
</div>
<div id="content" >
content1
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> <br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> <br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> c
</div>
CodePudding user response:
window.onscroll = function() {
scrollFunction();
};
// get initial pageYOffset value
let originOffset = window.pageYOffset;
function scrollFunction() {
// current pageYOffset value
const curOffset = window.pageYOffset;
if (curOffset - originOffset > 0 && document.documentElement.scrollTop > 100) {
// down
document.getElementById('top-content2').style.display = 'none';
document.getElementById("top-content").style.marginTop = "-50px";
document.getElementById("top-content").classList.add("is-scrollingUp");
} else {
// up
document.getElementById('top-content2').style.display = 'flex'; // change this to flex
document.getElementById('top-content').style.transition = 'all 0.5s ease'; // change this to flex
document.getElementById("top-content").style.marginTop = "0";
document.getElementById("top-content").classList.remove("is-scrollingUp");
}
// change originOffset value every time
originOffset = window.pageYOffset;
}
body {
color: white;
text-align: center;
box-sizing: content-box;
margin: 0;
}
.palette-1 {
background-color: #83B2FF;
}
.palette-2 {
background-color: #8BF18B;
}
.palette-4 {
background-color: #FF8650;
}
.palette-5 {
background-color: #FF555E;
}
#content {
grid-area: content;
}
#footer {
grid-area: footer;
}
.is-scrollingUp {
-webkit-transition: border-bottom-color .2s ease,-webkit-transform .2s ease;
transition: border-bottom-color .2s ease,-webkit-transform .2s ease;
-o-transition: transform .2s ease,border-bottom-color .2s ease;
transition: transform .2s ease,border-bottom-color .2s ease;
transition: transform .2s ease,border-bottom-color .2s ease,-webkit-transform .2s ease
}
@media only screen and (min-width: 1000px) {
#container {
background-color: #FFE981;
width: 100%;
display: grid;
grid-template-areas: "left-header header right-header" "left-content content right-content" "left-footer footer right-footer";
row-gap: 1rem;
grid-template-columns: 1fr 1000px 1fr;
grid-template-rows: auto auto auto;
grid-gap: 5px;
box-sizing: border-box;
height: 100%;
background-color: #8cffa0;
}
#top-content {
overflow: hidden;
position: fixed;
width: 100%;
grid-area: header;
align-items: center;
justify-content: center;
}
#top-content2 {
height: 50px; /* changed this from 50 to bigger height */
display: flex;
align-items: center;
justify-content: center;
}
#content {
margin-top: 50px;
}
#asdfasdf {
display: none;
}
ul.nav-menu {
margin: 0;
}
ul.nav-menu>li {
display: inline-block;
padding: 0 10px 0 10px;
margin: 0;
line-height: 70px;
border-top: 1px solid #83B2FF;
}
ul.nav-menu>li:hover {
background-color: #2779BF;
border-top: 1px solid #425c62;
}
}
@media only screen and (max-width: 999px) {
#container {
background-color: #FFE981;
width: 100%;
display: grid;
grid-template-areas: "header" "content" "footer";
row-gap: 1rem;
grid-template-columns: 1fr;
grid-template-rows: auto auto auto;
grid-gap: 5px;
box-sizing: border-box;
height: 200px;
background-color: #8cffa0;
}
#top-content {
grid-area: header;
display: flex;
align-items: center;
justify-content: center;
border-top: 3px solid #425c62;
}
#asdfasdf {
display: none;
}
ul.nav-menu {
margin: 0;
}
ul.nav-menu>li {
display: inline-block;
padding: 0 10px 0 10px;
margin: 0;
line-height: 70px;
border-top: 1px solid #83B2FF;
}
ul.nav-menu>li:hover {
background-color: #2779BF;
border-top: 1px solid #425c62;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="container">
<div id="top-content" >
<div id="top-content2">
<div id="testtest">
<img src="https://www.logolynx.com/images/logolynx/1f/1fa1c2a2ea0d69a85215d878f3a0652a.jpeg" width="100px" height="45px" />
</div>
<ul id="iconmenu" >
<li >Coffee</li>
<li >Tea</li>
<li >Milk</li>
</ul>
</div>
</div>
<div id="content" >
content0
<br /> content1
<br /> content2
<br /> content3
<br /> content4
<br /> content5
<br /> content6
<br /> content7
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> <br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> <br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> c
</html>
https://jsbin.com/mimiqebici/edit?html,css,output
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="container">
<div id="top-content" >
<div id="top-content2">
<div id="testtest">
<img src="https://www.logolynx.com/images/logolynx/1f/1fa1c2a2ea0d69a85215d878f3a0652a.jpeg" width="100px" height="45px" />
</div>
<ul id="iconmenu" >
<li >Coffee</li>
<li >Tea</li>
<li >Milk</li>
</ul>
</div>
</div>
<div id="content" >
content0
<br /> content1
<br /> content2
<br /> content3
<br /> content4
<br /> content5
<br /> content6
<br /> content7
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> <br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> <br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> content
<br /> c
</html>
body {
color: white;
text-align: center;
box-sizing: content-box;
margin: 0;
}
.palette-1 {
background-color: #83B2FF;
}
.palette-2 {
background-color: #8BF18B;
}
.palette-4 {
background-color: #FF8650;
}
.palette-5 {
background-color: #FF555E;
}
#content {
grid-area: content;
}
#footer {
grid-area: footer;
}
.is-scrollingUp {
-webkit-transition: border-bottom-color .2s ease,-webkit-transform .2s ease;
transition: border-bottom-color .2s ease,-webkit-transform .2s ease;
-o-transition: transform .2s ease,border-bottom-color .2s ease;
transition: transform .2s ease,border-bottom-color .2s ease;
transition: transform .2s ease,border-bottom-color .2s ease,-webkit-transform .2s ease
}
@media only screen and (min-width: 1000px) {
#container {
background-color: #FFE981;
width: 100%;
display: grid;
grid-template-areas: "left-header header right-header" "left-content content right-content" "left-footer footer right-footer";
row-gap: 1rem;
grid-template-columns: 1fr 1000px 1fr;
grid-template-rows: auto auto auto;
grid-gap: 5px;
box-sizing: border-box;
height: 100%;
background-color: #8cffa0;
}
#top-content {
overflow: hidden;
position: fixed;
width: 100%;
grid-area: header;
align-items: center;
justify-content: center;
}
#top-content2 {
height: 50px; /* changed this from 50 to bigger height */
display: flex;
align-items: center;
justify-content: center;
}
#content {
margin-top: 50px;
}
#asdfasdf {
display: none;
}
ul.nav-menu {
margin: 0;
}
ul.nav-menu>li {
display: inline-block;
padding: 0 10px 0 10px;
margin: 0;
line-height: 70px;
border-top: 1px solid #83B2FF;
}
ul.nav-menu>li:hover {
background-color: #2779BF;
border-top: 1px solid #425c62;
}
}
@media only screen and (max-width: 999px) {
#container {
background-color: #FFE981;
width: 100%;
display: grid;
grid-template-areas: "header" "content" "footer";
row-gap: 1rem;
grid-template-columns: 1fr;
grid-template-rows: auto auto auto;
grid-gap: 5px;
box-sizing: border-box;
height: 200px;
background-color: #8cffa0;
}
#top-content {
grid-area: header;
display: flex;
align-items: center;
justify-content: center;
border-top: 3px solid #425c62;
}
#asdfasdf {
display: none;
}
ul.nav-menu {
margin: 0;
}
ul.nav-menu>li {
display: inline-block;
padding: 0 10px 0 10px;
margin: 0;
line-height: 70px;
border-top: 1px solid #83B2FF;
}
ul.nav-menu>li:hover {
background-color: #2779BF;
border-top: 1px solid #425c62;
}
}
window.onscroll = function() {
scrollFunction();
};
// get initial pageYOffset value
let originOffset = window.pageYOffset;
function scrollFunction() {
// current pageYOffset value
const curOffset = window.pageYOffset;
if (curOffset - originOffset > 0 && document.documentElement.scrollTop > 100) {
// down
document.getElementById('top-content').style.display = 'none';
document.getElementById("top-content").style.marginTop = "-50px";
} else {
// up
document.getElementById('top-content').style.display = 'flex'; // change this to flex
document.getElementById('top-content').style.transition = 'all 0.5s ease'; // change this to flex
document.getElementById("top-content").style.marginTop = "0";
}
// change originOffset value every time
originOffset = window.pageYOffset;
}