I have this test page which I'm working on.
As you can see from the snippet, I have the "Share" bar rotated on the Z axis with transform: rotateZ(-90deg); and left-positioned with transform-origin:0 0;. (Note: this is the first time I use transform-origin and maybe I did something wrong(?)).
What I want to do:
When the page is scrolled up the "Share" bar should move to the left, outside the viewport, maintaining the transform: rotateZ(-90deg); property and when the page is scrolled down the "Share" bar should reappear in the same position. So it should stay shifted vertically when the transitions occur.
The problem:
When the page is scrolled up and the "Share" bar moves out of the viewport, the transform: translateX(-120px); property takes action as if the transform: rotateZ(-90deg); was never declared.
How can I make it stay vertically "fixed" during all the transitions, based on scrolling events?
ALSO: Why there is no overflow-x when the "Share" bar goes out the viewport?
Edit: I tried moving it with left and right, but in this case there's no transition time.
The snippet (Note: Go to the bottom of the css file for the .share element and its styling)
const burger = document.querySelector('.burger');
const nav = document.querySelector('nav');
burger.addEventListener('click', () => {
nav.classList.toggle('active');
burger.classList.toggle('hide');
xicon.classList.toggle('active');
});
const xicon = document.querySelector('.xicon');
xicon.addEventListener('click', function() {
nav.classList.toggle('active');
xicon.classList.toggle('active');
burger.classList.toggle('hide');
})
const header = document.querySelector('header');
let prev = window.pageYOffset;
window.addEventListener('scroll', function(){
let current = window.pageYOffset;
if(prev < current){
header.classList.add('hide');
}
else {
header.classList.remove('hide');
}
prev = current;
})
const shareThing = document.querySelector('.share');
let prevScroll = window.pageYOffset;
window.addEventListener('scroll', function() {
let currentScroll = window.pageYOffset;
if(prevScroll < currentScroll) {
shareThing.classList.add('hide');
icons.classList.add('hide');
}
else {
shareThing.classList.remove('hide');
icons.classList.remove('hide');
}
prevScroll = currentScroll;
})
let share = document.querySelector('.share');
let icons = document.querySelector('.side');
share.addEventListener('click', () => {
icons.classList.toggle('active');
})
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.logoandicon {
display: flex;
justify-content: space-between;
align-items: center;
background: powderblue;
padding: .7em 0;
}
.logo {
margin-left: .5em;
}
.burger {
margin-right: 1em;
z-index: 2;
}
.burger.hide {
display: none;
}
nav ul li a {
text-decoration: none;
color: black;
font-family: verdana;
}
nav ul li {
text-align: center;
list-style: none;
}
nav {
padding: 1em 0;
line-height: 2em;
background: lightgreen;
display: none;
}
nav.active {
display: block;
}
.xicon {
position: absolute;
right: 1em;
display: none;
}
.xicon.active {
display: block;
}
header {
width: 100vw;
position: fixed;
}
header.hide {
display: none;
}
main {
padding-top: 4em;
}
.one {
width: 20px;
height: 20px;
background: blue;
color: white;
}
.two {
width: 20px;
height: 20px;
background: green;
color: white;
}
.three {
width: 20px;
height: 20px;
background: red;
color: white;
}
.side {
width: 30px;
height: 100px;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
position: fixed;
top: 50%;
background: black;
display: none;
border-radius: 10px;
}
.side.active {
display: flex;
}
.side.hide {
display: none;
}
.share {
position: fixed;
top: 50%;
background: black;
color: white;
font-family: verdana;
font-size: 1.3rem;
transform: rotateZ(-90deg);
transform-origin: 0 0;
padding: .15em 1em;
border-radius: 10px;
border: 1px solid white;
transition: 1s;
}
.share.hide {
transform: translateX(-120px);
}
<header>
<div class="logoandicon">
<h1 class="logo">Logo</h1>
<img src="menu.svg" class="burger" width="30px" alt="burger-icon">
<img src="xicon.svg" class="xicon" width="27px" alt="x-icon">
</div>
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Contacts</li>
</ul>
</nav>
</header>
<main>
<p class="share">Share</p>
<div class="side">
<div class="one">Fb</div>
<div class="two">Ig</div>
<div class="three">Tw</div>
</div>
<h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Porro ut, animi iure sequi voluptatem maiores, aspernatur tempora, ipsa tempore, voluptate sint. Maiores quos inventore cumque reiciendis iusto illum, aut quam, suscipit temporibus veniam. Reprehenderit distinctio velit eveniet blanditiis, animi optio voluptas dolores repellendus ad itaque veritatis. Expedita deserunt, ab dolor accusamus sequi eum optio. Fuga dolorum, quod qui laboriosam iure molestiae magnam explicabo! Accusamus, alias, non. Eius voluptas in aspernatur natus illo et possimus optio, cupiditate saepe quisquam totam laboriosam ullam nihil consequatur rem earum ratione perspiciatis. Quo impedit ipsum assumenda suscipit unde inventore dolores quas? Ipsa aliquam sapiente rerum sit autem magni ad dolore iste vero optio saepe, impedit eveniet suscipit, deserunt. Modi architecto laudantium placeat beatae repellat harum deleniti facere deserunt officia unde mollitia error praesentium, sunt fugit recusandae soluta facilis earum voluptatem libero consectetur illum debitis quos! Odit unde, quo non blanditiis vitae aperiam obcaecati provident praesentium quas qui, veritatis enim accusamus iusto rerum harum porro maxime sit amet cum odio suscipit ipsum nulla adipisci. Totam consectetur quas ipsa. Natus officiis, eveniet reprehenderit neque rem sint eum, ratione, numquam blanditiis nisi culpa repudiandae nemo beatae. Ea cum officiis vel, sed distinctio, voluptatibus quibusdam! Soluta voluptate, non facilis?</h1>
</main>
<script src="main.js">
</script>
Your hide class should not only apply the translate, but also the rotation, like so:
.share.hide {
transform: translateX(-120px) rotateZ(-90deg);
}
Can you use left: -120px instead of transform: translate(-120px) ? You need absolute or relative position for this though. And when it should appear, set left: 0
SOLUTION
I managed to move it out of the viewport while maintaining the transform: rotateZ() proterty and the transition time, using:
margin-left: -35px;.
ALSO, as #Zohir Salak and #Turnip suggested in the comments above, chaining transform: rotateZ() with transform: translateX() on the same line, works as well.
const burger = document.querySelector('.burger');
const nav = document.querySelector('nav');
burger.addEventListener('click', () => {
nav.classList.toggle('active');
burger.classList.toggle('hide');
xicon.classList.toggle('active');
});
const xicon = document.querySelector('.xicon');
xicon.addEventListener('click', function() {
nav.classList.toggle('active');
xicon.classList.toggle('active');
burger.classList.toggle('hide');
})
const header = document.querySelector('header');
let prev = window.pageYOffset;
window.addEventListener('scroll', function(){
let current = window.pageYOffset;
if(prev < current){
header.classList.add('hide');
}
else {
header.classList.remove('hide');
}
prev = current;
})
const shareThing = document.querySelector('.share');
let prevScroll = window.pageYOffset;
window.addEventListener('scroll', function() {
let currentScroll = window.pageYOffset;
if(prevScroll < currentScroll) {
shareThing.classList.add('hide');
icons.classList.add('hide');
}
else {
shareThing.classList.remove('hide');
icons.classList.remove('hide');
}
prevScroll = currentScroll;
})
let share = document.querySelector('.share');
let icons = document.querySelector('.side');
share.addEventListener('click', () => {
icons.classList.toggle('active');
})
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.logoandicon {
display: flex;
justify-content: space-between;
align-items: center;
background: powderblue;
padding: .7em 0;
}
.logo {
margin-left: .5em;
}
.burger {
margin-right: 1em;
z-index: 2;
}
.burger.hide {
display: none;
}
nav ul li a {
text-decoration: none;
color: black;
font-family: verdana;
}
nav ul li {
text-align: center;
list-style: none;
}
nav {
padding: 1em 0;
line-height: 2em;
background: lightgreen;
display: none;
}
nav.active {
display: block;
}
.xicon {
position: absolute;
right: 1em;
display: none;
}
.xicon.active {
display: block;
}
header {
width: 100vw;
position: fixed;
}
header.hide {
display: none;
}
main {
padding-top: 4em;
}
.one {
width: 20px;
height: 20px;
background: blue;
color: white;
}
.two {
width: 20px;
height: 20px;
background: green;
color: white;
}
.three {
width: 20px;
height: 20px;
background: red;
color: white;
}
.side {
width: 30px;
height: 100px;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
position: fixed;
top: 50%;
background: black;
display: none;
border-radius: 10px;
}
.side.active {
display: flex;
}
.side.hide {
display: none;
}
.share {
position: fixed;
top: 50%;
background: black;
color: white;
font-family: verdana;
font-size: 1.3rem;
transform: rotateZ(-90deg);
transform-origin: 0 0;
padding: .15em 1em;
border-radius: 10px;
border: 1px solid white;
transition: 1s;
}
.share.hide {
margin-left: -35px;
}
/*OR
.share.hide {
transform: rotateZ(-90deg) translateX(-120px);
} /*
<!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">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<header>
<div class="logoandicon">
<h1 class="logo">Logo</h1>
<img src="menu.svg" class="burger" width="30px" alt="burger-icon">
<img src="xicon.svg" class="xicon" width="27px" alt="x-icon">
</div>
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Contacts</li>
</ul>
</nav>
</header>
<main>
<p class="share">Share</p>
<div class="side">
<div class="one">Fb</div>
<div class="two">Ig</div>
<div class="three">Tw</div>
</div>
<h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Porro ut, animi iure sequi vol uptatem maiores, aspernatur tempora, ipsa tempore, voluptate sint. Maiores quos inventore cumque reiciendis iusto illum, aut quam, suscipit temporibus veniam. Reprehenderit distinctio velit eveniet blanditiis, animi optio voluptas dolores repellendus ad itaque veritatis. Expedita deserunt, ab dolor accusamus sequi eum optio. Fuga dolorum, quod qui laboriosam iure molestiae magnam explicabo! Accusamus, alias, non. Eius voluptas in aspernatur natus illo et possimus optio, cupiditate saepe quisquam totam laboriosam ullam nihil consequatur rem earum ratione perspiciatis. Quo impedit ipsum assumenda suscipit unde inventore dolores quas? Ipsa aliquam sapiente rerum sit autem magni ad dolore iste vero optio saepe, impedit eveniet suscipit, deserunt. Modi architecto laudantium placeat beatae repellat harum deleniti facere deserunt officia unde mollitia error praesentium, sunt fugit recusandae soluta fuptatem maiores, aspernatur tempora, ipsa tempore, voluptate sint. Maiores quos inventore cumque reiciendis iusto illum, aut quam, suscipit temporibus veniam. Reprehenderit distinctio velit eveniet blanditiis, animi optio voluptas dolores repellendus ad itaque veritatis. Expedita deserunt, ab dolor accusamus sequi eum optio. Fuga dolorum, quod qui laboriosam iure molestiae magnam explicabo! Accusamus, alias, non. Eius voluptas in aspernatur natus illo et possimus optio, cupiditate saepe quisquam totam laboriosam ullam nihil consequatur rem earum ratione perspiciatis. Quo impedit ipsum assumenda suscipit unde inventore dolores quas? Ipsa aliquam sapiente rerum sit autem magni ad dolore iste vero optio saepe, impedit eveniet suscipit, deserunt. Modi architecto laudantium placeat beatae repellat harum deleniti facere deserunt officia unde mollitia error praesentium, sunt fugit recusandae soluta facilis earum voluptatem libero consectetur illum debitis quos! Odit unde, quo non blanditiis vitae aperiam obcaecati provident praesentium quas qui, veritatis enim accusamus iusto rerum harum porro maxime sit amet cum odio suscipit ipsum nulla adipisci. Totam consectetur quas ipsa. Natus officiis, eveniet reprehenderit neque rem sint eum, ratione, numquam blanditiis nisi culpa repudiandae nemo beatae. Ea cum officiis vel, sed distinctio, voluptatibus quibusdam! Soluta voluptate, non facilis?</h1>
</main>
<script src="main.js">
</script>
</body>
</html>
Related
This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 6 days ago.
I googled on some nice image sliders and stumbled across this one. When I put the javascript in a script tag in the html it works just fine, but when I try to put it in a .js file linked to the html it stops working and I can't figure out why. When I look at the console output, it says it can't read the "addEventListener" at line 10. Thanks in beforehand :)
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
overflow: hidden;
}
body {
background-color: #262626;
font-family: "Roboto Slab", serif;
position: relative;
overflow-x: hidden;
}
main {
width: 100vw;
height: 100vh;
}
#backdrop {
width: 90%;
height: 100%;
margin: 0 auto;
margin-top: 1rem;
border-radius: 1rem 1rem 0 0;
background-color: #727365;
}
#home-link {
color: #f2f2e4;
text-decoration: none;
font-size: 1.7rem;
font-family: "Space Mono", monospace;
font-weight: 400;
position: absolute;
top: 1.95rem;
left: 2.6rem;
}
/* Hamburger menu */
#menu a {
text-decoration: none;
color: #262626;
}
#menu a:hover {
color: #0c0c0c;
}
#menu-toggle {
display: block;
position: absolute;
top: 2.5rem;
right: 3rem;
z-index: 1;
}
#menu-checkbox {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
}
#menu-toggle span {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #f2f2e4;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1),
opacity 0.55s ease;
}
#menu-toggle span:first-child {
transform-origin: 0% 0%;
}
#menu-toggle span:nth-last-child(2) {
transform-origin: 0% 100%;
}
#menu-checkbox:checked ~ span {
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #3f403b;
}
#menu-checkbox:checked ~ span:nth-last-child(3) {
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}
#menu-checkbox:checked ~ span:nth-last-child(2) {
opacity: 1;
transform: rotate(-45deg) translate(0, -1px);
}
#menu {
width: 60vw;
height: 70vh;
position: absolute;
right: -100px;
margin: -100px 0 0 0;
padding: 50px;
padding-top: 125px;
border-radius: 1rem;
background-color: #bfbfae;
list-style: none;
transform-origin: 0% 0%;
transform: translate(100%, 0);
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1);
}
#menu li {
padding: 10px 0;
font-size: 22px;
}
#menu-checkbox:checked ~ ul {
transform: none;
opacity: 1;
}
/* footer */
#footer {
position: absolute;
top: 100vh;
}
/* Scrollbar */
::-webkit-scrollbar {
width: 13px;
}
::-webkit-scrollbar-thumb {
border-radius: 100px;
border: 3px solid transparent;
background-clip: content-box;
background-color: rgb(88, 88, 88);
}
::-webkit-scrollbar-thumb:hover {
background-color: rgb(109, 109, 109);
}
/* Media queries */
#media only screen and (min-width: 600px) {
}
#media only screen and (min-width: 769px) {
#backdrop {
margin-top: 2vh;
width: 98%;
height: 96.5%;
border-radius: 1rem;
}
#menu-checkbox {
display: none;
}
#menu-toggle span {
display: none;
}
#menu {
-webkit-transition: none !important;
-moz-transition: none !important;
-o-transition: none !important;
transition: none !important;
transform: none !important;
background-color: rgba(160, 160, 160, 0.568);
backdrop-filter: blur(15px);
padding: 0 1rem 0 1rem;
border-radius: 2rem;
font-family: monospace;
position: fixed;
top: 102vh;
right: 50%;
translate: 50% 0;
height: fit-content;
width: 45vw;
display: flex;
justify-content: space-between;
}
#menu li {
display: inline;
font-size: 1.2rem;
}
#active {
background-color: #f2f2e4;
border-radius: 3rem;
}
.menu-links {
padding: 8px 35px 8px 35px;
}
#home-link {
display: flex;
justify-content: center;
position: static;
padding-top: 0.5rem;
}
}
.slider{
position: relative;
background: #000116;
width: 800px;
min-height: 500px;
margin: 20px;
overflow: hidden;
border-radius: 10px;
}
.slider .slide{
position: absolute;
width: 100%;
height: 100%;
clip-path: circle(0% at 0 50%);
}
.slider .slide.active{
clip-path: circle(150% at 0 50%);
transition: 2s;
}
.slider .slide img{
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
}
.slider .slide .info{
position: absolute;
color: #222;
background: rgba(255, 255, 255, 0.3);
width: 75%;
margin-top: 50px;
margin-left: 50px;
padding: 20px;
border-radius: 5px;
box-shadow: 0 5px 25px rgb(1 1 1 / 5%);
}
.slider .slide .info h2{
font-size: 2em;
font-weight: 800;
}
.slider .slide .info p{
font-size: 1em;
font-weight: 400;
}
.navigation{
height: 500px;
display: flex;
align-items: center;
justify-content: space-between;
opacity: 0;
transition: opacity 0.5s ease;
}
.slider:hover .navigation{
opacity: 1;
}
.prev-btn, .next-btn{
z-index: 999;
font-size: 2em;
color: #222;
background: rgba(255, 255, 255, 0.8);
padding: 10px;
cursor: pointer;
}
.prev-btn{
border-top-right-radius: 3px;
border-bottom-right-radius: 3px;
}
.next-btn{
border-top-left-radius: 3px;
border-bottom-left-radius: 3px;
}
.navigation-visibility{
z-index: 999;
display: flex;
justify-content: center;
}
.navigation-visibility .slide-icon{
z-index: 999;
background: rgba(255, 255, 255, 0.5);
width: 20px;
height: 10px;
transform: translateY(-50px);
margin: 0 6px;
border-radius: 2px;
box-shadow: 0 5px 25px rgb(1 1 1 / 20%);
}
.navigation-visibility .slide-icon.active{
background: #4285F4;
}
#media (max-width: 900px){
.slider{
width: 100%;
}
.slider .slide .info{
position: relative;
width: 80%;
margin-left: auto;
margin-right: auto;
}
}
#media (max-width: 500px){
.slider .slide .info h2{
font-size: 1.8em;
line-height: 40px;
}
.slider .slide .info p{
font-size: 0.9em;
}
}
/*.mySlides {
display: none;
}
/* Slideshow container
.slideshow-container {
width: 100%;
height: 100%;
max-height: 100vh;
margin: auto;
}
/* The dots/bullets/indicators
.dot {
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active {
background-color: #717171;
}
/* On smaller screens, decrease text size
#media only screen and (max-width: 300px) {
.prev,
.next,
.text {
font-size: 11px;
}
}
/* Next & previous buttons
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}
/* Position the "next button" to the right
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Överdäckningen | Startsida</title>
<link rel="stylesheet" href="css/style.css" />
<script type="text/javascript" src="script/js.js"></script>
<link
href="https://fonts.googleapis.com/css2?family=Roboto+Slab:wght#200;300;400&display=swap"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Space+Mono&display=swap"
rel="stylesheet"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
/>
</head>
<!--
Göra så loggan försvinner när man scrollar ner och kommer tillbaka när man scrollar upp
navbar responsiv, desktop
mediaquery för ipad
bilder, text
göra så bildspelet passar in
anpassa alla bilder för skärmstorlek, picture element
(karta kanske?)
lägga rätt fonter på rubriker och text
-->
<body>
<main>
<div id="backdrop">
<div class="slider">
<div class="slide active">
<img src="imgs/1.jpg" alt="" />
<div class="info">
<h2>Winter Mountains</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
</div>
<div class="slide">
<img src="imgs/2.jpg" alt="" />
<div class="info">
<h2>Tropical Desert</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
</div>
<div class="slide">
<img src="imgs/3.jpg" alt="" />
<div class="info">
<h2>Steaming Volcanoes</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
</div>
<div class="slide">
<img src="imgs/4.jpg" alt="" />
<div class="info">
<h2>Mountain River</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
</div>
<div class="slide">
<img src="imgs/5.jpg" alt="" />
<div class="info">
<h2>Egypt Pyramids</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
</div>
<div class="navigation">
<i class="fas fa-chevron-left prev-btn"></i>
<i class="fas fa-chevron-right next-btn"></i>
</div>
<div class="navigation-visibility">
<div class="slide-icon active"></div>
<div class="slide-icon"></div>
<div class="slide-icon"></div>
<div class="slide-icon"></div>
<div class="slide-icon"></div>
</div>
</div>
<script type="text/javascript">
const slider = document.querySelector(".slider");
const nextBtn = document.querySelector(".next-btn");
const prevBtn = document.querySelector(".prev-btn");
const slides = document.querySelectorAll(".slide");
const slideIcons = document.querySelectorAll(".slide-icon");
const numberOfSlides = slides.length;
var slideNumber = 0;
//image slider next button
nextBtn.addEventListener("click", () => {
slides.forEach((slide) => {
slide.classList.remove("active");
});
slideIcons.forEach((slideIcon) => {
slideIcon.classList.remove("active");
});
slideNumber++;
if (slideNumber > numberOfSlides - 1) {
slideNumber = 0;
}
slides[slideNumber].classList.add("active");
slideIcons[slideNumber].classList.add("active");
});
//image slider previous button
prevBtn.addEventListener("click", () => {
slides.forEach((slide) => {
slide.classList.remove("active");
});
slideIcons.forEach((slideIcon) => {
slideIcon.classList.remove("active");
});
slideNumber--;
if (slideNumber < 0) {
slideNumber = numberOfSlides - 1;
}
slides[slideNumber].classList.add("active");
slideIcons[slideNumber].classList.add("active");
});
//image slider autoplay
var playSlider;
var repeater = () => {
playSlider = setInterval(function () {
slides.forEach((slide) => {
slide.classList.remove("active");
});
slideIcons.forEach((slideIcon) => {
slideIcon.classList.remove("active");
});
slideNumber++;
if (slideNumber > numberOfSlides - 1) {
slideNumber = 0;
}
slides[slideNumber].classList.add("active");
slideIcons[slideNumber].classList.add("active");
}, 4000);
};
repeater();
//stop the image slider autoplay on mouseover
slider.addEventListener("mouseover", () => {
clearInterval(playSlider);
});
//start the image slider autoplay again on mouseout
slider.addEventListener("mouseout", () => {
repeater();
});
</script>
<header id="wrapper">
<nav>
ÖVERDÄCKNINGEN
<div id="menu-toggle">
<input id="menu-checkbox" type="checkbox" />
<span class="hamburger-span"></span>
<span class="hamburger-span"></span>
<span class="hamburger-span"></span>
<ul id="menu">
<li><a class="menu-links" id="active" href="#">Länk 1</a></li>
<li><a class="menu-links" href="html/link2.html">Länk 2</a></li>
<li><a class="menu-links" href="html/link3.html">Länk 3</a></li>
<li><a class="menu-links" href="html/link4.html">Länk 4</a></li>
</ul>
</div>
</nav>
</header>
</div>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Porro amet
dolores nisi delectus ad inventore laboriosam alias, architecto eos
sunt quod eum maiores provident aspernatur debitis ratione minus
quaerat cumque veniam. Delectus commodi odit molestiae excepturi.
Doloremque rem tempora quod est veritatis earum accusamus vel fugiat
numquam reprehenderit ad labore repudiandae recusandae neque
voluptatum a nulla dignissimos sapiente ut, officiis fugit porro.
Aliquid unde incidunt quisquam. Veniam sunt doloremque modi in
voluptates inventore. In corrupti hic eos, incidunt iusto dignissimos
praesentium ut adipisci possimus officiis nesciunt, eveniet asperiores
illum laudantium aliquid dicta temporibus aut maxime dolor fugit totam
animi velit repudiandae ea? Esse, quod repudiandae amet iusto
temporibus aperiam dolore aliquid, magni sunt eveniet deleniti at
suscipit quasi adipisci, quo officia accusantium repellat nostrum
nemo! Reiciendis repellat omnis facilis qui excepturi assumenda id
laborum vero voluptas, vitae aspernatur eius magnam voluptates iusto,
dolores a fugit tenetur quo nostrum ipsa sequi cumque? Velit voluptate
et at aut iste obcaecati nisi, vitae repellat placeat quasi iusto.
Rerum vero eos earum nobis in inventore quos aliquid id, assumenda
tempora? Modi repudiandae dolorem architecto ut animi doloribus,
tempore possimus nihil ipsum accusamus fugiat laboriosam culpa iure
eum mollitia ab? Quidem quas minus necessitatibus voluptate, rem vero
eligendi possimus totam voluptates laudantium atque illo provident
quasi! Laboriosam quo odio quia in! Commodi voluptatem facilis eos
neque non, magni fugiat numquam consectetur, ipsa dolores ut rerum,
sint sequi quos illo obcaecati omnis harum laudantium error inventore
praesentium. Commodi fugit nostrum asperiores vel ducimus
reprehenderit quis suscipit laborum et fugiat libero eius assumenda
esse nobis, iste, impedit ipsa repudiandae aut aspernatur placeat
inventore! Eligendi sit in obcaecati quia repudiandae explicabo
officia eveniet omnis earum. Quibusdam alias amet quas nobis ullam
vitae enim quod facilis quam maiores! Rem consequatur distinctio
laudantium harum labore, minus qui molestiae suscipit modi libero quia
pariatur maxime hic, voluptate totam numquam, illo beatae voluptatibus
atque ipsa assumenda. Ipsum laudantium, modi nam repellat similique
vitae asperiores corrupti accusamus voluptate maxime harum expedita,
ipsa fugiat obcaecati deserunt porro impedit blanditiis error
reprehenderit temporibus ex at, molestias eius magnam? Voluptatem,
repellat ratione distinctio sit odio assumenda non ad fugit eligendi
nemo eveniet enim porro asperiores, aliquam aliquid? Officia
reiciendis libero tempore cupiditate rem laboriosam odit aspernatur!
Perferendis, id odit. Laudantium, culpa. Iure placeat deserunt
consectetur! Cumque, asperiores itaque nobis laudantium fuga fugit,
nemo autem maiores quo et perferendis laborum illum quis suscipit
eveniet ullam modi. Soluta cum eaque, sequi eveniet deserunt quia odio
officia tenetur vero corrupti labore eum voluptatum quam cumque fuga
hic numquam! Ullam, a dolore omnis nemo beatae at possimus eum
officiis molestiae exercitationem temporibus ab blanditiis voluptatum
reiciendis mollitia maxime perspiciatis harum nostrum officia deleniti
delectus. Hic quos molestias rerum ducimus fugiat consectetur facilis
perspiciatis animi, incidunt error ut tenetur similique unde molestiae
labore perferendis sapiente totam mollitia! Qui, similique ad eos,
cumque laudantium sapiente dolores quas omnis nam fuga exercitationem
blanditiis quaerat aut animi at beatae explicabo nihil adipisci.
Voluptatum qui repudiandae provident ratione, tenetur illum fuga quas
sapiente laborum ipsa obcaecati aut quod perspiciatis, ullam, veniam
explicabo nulla quaerat incidunt quidem! Est odit corrupti quibusdam
doloribus! Vel excepturi voluptatum, nobis voluptatem error
consectetur placeat quibusdam architecto! Molestiae ipsum suscipit est
aperiam veniam nobis laboriosam porro alias, sit eos repellendus
labore, ratione unde ducimus, accusantium illum voluptatum. Tempore,
rem repellat, laboriosam velit aut aliquid ipsa quibusdam consectetur
soluta veniam architecto totam! Reiciendis aliquid vel voluptatem
tempore ea, unde officiis. Corrupti perspiciatis voluptates, suscipit
vero voluptatibus corporis commodi excepturi reprehenderit eos et
nulla voluptatem fuga porro maxime quam eius distinctio, at id aliquid
itaque consequuntur ut repudiandae. Officiis, qui eveniet animi
officia voluptate ipsum, fuga, nisi doloribus in obcaecati quia error
eaque ad cupiditate aliquam alias modi placeat mollitia quibusdam
illum quas aliquid sint? Eligendi, nostrum maiores quas iste
recusandae quae natus impedit consequatur id ipsam dignissimos sequi
excepturi, nobis ducimus dolorum tenetur repellendus praesentium in
dicta atque velit voluptatum quia. Placeat culpa aliquid ab natus
quaerat non quasi cupiditate reprehenderit soluta, unde repellendus
illum, praesentium distinctio blanditiis quod magnam laborum excepturi
iusto molestiae accusamus dolorem nihil. Voluptatum, molestias
debitis? Ipsum veritatis iure suscipit, saepe alias aliquam enim,
officiis deleniti soluta ratione tempora maxime ab beatae autem
accusamus nemo sit, porro dolore illum esse hic. Repudiandae expedita
iste, inventore, officia, ut voluptatibus unde ullam sint debitis enim
ipsa dolorum ducimus quos. Suscipit tenetur ratione sed iure! Tempora
maxime, et neque natus dolores aliquam hic magni commodi quod a vel
delectus quisquam aperiam cupiditate eaque aspernatur tempore atque
tenetur modi? Exercitationem repellat hic officiis atque aperiam
repellendus et eos quisquam ratione, quidem enim quae sint facere
laudantium amet iure? Dolorum esse autem corrupti explicabo laborum
eum at distinctio dicta aut molestias sunt, totam ab eligendi
possimus. Ipsa, dolorem numquam sint doloribus reprehenderit
laboriosam aperiam alias, culpa recusandae minima assumenda tempore
placeat sunt nesciunt, dolores quidem repudiandae dolor. Ipsa illo, ex
repellat iusto ea fugit sapiente hic, adipisci nam, soluta numquam
error eum neque quos commodi quae a architecto voluptas perspiciatis?
Velit impedit libero ut sit minima exercitationem delectus nobis
quibusdam ab excepturi, soluta maxime ipsum, maiores laudantium iste?
Atque corrupti cupiditate non deserunt alias deleniti delectus numquam
repellendus soluta obcaecati fugit veniam, porro asperiores nesciunt
sed hic esse iusto ipsam provident est praesentium nam eveniet neque
at. Eaque aliquid ipsam vitae. Impedit quas est asperiores sunt
recusandae, iure beatae accusantium quidem quos iste sint sed
architecto eum perferendis dolorum tempora nihil odio error.
</p>
</div>
<footer id="footer">
<p id="footer-text">Lorem ipsum dolor sit amet consectetur.</p>
</footer>
</main>
</body>
</html>
Yeah, What #evolutionxbox said. If you have the JS included in the header, or any place above where you have defined nextBtn in the HTML or any of the other elements you plan on using, then those elements are going to be null when the script executes.
You should make sure you script runs after the document is loaded by putting the above into a function and running it by attaching said function to the window onload event.
The JS include should look something like the following
function yourScript(){
/* Your script goes here */
}
window.addEventListener("load",yourScript);
You can also use
window.addEventListener("DOMContentLoaded",yourscript);
If you want to run the script after the DOM has been parsed, but before the images and other third party content has been loaded. This is probably fine for your script.
I was recently learning the intersection observer API to make some scroll animations and had a couple of issues.
transform:translate property didn't work properly. I was trying to achieve a left to center slide while i scroll to that div, but that didn't happen. However if I apply transform:scale property or even opacity they work well.
If I bypass a section with 'on scroll opacity transition' too quickly its behavior changes 180. I mean that if by default if I have set the opacity to increase from 0 to 1 on up to down scroll and I do this once and then scroll up fast the animations don't happen and as a result if I again scroll down things start to disappear(opacity:0) instead of appear(opacity:1), opposite to what I have set.
*** I have tried brave and chrome to see if it may work but no positivity****
code>>>>>>>>
Uploaded on codepen>>>link>>> https://codepen.io/Muneebthelearner/pen/PoRRgOE
// CARD ANIMATION ************
const card1 = document.querySelector(".firstcard");
const options = {
root: null,
threshold: 0.25
};
let observer = new IntersectionObserver(callback, options);
function callback(entries, observer) {
entries.forEach((entry) => {
if (!entry.intersectionRatio) {
return;
} else {
entry.target.classList.toggle("after");
}
console.log(entry.target);
});
}
observer.observe(card1);
// IMAGE ANIMATION ***********************
const building = document.querySelector(".building");
let imageobserver = new IntersectionObserver(imagecallback, options);
function imagecallback(entries, imageobserver) {
entries.forEach((entry) => {
if (!entry.intersectionRatio) {
return;
} else {
entry.target.classList.toggle("show");
}
console.log(entry.target);
});
}
imageobserver.observe(building);
#import url("https://fonts.googleapis.com/css2?family=Montserrat:wght#500&family=Roboto:wght#300;400&display=swap");
*,
*::before,
*::after {
padding: 0;
margin: 0;
box-sizing: border-box;
}
nav {
display: flex;
justify-content: space-between;
align-items: center;
background-color: #f5e8c7;
padding: 0.5rem 1rem;
}
.logo {
max-width: 50px;
}
ul {
display: flex;
justify-content: space-between;
list-style: none;
}
ul > * {
padding: 0.5rem 1rem;
}
a {
text-decoration: none;
font-size: 1.1rem;
font-family: "Roboto", sans-serif;
color: black;
font-weight: 300;
}
/* wolf-section */
.main {
background: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.7)),
url("https://i.postimg.cc/d1McYFh7/wolves-2864647-1920.jpg");
color: white;
max-width: 100%;
min-height: 100vh;
background-position: center bottom;
background-size: cover;
display: grid;
place-items: center;
text-align: center;
}
.main-text {
max-width: 600px;
}
.heading {
font-size: 3rem;
font-family: "Montserrat", sans-serif;
line-height: 60px;
}
.about {
font-family: "Roboto", sans-serif;
font-weight: 300;
line-height: 20px;
}
.one {
padding: 0.5rem 1.5rem;
margin: 1rem 0rem;
}
/* Cards section */
.cards {
padding: 2rem 1rem;
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
.firstcard,
.secondcard,
.thirdcard {
width: clamp(200px, 250px, 100%);
background-color: rgba(0, 0, 0, 0.7);
padding: 0rem;
margin:1rem 0rem;
text-align: center;
}
.firstcard {
/*transform: translateX(-150%);
position: relative;
left: -500px; */
/* transform: scale(1.2); */
opacity: 0;
}
.firstcard.after {
/* transform: translateX(0%);
transform: scale(1); */
/* left: 0px; */
opacity: 1;
transition: all 1s ease-in;
}
.title {
text-align: center;
font-size: 1.2rem;
font-family: "Roboto", sans-serif;
font-weight: 400;
padding: 0.5rem 0rem;
color: black;
background-color: #afb4ff;
}
.about_animal {
text-align: justify;
padding: 0.5rem;
font-family: "Roboto", sans-serif;
font-weight: 300;
color: white;
}
.donate {
padding: 0.5rem 1.5rem;
margin: 1rem 0rem 0.5rem;
}
/* TOO text section; */
.too_text {
padding: 1rem;
font-family: "Roboto", sans-serif;
font-weight: 300;
}
.theory {
font-size: 2rem;
font-family: "Montserrat", sans-serif;
text-align: center;
padding: 1rem 0rem;
}
.building {
max-width: 100%;
opacity: 0;
transition: all 1.5s ease;
transform: scale(1.1);
}
.building.show
{
opacity: 1;
transform: scale(1);
}
<header>
<nav>
<div class="logo_div">
<img src="https://i.postimg.cc/BbspTYV7/logo.png" alt="" class="logo">
</div>
<ul>
<li>Home</li>
<li>Contact</li>
<li>About</li>
<li>Login</li>
</ul>
</nav>
</header>
<section class="main">
<div class="main-text">
<h1 class="heading">
WildLife is Looking for your love
</h1>
<p class="about">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi sit beatae quidem, nemo quae excepturi nesciunt alias facilis deserunt nostrum!</p>
<button class="one">Login</button>
<button class="one">Donate</button>
</div>
</section>
<section class="cards">
<div class="firstcard">
<h3 class="title">Animal X</h3>
<p class="about_animal">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Et molestias iure fugiat quisquam vero voluptates qui corporis quod itaque porro?
</p>
<button class="donate">Donate</button>
</div>
<div class="secondcard">
<h3 class="title">Animal Y</h3>
<p class="about_animal">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Et molestias iure fugiat quisquam vero voluptates qui corporis quod itaque porro?
</p>
<button class="donate">Donate</button>
</div>
<div class="thirdcard">
<h3 class="title">Animal Z</h3>
<p class="about_animal">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Et molestias iure fugiat quisquam vero voluptates qui corporis quod itaque porro?
</p>
<button class="donate">Donate</button>
</div>
</section>
<section class="too_text">
<div class="Ltext">
<h3 class="theory">About Us</h3>
<p>
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Temporibus ullam quod dignissimos mollitia voluptates consectetur provident, enim quas sed. Labore aut, sequi commodi ex delectus expedita aperiam consequuntur maiores, eos adipisci quod similique suscipit rem illum quisquam ratione hic facilis dolorum minima pariatur? Sit, commodi quo consequuntur excepturi nesciunt perferendis deleniti nobis accusantium cupiditate, rerum architecto similique asperiores perspiciatis? Quo mollitia fugit esse ducimus est nisi enim exercitationem modi! Libero dolorum illo est, provident accusantium aut harum doloremque numquam, perspiciatis vitae, vel quaerat error dolor eveniet dicta dolores ex nobis suscipit impedit eligendi quis ut delectus! Quam maxime laudantium illo sequi sint soluta recusandae consequatur nam blanditiis eum ipsa tenetur vel iure aliquam, quae optio nemo ab aut! Recusandae consectetur id soluta eum temporibus aliquam neque, doloremque voluptatem consequatur assumenda quae eos facere dolorem necessitatibus nobis repudiandae beatae repellat minima similique voluptates, itaque expedita! Corporis at dolor cum eius tempora excepturi fugit, reiciendis aut debitis saepe eaque! Voluptatum, asperiores unde eveniet officiis officia ullam nam quasi debitis explicabo consequatur adipisci tenetur corrupti itaque possimus sequi dicta dolorum hic voluptate perspiciatis, repellat vel excepturi pariatur recusandae quis! Labore, incidunt, iusto accusantium sint dolore cupiditate voluptatem at consequuntur iure quasi obcaecati neque.</p>
<img src="https://i.postimg.cc/sg53gKhH/image.jpg" alt="" class="building">
</div>
</section>
<section class="dummy_text">
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Ullam totam ab, dolor doloribus facilis possimus quas veniam quidem. A quam quis eligendi dicta eaque dignissimos placeat molestiae nisi eum sint nostrum facere, deserunt provident illum, modi praesentium nulla similique. Voluptates vel nisi, et mollitia delectus obcaecati omnis modi unde? Quaerat maiores labore tempora quod voluptate! Optio, perspiciatis voluptatum dolores non atque corporis consequatur consectetur mollitia sapiente animi, rem rerum. Similique eaque neque quam debitis a autem? Atque illum autem quibusdam excepturi officiis adipisci debitis repellat quos inventore magni minima, obcaecati iure dignissimos, sed placeat
Optio, perspiciatis voluptatum dolores non atque corporis consequatur consectetur mollitia sapiente animi, rem rerum. Similique eaque neque quam debitis a autem? Atque illum autem quibusdam excepturi officiis adipisci debitis repellat quos inventore magni minima, obcaecati iure dignissimos, sed placeat</p>
</section>
Concerned classes:
1.firstcard
2.building
I'm having trouble opening and closing a dynamically created div with Javascript.
The problems are these:
In the opening phase, the div created does not follow the transition scale 1s rule
When I close the div for the first time it respects the transition and closes correctly, while when I open it a second time the div does not close
Here is the HTML, CSS and JS code:
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="stile.css">
</head>
<body>
<div id="check">
<input type="checkbox">
<label>Ho preso visione dell'informativa alla privacy</label>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
#check {
margin-top: 20px;
margin-bottom: 20px;
}
label {
line-height: 1.5;
margin-left: 5px;
}
#informativa-privacy {
z-index: 200;
transform: scale(0);
position:fixed;
/* margin-top: 100px; */
top: 0;
left: 0;
/* background-color: rgba(0, 0, 0, 0.815); */
height: 100%;
overflow-y: scroll;
transition: all .5s;
}
#informativa-privacy section {
background-color: white;
padding: 10px;
width: 90%;
margin: 20px auto;
}
#attenzione {
font-weight: 800;
margin-bottom: 20px;
text-align: center;
}
#informativa-privacy button {
padding: 10px;
background-color: gold;
box-shadow: 0 0 3px black;
outline: none;
border: none;
display: block;
width: 90%;
margin: 10px auto;
font-weight: 800;
}
#overflow {
height: 200px;
overflow-y: scroll;
margin-bottom: 20px;
border: 1px solid blue;
padding: 5px;
}
JS
function apriPrivacy(linkPrivacy) {
linkPrivacy.onclick = function (e) {
e.preventDefault()
let informativa = document.createElement("div")
informativa.id = "informativa-privacy"
document.body.appendChild(informativa)
let contenutoInformativa = `<section>
<p id="attenzione">LEGGI CON ATTENZIONE</p>
<div id="overflow">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Asperiores inventore voluptatum quidem impedit voluptas vitae
ea, amet a autem, quia dicta! Possimus ipsum in earum sapiente
architecto odit ad, veniam natus ullam sint esse amet. Fugiat
mollitia, unde eos maxime consequuntur officia. Natus rem
illum, expedita fugit nam harum, aliquid ducimus ab laboriosam
obcaecati, necessitatibus alias repellat numquam tempora!
Mollitia molestias ipsa dicta non aut, voluptates placeat.
Molestias quibusdam voluptas, tempore deserunt aut cum
veritatis ducimus quidem deleniti sapiente nostrum
perferendis. Molestias ab tenetur provident tempore ut
deserunt nam exercitationem sint labore! Maiores laborum
molestias animi repellat eos ratione. Iste, laborum ducimus!
Autem nisi quos nobis mollitia voluptate consectetur, placeat
minima error earum velit ipsa, numquam blanditiis rerum
quibusdam eligendi magni enim itaque fugit modi. Modi dolore
quae commodi rem quidem maxime eos architecto delectus alias?
Consequatur reprehenderit consequuntur esse tempora architecto
minus animi deleniti debitis neque, ut reiciendis nemo.</p>
</div>
<button>ACCETTA</button>
</section>`
informativa.innerHTML = contenutoInformativa
informativa.style.transition = "transform 1s"
informativa.style.transform = "scale(1)"
let bottoneAccetta = document.querySelector("#informativa-
privacy button")
bottoneAccetta.onclick = function () {
informativa.style.transform = "scale(1)"
informativa.style.transform = "scale(0)"
}
}
}
let linkPrivacy = document.querySelector("#check a")
apriPrivacy(linkPrivacy)
How can I solve the problem?
OK, here is an answer:
Your code didn't work, because everytime someone clicked the link, a new div was created. Then you selected the first "Accetta" button in the document and set it's onclick property. For the first time, when only one button is in the whole document, it works, but for all other times, you select the first button in the document, but it is not the one that is visible
I have reordered your code. Now the element (informativa) is inserted immediately in the body when you call your function apriPrivacy.
Then comes the EventListener on the linkPrivacy element, in which I add the class open to informativa. In the CSS I changed the following:
#informativa-privacy {
z-index: 200;
transform: scale(0);
position: fixed;
/* margin-top: 100px; */
top: 0;
left: 0;
/* background-color: rgba(0, 0, 0, 0.815); */
height: 100%;
overflow-y: scroll;
transition: all .5s, transform 1s;
}
#informativa-privacy.open {
transform: scale(1);
}
Then in the click EventListener on the bottoneAccetta button I remove the class open from informativa and the CSS is responsible for the animation again.
In the following snippet is your complete code working:
function apriPrivacy(linkPrivacy) {
let informativa = document.createElement("div");
informativa.id = "informativa-privacy";
document.body.appendChild(informativa);
let contenutoInformativa = `<section>
<p id="attenzione">LEGGI CON ATTENZIONE</p>
<div id="overflow">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Asperiores inventore voluptatum quidem impedit voluptas vitae
ea, amet a autem, quia dicta! Possimus ipsum in earum sapiente
architecto odit ad, veniam natus ullam sint esse amet. Fugiat
mollitia, unde eos maxime consequuntur officia. Natus rem
illum, expedita fugit nam harum, aliquid ducimus ab laboriosam
obcaecati, necessitatibus alias repellat numquam tempora!
Mollitia molestias ipsa dicta non aut, voluptates placeat.
Molestias quibusdam voluptas, tempore deserunt aut cum
veritatis ducimus quidem deleniti sapiente nostrum
perferendis. Molestias ab tenetur provident tempore ut
deserunt nam exercitationem sint labore! Maiores laborum
molestias animi repellat eos ratione. Iste, laborum ducimus!
Autem nisi quos nobis mollitia voluptate consectetur, placeat
minima error earum velit ipsa, numquam blanditiis rerum
quibusdam eligendi magni enim itaque fugit modi. Modi dolore
quae commodi rem quidem maxime eos architecto delectus alias?
Consequatur reprehenderit consequuntur esse tempora architecto
minus animi deleniti debitis neque, ut reiciendis nemo.</p>
</div>
<button>ACCETTA</button>
</section>`;
informativa.innerHTML = contenutoInformativa;
linkPrivacy.onclick = function(e) {
e.preventDefault();
informativa.classList.add("open");
}
let bottoneAccetta = document.querySelector("#informativa-privacy button");
bottoneAccetta.onclick = function() {
informativa.classList.remove("open");
}
}
let linkPrivacy = document.querySelector("#check a");
apriPrivacy(linkPrivacy);
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#check {
margin-top: 20px;
margin-bottom: 20px;
}
label {
line-height: 1.5;
margin-left: 5px;
}
#informativa-privacy {
z-index: 200;
transform: scale(0);
position: fixed;
/* margin-top: 100px; */
top: 0;
left: 0;
/* background-color: rgba(0, 0, 0, 0.815); */
height: 100%;
overflow-y: scroll;
transition: all .5s, background-color 0s 1s, transform 1s;
}
#informativa-privacy.open {
background-color: red;
transform: scale(1);
}
#informativa-privacy section {
background-color: white;
padding: 10px;
width: 90%;
margin: 20px auto;
}
#attenzione {
font-weight: 800;
margin-bottom: 20px;
text-align: center;
}
#informativa-privacy button {
padding: 10px;
background-color: gold;
box-shadow: 0 0 3px black;
outline: none;
border: none;
display: block;
width: 90%;
margin: 10px auto;
font-weight: 800;
}
#overflow {
height: 200px;
overflow-y: scroll;
margin-bottom: 20px;
border: 1px solid blue;
padding: 5px;
}
<div id="check">
<input type="checkbox">
<label>Ho preso visione dell'informativa alla privacy</label>
</div>
Situation
I have a reverse sticky navbar where the navbar is in position:fixed at the top and when the top of a specific div container touches the bottom of the navbar, the position:static with jquery.
Problem
When the navbar becomes position:static the navbar disappears (please view code in full screen to see problem). I want the navbar to stick to the top of the div container while scrolling to the bottom of the page which means it scroll out of frame when position is not fixed. like this http://jsfiddle.net/vx8uc2rm/.
var fixmeTop = $('.texttop').offset().top;
$(window).scroll(function() {
var currentScroll = $(window).scrollTop()+210;
if (currentScroll >= fixmeTop) {
$('.header-container').css({
position: 'relative'
});
} else {
$('.header-container').css({
position: 'fixed',
top: 0,
left: 0
});
}
});
body {
background-color: #fbfaf8;
overscroll-behavior: none;
overflow: scroll;
}
.myheroimage {
background-color:palevioletred;
min-height: 1400px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
z-index: 0;
}
.header-container {
width: 100%;
position: fixed;
z-index: 99;
background-color: #fbfaf8;
height: 180px;
}
header {
display: flex;
justify-content:space-between;
padding: 30px 120px;
align-items: center;
margin-bottom: 80px;
}
a {
color: white;
text-decoration: none;
}
a.mybutton {
color: #191919;
text-decoration: none;
font-family: 'Roboto', sans-serif;
font-weight: 700;
font-size: 1.5rem;
letter-spacing: 3px;
}
a.mybutton:hover {
color: #758086;
}
a.mybutton1 {
color: #35BDAF;
text-decoration: none;
font-family: 'Roboto', sans-serif;
font-weight: 700;
font-size: 1.5rem;
letter-spacing: 3px;
}
.mybutton{
margin-left: 44px;
}
.texttop {
text-align: center;
font-family: 'Roboto', sans-serif;
letter-spacing: 4px;
font-size: 2rem;
margin: 0 0 68px
}
.container-projects {
display: flex;
flex-wrap: wrap;
align-content: flex-start;
justify-content:flex-start;
margin: 0 120px 120px;
}
.footer-container {
background-color:#1E2224;
color: white;
font-family: 'Roboto', sans-serif;
letter-spacing: 1px;
font-weight: 300;
font-size: 1rem;
height: 150px;
padding: 50px 120px 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
<link rel="stylesheet" href="css-reset.css">
<link rel="stylesheet" href="style.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Racing+Sans+One&family=Roboto:wght#300;400;700&display=swap" rel="stylesheet">
</head>
<body>
<div class="hiddenoverflow"></div>
<div class="myheroimage">
<div class="header-container">
<header>
<div class="logo"></div>
<nav>
Title
Title2
Title3
</nav>
</header>
</div>
</div>
<div class="texttop">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
<div class="container-projects">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia quam tempora quibusdam in atque! Nobis explicabo, at facere, sint sunt voluptas ad nesciunt deleniti dolorem aliquid dolor repellendus hic labore?
Facere ex debitis dolores minus nihil iusto veniam voluptatum nesciunt, maxime nobis esse architecto dignissimos ipsa? Architecto deleniti saepe vel maiores, quia alias maxime, dicta ea aliquid hic quis deserunt.
Earum eum hic nam corporis necessitatibus unde provident, numquam omnis doloremque ad ducimus velit neque, repellat quisquam distinctio. Rem velit veniam reiciendis accusantium nam debitis tempora perspiciatis iste accusamus error!
Suscipit at illo voluptatum eveniet error. Perferendis cupiditate atque rem architecto voluptates excepturi saepe assumenda earum delectus similique voluptatem sint voluptatibus ea, aperiam maiores perspiciatis error doloribus est blanditiis sit!
Beatae recusandae delectus commodi omnis obcaecati quia atque, architecto aliquid sapiente vitae vero quasi veniam ipsam totam earum doloremque assumenda? Ut, ad. Tempora, necessitatibus voluptates qui sit natus eum assumenda.
Eius molestias totam non optio? Totam laudantium odio tempore consequuntur laboriosam tempora, voluptas eligendi quae officiis ad necessitatibus optio quas excepturi voluptatibus doloremque provident numquam molestiae dolorum, exercitationem aliquid reprehenderit?
Alias laboriosam, aperiam quas provident quibusdam in reprehenderit voluptatem officiis laborum. Deleniti, beatae molestiae. Minima cupiditate consequuntur, repudiandae quasi aliquam, autem, delectus suscipit ex similique vel doloremque cumque odio rem?
Praesentium vel, voluptatem nam soluta reiciendis maiores quia hic pariatur eaque incidunt perspiciatis voluptatum debitis alias unde iure, accusamus dolorem dolor consectetur eos? Unde autem suscipit nihil corporis, sequi quis!
Tempore suscipit iusto libero, illo doloremque cumque odio reprehenderit cupiditate quis, dignissimos porro officiis est error a enim optio accusantium? Maxime laborum totam ut quod iure iste. Aspernatur, minus porro!
Natus inventore dolore, eveniet laudantium magni iste impedit? Voluptatibus inventore eius, illum voluptatem magnam non reprehenderit perspiciatis vitae qui, est error dolorem voluptas incidunt quia ipsa exercitationem expedita tempore autem?</p>
</div>
<div class="footer-container">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="stickyscroll.js"></script>
</body>
</html>
Please check this fiddle:http://jsfiddle.net/dularnikode/742azj0x/5/ if it can solve your first problem.
You can use position "sticky" to solve your first problem.
body {
height: 3000px;
}
.content {
background: blue;
height: 500px;
}
.fixme {
background: green;
color: white;
text-align: center;
width: 100%;
position:sticky;
top:0px;
}
.writehere {
background: red;
width: 100%;
}
<div class="fixme">Scroll here</div>
<div class="content"></div>
<div class="writehere">hi</div>
Figured it out! I just used scrollToFixed(); jquery plugin in my js file instead by bigsotteddog https://github.com/bigspotteddog/ScrollToFixed#demos
$(document).ready(function() {
$('.header-container').scrollToFixed( { marginTop: 10, limit: 400 } );
The marginTop fixes the navbar/header to whichever height set, 0 is the top of the page and the limit is the scroll position/point in which it triggers the navbar to stop sticking.
This specific demo provided by the github page helped me understand how to use the plugin, http://jsfiddle.net/y3qV5/435/.
here's a link to a similar question I had that was answered on stackoverflow too, "Reverse" sticky header.
here's my codepen:
https://codepen.io/squishyboots19996/pen/BaoQjPL
I'm trying to create a navigation menu that slides in when you hover on the arrow and then slides away when your mouse leaves the menu.
I have UIController.showMenu and UIController.closeMenu.
If I call either one of them in the console, they work as expected.
However the event listener for the closeMenu is not working. It just doesn't detect the mouse leaving the menu and doesn't close.
If i add a console.log("Hello") to the function, it will trigger when the page first loads. But won't work when I need it to.
app.js:
var Controller = (function() {
UIController.updateNav;
setInterval(UIController.moveArrowDown, 3000);
//UIController.addBodyMargin();
document.querySelector('#menuArrow').addEventListener("mouseover", UIController.openMenu);
document.querySelector('#sideMenu').addEventListener("onmouseleave", UIController.closeMenu);
});
//-----------------------------------------------------------
var UIController = (function() {
return {
openMenu: (function(){
document.getElementById('sideMenu').style.marginLeft = '0';
document.getElementById('menuArrow').style.marginLeft = '250px';
document.body.style.marginLeft = '25px'
}),
closeMenu: (function(){
document.getElementById('sideMenu').style.marginLeft = '-250px';
document.getElementById('menuArrow').style.marginLeft = '0';
document.body.style.marginLeft = '0';
})
}
})();
document.addEventListener('DOMContentLoaded', (event) => {
Controller();
})
navbar.css:
.sidemenu {
height: 100vh;
width: 250px;
position: fixed;
top: 0;
left: 0;
background-color: red;
z-index: 5001;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-left: -250px;
transition: .5s;
cursor: pointer;
}
.sidemenu ul {
list-style: none;
padding: 0;
}
.sidemenu a {
text-decoration: none;
font-size: 2rem;
}
.expand-arrow {
z-index: 4999;
position: fixed;
transition: .5s;
bottom: 50%;
}
HTML:
<body>
<div class="sidemenu" id="sideMenu">
<ul>
<li><a>Home</a></li>
<li><a>About</a></li>
<li><a>Questionnaire</a></li>
</ul>
</div>
<i class="expand-arrow" id="menuArrow">SHOW MENU BUTTON ></i>
<section class="child about-section" id="aboutSection">
<div class="about-section__content">
<div class="about-section__content__info-box">
<h1 class="about-section__content__info-box__header">About Us</h1>
<p class="about-section__content__info-box__text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi, soluta magni exercitationem consectetur natus assumenda dignissimos quasi porro non doloremque labore odit quae accusantium excepturi, dicta recusandae possimus. Necessitatibus, ipsam.
Esse dolorem porro quo velit odit quam, rerum, non optio, labore aliquam ducimus totam molestias ipsa quasi explicabo voluptates. Quas eius dolor harum optio corrupti quo impedit dolorem sunt porro?
Eaque officia vero, facere possimus exercitationem sint magnam aliquam veniam, ipsam soluta eveniet ex iusto perferendis blanditiis a repellendus assumenda dolores natus dolorum. Cumque vero deserunt fuga autem reprehenderit dignissimos.
</p>
</div>
</div>
</section>
<script src="app.js"></script>
</body>
When using the addEventListener() method, it's "mouseleave" not "onmouseleave"
you shouldn't use "on" while adding the event, just use the name of
the function
document.querySelector('#sideMenu').addEventListener("mouseleave", UIController.closeMenu);
instead of
document.querySelector('#sideMenu').addEventListener("onmouseleave", UIController.closeMenu);