Changing css transform in javascript doesn't trigger transition in Javascript - javascript

I want to program a responsive navigation bar, so when the user clicks on the burger menu, the links of the navigation should move from right to left inside the website.
In Javascript, I toggle the class "nav-active" of "nav ul". The "nav-active" class has the attribute "transform: translateX(0%);", which should position the element back to the website, but it doesn't.
HTML:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="stylesheets/style.css">
</head>
<body>
<!------------------------------------------------------------------------Top Navigation-->
<nav class="navigation">
<div class="logo">
<img src="images/logodressler350.png" alt="Logo Image" width="250">
</div>
<ul class="nav-links">
<li>Startseite</li>
<li>lernraumdressler</li>
<li>Nachhilfe</li>
<li>Anfahrt</li>
<li>Kontakt</li>
</ul>
<div class="burger">
<div class="line-1"></div>
<div class="line-2"></div>
<div class="line-3"></div>
</div>
</nav>
<div class="green-line"></div>
<script type="text/javascript" src="javascripts/navbar.js"></script>
</body>
</html>
SCSS:
.navigation{
display: flex;
min-height: 8vh;
justify-content: space-around;
align-items: center;
background-color: white;
ul {
display: flex;
justify-content: space-around;
align-items: center;
width: 70%;
}
ul li{
list-style: none;
text-align: center;
}
ul li a{
text-decoration: none;
color: #333;
letter-spacing: 2px;
font-weight: bold;
font-size: 18px;
}
}
.dropdowncontent{
display: none;
}
.burger{
display: none;
cursor: pointer;
div{
width: 25px;
height: 3px;
background-color: #333;
margin: 5px;
transition: all 0.3s ease;
}
}
.green-line{
background-color: lightgreen;
width: 100%;
height: 2px;
}
//------------------------------------------------------------------------------MEDIA
#media screen and (max-width: 968px){
.navigation{
ul{
width: 80%;
}
}
}
#media screen and (max-width: 868px){
body{
//overflow-x: hidden;
}
.burger{
display: block;
}
.navigation ul{
position: absolute;
right: 0;
height: 92vh;
top: 8vh;
background-color: gray;
display: flex;
flex-direction: column; // x und y werden getauscht
align-items: center;
width: 50%;
transform: translateX(100%);
transition: transform 0.5s ease-in;
li{
opacity: 0;
}
}
}
//------------------------------------------------------------------------------Keyframes
/*wenn man clickt*/
.nav-active{
transform: translateX(0%);
}
#keyframes navLinkFade{
from{
opacity: 0;
transform: translateX(50px);
}
to{
opacity: 1;
transform: translateX(0px);
}
}
.toggle .line-1{
transform: rotate(-45deg) translate(-5px, 6px);
}
.toggle .line-2{
opacity: 0;
}
.toggle .line-3{
transform: rotate(45deg) translate(-5px, -6px);
}
JS:
const navSlide = () => {
const burger = document.querySelector(".burger");
var nav = document.querySelector(".navigation ul");
const navLinks = document.querySelectorAll(".navigation ul li");
burger.addEventListener("click", () => {
// Toggle Nav
nav.classList.toggle("nav-active");
// Animate Links
navLinks.forEach((link, index) => {
if (link.style.animation){
link.style.animation = "";
} else{
link.style.animation = `navLinkFade 0.5s ease forwards ${index / 7}s`;
}
});
// Burger Animation
burger.classList.toggle("toggle");
});
}
navSlide();
Here is the Demo
https://jsfiddle.net/chinamarc/6r78a0e2/4/

https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
Specificity is a weight that is applied to a given CSS declaration, determined by the number of each selector type in the matching selector. When multiple declarations have equal specificity, the last declaration found in the CSS is applied to the element. Specificity only applies when the same element is targeted by multiple declarations. As per CSS rules, directly targeted elements will always take precedence over rules which an element inherits from its ancestor.
//-----------------------------------------------Keyframes
/*wenn man clickt*/
.navigation .nav-active{
transform: translateX(0%);
}

Related

How do I animate my Navigation Button to perform the proper animation assigned to it using JavaScript?

So here's what I have and I can figure out why my burger div (navigation button on mobile) won't animate to shape a X can anyone explain what I'm missing or doing wrong?
Ive tried changing the class name using toggle through javaScript but it doesn't animate the lines 45 degrees and Im unsure why that is?
I would like to know what I can do in terms of changing the burger (nav button) to form a X and hide the center line by fading it out using a javaScript
This also my first time using stack overflow in terms of asking a question so please go easy on me.
const navSlide = () => {
const burger = document.querySelector('.burger');
const nav = document.querySelector('.nav-items');
const navItems = document.querySelectorAll('.nav-items li');
burger.addEventListener('click', () => {
//NAV Toggle
nav.classList.toggle('nav-active');
//Items Animated
navItems.forEach((item, index) => {
if (item.style.animation) {
item.style.animation = '';
} else {
item.style.animation = `navItemFade 0.5s ease forwards ${index / 7 + .3}s`;
}
});
//Burger Animation
burger.classList.toggle('tip');
});
}
navSlide();
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
scroll-behavior: smooth;
}
a {
text-decoration: none;
color: rgb(245, 245, 245);
transition: .3s;
}
/* NAV */
nav {
display: flex;
justify-content: space-between;
align-items: center;
min-height: 11vh;
font-family: 'Poppins', sans-serif;
background-color: rgb(255, 145, 0);
}
.logo {
margin-left: 3rem;
}
.logo>a>h1 {
color: rgb(245, 245, 245);
letter-spacing: 3px;
text-transform: uppercase;
font-size: 32px;
}
.nav-items {
display: flex;
align-items: center;
list-style-type: none;
margin-right: 25rem;
}
.nav-items>li {
padding: 1.5rem;
}
.nav-items>li>a {
font-size: 16px;
}
.nav-items>li>a:hover {
color: rgb(0, 0, 0);
}
.nav-icons {
display: flex;
margin-right: 2rem;
}
.nav-fas {
margin: 1rem;
cursor: pointer;
}
.burger {
display: none;
margin-left: 1rem;
}
.burger>div {
width: 25px;
height: 3px;
background-color: rgb(0, 0, 0);
margin: 5px;
transition: all 0.3s ease;
}
/* NAV MOBILE */
#media screen and (max-width:1240px) {
.nav-items {
margin-right: 10px;
transition: .3s;
}
}
#media screen and (max-width:860px) {
body {
overflow-x: hidden;
}
.nav-items {
width: 100%;
max-width: 300px;
display: flex;
flex-direction: column;
justify-content: space-evenly;
position: absolute;
height: 90vh;
top: 14vh;
background-color: rgb(255, 145, 0);
transform: translateX(-100%);
transition: transform 0.5s ease-in;
}
.nav-items li {
opacity: 0;
}
.burger {
display: block;
cursor: pointer;
}
#fas1,
#fas2 {
display: none;
}
}
.nav-active {
transform: translateX(0%);
}
#keyframes navItemFade {
from {
opacity: 0;
transform: translateX(50px);
}
to {
opacity: 1;
transform: translateX(0px);
}
}
.tip .line1 {
transform: rotate(-45deg) translate(-5px, 6px);
}
.tip .line2 {
opacity: 0;
}
.tip .line3 {
transform: rotate(45deg) translate(-5px, -6px);
}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="main.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<title>E-Store</title>
</head>
<body>
<header>
<nav>
<div class="burger">
<div clsss="line1"></div>
<div clsss="line2"></div>
<div clsss="line3"></div>
</div>
<div class="logo">
<a href="index.html">
<h1>logo</h1>
</a>
</div>
<ul class="nav-items">
<li>Home</li>
<li>Collection</li>
<li>About</li>
<li>Contact us</li>
</ul>
<div class="nav-icons">
<div class="nav-fas" id="fas1">
<i class="fas fa-map-marker-alt"></i>
</div>
<div class="nav-fas" id="fas2">
<i class="fas fa-envelope"></i>
</div>
<div class="nav-fas" id="fas3">
<i class="fas fa-search"></i>
</div>
<div class="nav-fas" id="fas4">
<i class="fas fa-shopping-cart"></i>
</div>
</div>
</nav>
</header>
<script src="./app.js" type="module"></script>
</body>
</html>
Everything is right. You just have one simple spelling mistake, you wrote clsss instead of class on three divs with class line1, line2, line3.
PS:- It looks like you are new to web development. A piece of advice for you, make use of text editor(whatever you might be using) to detect small spelling mistakes and other linting errors. It will save you a lot of time.

Navigation bar isn't being responsive in the phone view

I am a beginner in web design and development and I am trying to make the navigation bar be more responsive when it is viewed on phones and tablets but for some reason it appears really buggy on the phone, I have used a meta tag so that the browser renders it correctly but it doesn't, it comes out all buggy like the picture below:
Click on the link to see picture -> As you can see it's coming out half and half
I have enabled overflow-x:hidden but some how am still able to browse towards the right and see the nav bar which isn't meant to be visible unless clicked, I don't understand why that's happening.
Click on the link to see picture -> This is how it is when you load it.
I have also tried to put the screen resolution as follows:
`#media screen and (max-width:1024px){
.nav-links
{
width:48%;
}
}
#media screen and (max-width:768px)`
This is also running on my firebase server and is available to view through this link:https://test-response-5f60c.web.app/PAGES/quotes.html
I am sorry for any code inconsistencies and mistakes, please help me out, I don't understand what I am doing wrong in the CSS. This is the tutorial I followed: Click on the link -> Tutorial
Following is my code:
function navSlide() {
const burger = document.querySelector(".burger");
const nav = document.querySelector(".nav-links");
const navLinks = document.querySelectorAll(".nav-links li");
burger.addEventListener("click", () => {
//Toggle Nav
nav.classList.toggle("nav-active");
//Animate Links
navLinks.forEach((link, index) => {
if (link.style.animation) {
link.style.animation = ""
} else {
link.style.animation = `navLinkFade 0.5s ease forwards ${index / 7 + 0.5}s`;
}
});
//Burger Animation
burger.classList.toggle("toggle");
});
}
navSlide();
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
margin: 0;
}
nav{
display: flex;
justify-content: space-around;
align-items: center;
min-height: 8vh;
background-color: #000000;
color: #f9f9f9;
font-family: 'Open Sans', sans-serif;
font-size: large;
}
.logo{
text-transform: uppercase;
letter-spacing: 3px;
font-size: 20px;
}
.nav-links {
list-style-type: none;
padding: 1.2%;
margin:0;
overflow: hidden;
background-color: #000000;
display: flex;
align-items: center;
justify-content: center;
}
.nav-links li a {
font-size: 18px;
font-weight: bold;
display: inline-block;
color: white;
text-align: center;
padding: 26px 26px;
text-decoration: none;
transition: 0.3s;
}
li a:hover {
background-color: #3498DB ;
}
.burger{
display: none;
cursor: pointer;
}
.burger div{
width: 25px;
height: 4px;
background-color: #f9f9f9;
margin: 5px;
}
.head {
font-family: sans-serif;
font-weight: bold;
margin: auto;
width: 60%;
border: 3px solid #3498DB ;
padding: 40px;
text-align: center;
opacity: 5.9;
animation-duration: 3s;
animation-name: fadein;
}
#media screen and (max-width:1024px){
.nav-links{
width:48%;
}
}
#media screen and (max-width:768px){
body {
overflow-x: hidden !important;
}
.nav-links{
position: absolute;
right: 0;
height: 92vh;
top: 8vh;
background-color: #000000;
display: flex;
flex-direction: column;
align-items:center;
width: 50%;
transform: translate(100%);
transition: transform 0.5s ease-in;
}
.nav-links li{
opacity: 0;
}
.burger{
display: block;
}
.nav-active {
transform: translate(0%);
}
}
#keyframes navLinkFade {
from{
opacity: 0;
transform: translateX(50px);
}
to{
opacity: 1;
transform: translateX(0px);
}
}
.toggle .line1 {
transform: rotate(-45deg) translate(-5px, 6px);
}
.toggle .line2 {
opacity: 0;
}
.toggle .line3 {
transform: rotate(45deg) translate(-5px, -6px);
}
#googleForm {
margin-left: 20px;
text-align: center;
margin-top: 20px;
}
<!doctype html>
<html>
<head>
<style>
body {
background-image: url('../IMG/land2.jpg');
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: auto ;
}
</style>
<title> Quotes </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="../CSS/style.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet">
</head>
<body>
<nav>
<div class="logo">
<h4> Edge Concreting and Landscaping </h4>
</div>
<ul class="nav-links">
<li>Home</li>
<li>Quotes</li>
<li>Contact</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
<div id="googleForm">
<iframe src="https://docs.google.com/forms/d/e/1FAIpQLSd5uy_5vc6ozsY1kcGRliC8hYH9w_WqEU1acN0tJQ6rrqEJmg/viewform?embedded=true" width="640" height="1427" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>
</div>
<script src="../JS/app.js"></script>
</body>
</html>
Set your media queries to a lower max width and position absolute.
For example.
#media (max-width: 768px){.logo h4 {position: absolute;
right: 50px;}}
That should enable you to select your logo and adjust it for mobile device.
#media (max-width: 768px){.nav-links{position: absolute;
right: 50px;}}
Should enable you to select and adjust your dropdown. You can change or substitute rightor left or top or bottom or width within the same media query.
Try reducing the width of nav-links.
This worked for me:
html, body {
overflow-x: hidden;
}
body {
position: relative;
}

'classList' property returning null when element is present?

I'm fairly new to JS so I'm sorry if this has been answered.
I've siphoned through most answers and they didn't specifically fall in line with mine.
I'm not understanding why the element cant is called upon when it's there.
I don't think it seems to be anything with the CSS but I'm not completely sure.
const navSlide = () => {
const burger = document.querySelector('.burger');
const nav = document.querySelector('nav-links');
burger.addEventListener('click', () => {
nav.classList.toggle('nav-active');
});
}
navSlide();
display: flex;
justify-content:space-around;
align-items: center;
min-height: 8vh;
background-color: var(--mainBg);
}
.nav-links li{
list-style: none;
}
.nav-links{
display: flex;
justify-content: space-evenly;
width: 20%;
font-size: 20px;
}
.nav-links a{
letter-spacing: 2px;
color:var(--mainText);
}
.burger{
display: none;
cursor: pointer;
}
.burger div{
width: 25px;
height: 4px;
background-color: var(--mainText);
margin:3px;
padding: 0;
}
.showacase-hero{
background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),url("img/D1.jpg");
height: 600px;
background-position:center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
}
.hero-container{
text-align: center;
position: absolute;
top: 50%;
left: 28%;
color: var(--mainBanner);
font-size: 20px;
}
/* Media Queries*/
#media screen and (max-width:1024px){
.nav-links{
width: 60%;
}
}
#media screen and (max-width:728px){
body{
overflow-x: hidden;
}
.nav-links{
position: fixed;
right: 0px;
height: 92vh;
top: 8vh;
background-color:var(--mainBg);
display: flex;
flex-direction: column;
align-items: center;
width: 50%;
transform: translateX(100%);
transition: transform 0.5s ease-in;
}
.nav-links li{
opacity: 0;
}
.burger{
display: block;
margin-left: 28em;
}
.nav-active{
transform: translateX(0%);
}
}
#keyframes navLinkFade{
from{
opacity: 0;
transform: translateX(50px);
}
to{
opacity: 1;
transform: translateX(0px);
}
}
#media screen and(max-width:566px){
}
<body>
<nav>
<ul class="nav-links">
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Contact</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
<div class="showacase-hero">
<div class="hero-container">
<h1>Yade's Pet</h1>
<span class="banner"><p>Petcare Specialist/Dog Walker</p></span>
<form action="https://www.w3docs.com/">
<button class="btn" type="submit">Let's Take a Stroll</button>
</form>
</div>
</div>
<script type="text/javascript" src="app2.js"></script>
</body>
</html>
nav-links is a class, not a tag, so add a dot before the class name:
document.querySelector( '.nav-links' );
PS: It's a bad practice to use variables const navSlide = () => { instead of a clear function declarations:
function navSlide () {
...
}

Button on click doesn't working -> transform

I have a problem with my (burger) button.
When I click It's adding a class to my header-links, but class is not working -> I need to change on click from transform: translateX(100%) to transform: translateX(0);
I don't know why it's not working. Please help me, I tried a lot of ways to fix it but still, it's not working.
My code so far:
<header>
<div class="header-logo">
<p>ecodex</p>
</div>
<ul class="header-links">
<li>O NAS</li>
<li>OFERTA</li>
<li>KONTAKT</li>
</ul>
<div class="menu">
<div class="line-one"></div>
<div class="line-two"></div>
<div class="line-three"></div>
</div>
</header>
header {
display: flex;
justify-content: space-around;
align-items: center;
min-height: 8vh;
p {
font-size: 14px;
letter-spacing: 5px;
text-transform: uppercase;
}
.header-active {
transform: translateX(0);
}
.header-links {
display: flex;
justify-content: space-around;
width: 30%;
list-style-type: none;
li {
font-size: 14px;
letter-spacing: 3px;
#media only screen and (max-width: 768px) {
opacity: 0;
}
}
#media only screen and (max-width: 1024px) {
width: 60%;
}
#media only screen and (max-width: 768px) {
position: absolute;
right: 0;
height: 90vh;
top: 8vh;
background: black;
display: flex;
flex-direction: column;
align-items: center;
color: white;
width: 50%;
transform: translateX(100%);
transition: transform 0.5s ease-in;
.header-active {
transform: translateX(0);
}
}
}
.menu {
cursor: pointer;
display: none;
#media only screen and (max-width: 768px) {
display: block;
}
div {
width: 25px;
height: 2px;
background-color: black;
margin: 5px;
}
}
}
const headerSlide = () => {
const menu = document.querySelector('.menu');
const headerLinks = document.querySelector('.header-links');
menu.addEventListener('click', () => {
headerLinks.classList.toggle('header-active');
});
}
headerSlide();
In the developer tools when I click the button the class 'header-active' is adding and removing - it's good, but don't know why my transform: translate(0) is not working.
This should do the trick:
.header-active {
transform: translateX(0) !important;
}
The problem is that .header-links has translateX(100%), so translateX is ignored. I would move transform: translateX (100%) to another class and remove it before adding .header-links.
CSS
.tX100 {
transform: translateX(100%);
}
HTML
<ul class="header-links tX100">
JS
headerLinks.classList.toggle('tx100');
headerLinks.classList.toggle('header-active');

web page looks different when changing the responsive look on developer tools

I have a web page I started and currently just trying to get the nav bar to be responsive, I've got all the media query tags in but when I'm looking at how it looks in different screen sizes on developer tools, sometimes it will look exactly how I want it to and other times it will look wrong.
I haven't changed anything in the code but when I'm going through the different screen options it will just switch between and I'm not sure why so I don't know whether the code is wrong or if it's just developer tools adjusting?
Any help is greatly appreciated.
const navSlide = () => {
const burger = document.querySelector('.burger');
const nav = document.querySelector('.nav-links');
const NavLinks = document.querySelectorAll('.nav-links li');
burger.addEventListener('click', () => {
//toggle Nav
nav.classList.toggle('nav-active');
//Animate Links
NavLinks.forEach(link, index); {
if (link.style.animation) {
link.style.animation = '';
} else {
link.style.animation = `NavLinkFade 0.5s ease forwards ${index / 7 + 3}s`;
}
};
//burger animation
burger.classList.toggle('toggle');
}, )
}
navSlide();
body {
margin: 0;
padding: 0;
height: 100%;
}
/*navigation top bar*/
.nav {
background-color: #2b2929;
display: flex;
justify-content: space-around;
align-items: center;
min-height: 8vh;
font-family: Arial, Helvetica, sans-serif;
}
.logo {
color: white;
text-transform: uppercase;
letter-spacing: 5px;
font-size: 22px;
}
.nav-links {
display: flex;
justify-content: space-around;
width: 30pc;
}
.nav-links li {
list-style: none;
}
.nav-links :hover {
color: rgb(0, 204, 255);
}
.nav-links a {
color: white;
text-decoration: none;
letter-spacing: 3px;
font-weight: bold;
font-size: 14px;
text-transform: uppercase;
}
.burger {
display: none;
cursor: pointer;
}
.burger div {
width: 25px;
height: 3px;
background-color: white;
margin: 5px;
transition: all 0.3s ease;
}
#media screen and (max-width: 1024px) {
.nav-links {
width: 60%;
}
}
#media screen and (max-width:768px) {
body {
overflow-x: hidden;
}
.nav-links {
position: absolute;
right: 0px;
height: 92vh;
top: 6vh;
background-color: #2b2929;
display: flex;
flex-direction: column;
align-items: center;
width: 50%;
transform: translateX(100%);
transition: transform 0.5s ease-in;
}
.nav-links li {
opacity: 100%;
}
.burger {
display: block;
}
.footer {
padding-bottom: 100px;
}
}
#media screen and (min-width:768px) {
body {
overflow-x: hidden;
}
}
.nav-active {
transform: translateX(0%);
}
#keyframes navlinkfade {
from {
opacity: 0;
transform: translateX(50px)
}
to {
opacity: 1;
transform: translateX(0px);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sue Allerton</title>
<link rel="stylesheet" href="styles.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<nav class="nav">
<h1 class="logo">Sue Allerton</h1>
<ul class="nav-links">
<li>Home</li>
<li>About Me</li>
<li>Collections</li>
<li>Contact</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
<section class="container">
<div class="main">
<p>let your imagination run wild</p>
</div>
</section>
<footer class="footer">
<div class="social">
<ul>
<li>Facebook</li>
<li>Instagram</li>
<li>Goodreads</li>
</ul>
</div>
</footer>
<script src="app.js"></script>
</body>
</html>
I'm looking at how it looks in different screen sizes on developer tools, sometimes it will look exactly how I want it to and other times it will look wrong.
You should mention on which screen size it looks different and what you mean by how exactly you want it to look. Also it seems like link is not defined because you are using forEach on a collection and that is an Array method. You can convert the collection to an array first
const NavLinksArray = Array.prototype.slice.call(NavLinks)

Categories

Resources