EJS selecting only a sepcific category - javascript

I have this piece of code.
HTML:
<header<% if ( current.source === 'features' || current.path[0] === 'index' || current.source !== 'customers' ) { %> class="header-white"<% } %>>
<div class="row">
<div class="small-3 columns">
<div class="logo">
<a href="/">
<svg class="handsontable-logo">
<use xlink:href="#handsontable-logo"></use>
</svg>
</a>
<a href="//github.com/handsontable/handsontable" id="github-star" class="star-counter" target="_blank">
<svg>
<use xlink:href="#github-squid"></use>
</svg>
<div class="github-star">
<div class="triangle"></div>
<div data-bind="starsCount">-</div>
</div>
</a>
</div>
</div>
<div class="small-9 columns text-right">
<nav class="mobile-inactive">
<a href="#" id="mobile-nav-menu">
<svg>
<use xlink:href="#open-mobile-nav"></use>
</svg>
</a>
<ul>
<li class="mobile-only">Home</li>
<li>Features</li>
<li><a
href="/examples.html?manual-resize&manual-move&conditional-formatting&context-menu&filters&dropdown-menu&headers">
Examples
</a></li>
<li>Download</li>
<li>Pricing</li>
<li>Case studies</li>
<li>Developers</li>
<li class="mobile-only">Resellers</li>
<li class="mobile-only">Blog</li>
<li class="mobile-only">Contact</li>
<li class="news">
<svg>
<use xlink:href="#bell"></use>
</svg>
</li>
<li><a href="//my.handsontable.com/sign-in.html"
class="btn size-small bg-green-light bg-lighten text-white">
Sign in
</a>
</li>
</ul>
</nav>
</div>
</div>
SCSS:
header {
#include absolute-top-left (0, 0);
width: 100%;
padding: 34px 0 0;
z-index: 1;
.logo {
#include relative-top-left (-3px, 0);
#media only screen and (min-width: $largeWidth) {
#include relative-top-left (10px, 0);
}
svg {
&.handsontable-logo {
#include rectangle (130px, 25px);
fill: $baseGray;
}
}
}
/* Begin: This allows to stretch the mobile menu to 100% of width of the viewport */
& > .row > .columns:last-child {
position: static;
}
/* End */
nav {
& > a {
#include absolute-top-right (4px, 5px);
padding: 20px;
display: block;
z-index: 11;
#media only screen and (min-width: $largeWidth) {
display: none;
}
svg {
#include square (28px);
fill: $baseGray;
}
}
ul {
display: none;
#media only screen and (min-width: $largeWidth) {
display: block;
}
li {
padding: 0 19px;
display: inline-block;
&:last-child {
padding-right: 0;
}
&.mobile-only {
display: none;
}
&.news {
padding-right: 32px;
position: relative;
svg {
#include square (18px);
#include relative-top-left (4px, 0);
fill: $baseGray;
}
#HW_badge_cont {
#include absolute-top-left (0, 13px);
#HW_badge {
#include square (12px);
#include relative-top-left (0, 0);
line-height: 13px;
background-color: $brandRedActive;
&.HW_softHidden {
opacity: .4;
background-color: $brandVibrantGreenActive;
}
}
}
}
a, a:hover, a:active, a:visited {
font-size: 13px;
color: $baseGray;
}
a:hover {
color: $darkGray;
}
}
}
}
/* Menu visible only on mobile devices */
nav.mobile-active {
#media only screen and (min-width: $largeWidth) {
display: none;
}
& > a {
svg {
fill: $darkGray;
}
}
ul {
#include absolute-top-left (12px, 2%);
width: 96%;
padding: 66px 0 8px;
display: block;
text-align: center;
border-radius: 4px;
z-index: 10;
box-shadow: 0 3px 18px rgba(0, 0, 0, 0.15), 0 3px 18px rgba(0, 0, 0, 0.15);
background: #fff;
li {
width: 49%;
padding: 10px 0;
&.mobile-only {
display: inline-block;
}
a, a:hover, a:active, a:visited {
font-size: 18px;
color: $brandBlue;
&.btn {
width: 100%;
color: #fff;
font-size: 18px;
}
}
&:last-child {
width: 90%;
padding-top: 40px;
}
&.news {
display: none;
}
}
}
}
&.header-white {
.logo {
.github-star {
border: 1px solid $darkWhite;
color: $darkWhite;
font-weight: 600;
.triangle {
border-right-color: $darkWhite;
}
}
svg {
fill: $darkWhite;
}
}
a, a:hover, a:active, a:visited {
color: #fff;
}
a:hover:not(.btn) {
color: $brandFeatherBlue;
}
svg, .news svg {
fill: #fff;
}
}
}
My problem is that I want to change navigation color to customers page only.
The problem is that all subpages within the customer's category are also having recolored navigation style.
So:
customers === white
| subfolder === white
| subfolder === white
| subfolder === white
And I want to keep the subfolders in the customer's directory with the original color of navigation.
Any help please, I'm not a JS dev at the moment I'm trying to understand how does it work?

Cutting it down to the relevant bits you've got this is your SCSS:
header {
&.header-white {
a, a:hover, a:active, a:visited {
color: #fff;
}
}
}
That's going to set all your links to white.
Assuming this is your 'customers' section:
<li>Case studies</li>
you can add a class to single it out:
<li><a class="customers" href="/customers">Case studies</a></li>
and then update the CSS accordingly:
header {
&.header-white {
a.customers {
&, &:hover, &:active, &:visited {
color: #fff;
}
}
}
}

Ok problem solved:
I was approaching it in a wrog way, instead trying to link the exact path like here:
<header<% if ( current.source === 'features' || current.path[0] === 'index' || current.source !== 'customers' ) { %> class="header-white"<% } %>>
All I had to do was identify the exact length of the link like here:
<header<% if ( current.source === 'features' || current.path[0] === 'index' || current.path[1] === 'index' ) { %> class="header-white"<% } %>>
It worked for me the only problem here was identifying subpages name and link length.

Related

Is there a way to hide absolute div with JS?

I have this .endGame div which I wanna hide and then show again when I need it. So I created .endGameDisplay to hide it but it doesn't work that way and I don't know why.
It only hides when display: none; is in .endGame
In java script I want it to be hidden on page opening so i have sth like this:
const endGame = document.querySelector('.endGame');
endGameOff();
//end game on func
function endGameOn() {
endGame.classList.remove('.endGameDisplay');
}
//end game off func
function endGameOff() {
endGame.classList.add('.endGameDisplay');
}
I also tried to change classlist.add to .remove to check if I wasn't mistaken but still nothing.
My HTML:
<div class="endGame">
<h1>You</h1>
<h2 class="wonORlost">text</h2>
<h3>Do you want to restar game or go back to the menu?</h3>
<div class="btns">
<div class="restart">Restart</div>
<div class="exit">Exit</div>
</div>
</div>
My CSS:
.endGame {
position: absolute;
z-index: 1;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
background: $primary-color;
color: $white-color;
border-radius: 25px;
text-align: center;
box-shadow: 0 0 15px #333;
.h1 {
font-weight: $font-bold;
}
.h2 {
font-weight: $font-bold;
color: lighten($bot-color, 15%);
}
.h3 {
font-weight: $font-normal;
}
.btns {
display: grid;
justify-content: center;
justify-items: center;
.restart {
background: $green-color;
cursor: pointer;
#include transition-ease;
&:hover {
background: darken($green-color, 10%);
#include transition-ease;
}
}
.exit {
background: $red-color;
cursor: pointer;
#include transition-ease;
&:hover {
background: darken($red-color, 10%);
#include transition-ease;
}
}
}
&.endGameDisplay {
display: none;
}
}
Remove the period (.) when adding a class. .endGameDisplay should be endGameDisplay:
const endGame = document.querySelector('.endGame');
endGameOff();
//end game on func
function endGameOn() {
endGame.classList.remove('endGameDisplay');
}
//end game off func
function endGameOff() {
endGame.classList.add('endGameDisplay');
}
.endGameDisplay {
display: none;
}
<div class="endGame">
<h1>You</h1>
<h2 class="wonORlost">text</h2>
<h3>Do you want to restar game or go back to the menu?</h3>
<div class="btns">
<div class="restart">Restart</div>
<div class="exit">Exit</div>
</div>
</div>

eventListener stops working after scrolling down/changing property in javascript

I have a navigation bar. In it, I have some anchors. One of the anchors has the text 'Guides'. I want a div to appear when I hover over the anchor, and I want it to remain on the screen as long as I am hovering over the anchor. I also want the the div to remain on the screen as long as I am hovering over the div itself. All of this is working fine. When I scroll down, I decrease the size of the navigation bar and change the top position of the div. After I do this, the eventListener that I have set up to keep the div on the screen as long as I am hovering over it stops working. Please note that the div still appears when I hover over the anchor, and it remains on the screen as long as I am hovering over it.
Here's the code. The div is the one with id="dropdown-guides":
// Show dropdown on hovering over 'guides' in navigation bar
let guidesAnchor = document.querySelector('#nav-anchor-guides')
let guidesDropdown = document.querySelector('#dropdown-guides')
function showGuidesDropdown() {
guidesDropdown.style.display = 'block'
}
function hideGuidesDropDown() {
guidesDropdown.style.display = 'none'
}
guidesAnchor.addEventListener('mouseenter', showGuidesDropdown)
guidesAnchor.addEventListener('mouseleave', hideGuidesDropDown)
guidesDropdown.addEventListener('mouseenter', showGuidesDropdown)
guidesDropdown.addEventListener('mouseleave', hideGuidesDropDown)
// Show search bar on clicking search icon
let searchIcon = document.querySelector('#search-icon_anchor')
let searchBar = document.querySelector('#search-bar')
searchIcon.addEventListener('click', function() {
if (searchBar.style.display === 'none') {
searchBar.style.display = 'block'
} else {
searchBar.style.display = 'none'
}
})
// Change navigation bar on scrolling down
let navBar = document.querySelector('nav')
let mainIcon = document.querySelector('#nav-main-icon')
let navAnchors = document.querySelectorAll('nav a')
let iconDesignGuideAnchor = document.querySelector('#nav-dropdown-guides-icon-design-guide')
let pixelPerfectIconsAnchor = document.querySelector('#nav-dropdown-guides-crafting-pixel-perfect-icons')
let dribbbleAudienceAnchor = document.querySelector('#nav-dropdown-guides-build-your-dribbble-audience')
window.onscroll = () => {
if (document.body.scrollTop > 10 || document.documentElement.scrollTop > 10) {
navBar.style.height = '42px'
navBar.style.paddingTop = '10px'
navBar.style.boxShadow = '0px 0px 7px #0000001A'
mainIcon.style.top = '10px'
mainIcon.style.width = '97px'
mainIcon.style.height = '30px'
navAnchors.forEach(navAnchor => {
navAnchor.style.top = '14px'
navAnchor.style.height = '23px'
})
guidesDropdown.style.top = '42px'
searchBar.style.top = '52px'
iconDesignGuideAnchor.style.top = '65px'
pixelPerfectIconsAnchor.style.top = '108px'
dribbbleAudienceAnchor.style.top = '174px'
} else {
navBar.style.height = '80px'
navBar.style.paddingTop = '0px'
navBar.style.boxShadow = 'none'
mainIcon.style.top = '18px'
mainIcon.style.width = '139px'
mainIcon.style.height = '43px'
navAnchors.forEach(navAnchor => {
navAnchor.style.top = '28px'
navAnchor.style.height = '52px'
})
guidesDropdown.style.top = '80px'
searchBar.style.top = '80px'
iconDesignGuideAnchor.style.top = '103px'
pixelPerfectIconsAnchor.style.top = '146px'
dribbbleAudienceAnchor.style.top = '212px'
}
}
body {
margin: 0;
font-family: "Open Sans", Arial, sans-serif;
}
nav {
width: 100%;
height: 80px;
position: fixed;
border-bottom: 1px solid #E5E5E5;
z-index: 1;
background-color: #FFFFFF;
}
#nav-main-icon {
left: 135px;
top: 18px;
width: 139px;
height: 43px;
position: fixed;
}
a {
text-decoration: none;
}
nav a {
position: fixed;
top: 28px;
height: 52px;
font-family: "Open Sans", sans-serif;
font-size: 14px;
color: #666666;
}
nav a:hover {
color: #333333;
}
#nav-anchor-blog {
left: 748px;
}
#nav-anchor-guides {
left: 802px;
}
#nav-anchor-free-icons {
left: 887px;
}
#nav-anchor-free-wallpapers {
left: 979px;
}
#nav-anchor-about-me {
left: 1110px;
}
#nav-search-icon {
position: fixed;
left: 1197px;
width: 18px;
height: 17px;
font-size: 14px;
fill: #00000080;
}
#nav-search-icon:hover {
fill: #E74225;
}
.nav-dropdown {
top: 80px;
box-shadow: 0 2px 5px #0000001A;
border-top: 3px solid #E74225;
background-color: #FFFFFFFF;
display: none;
}
#dropdown-guides {
position: relative;
left: 776px;
width: 200px;
height: 175px;
padding: 20px;
z-index: 3;
}
#dropdown-guides a {
left: 796px;
width: 160px;
padding: 10px 20px;
line-height: 23px;
}
#dropdown-guides a:hover {
background-color: #00000008;
}
#nav-dropdown-guides-icon-design-guide {
top: 103px;
height: 23px;
}
#nav-dropdown-guides-crafting-pixel-perfect-icons {
top: 146px;
height: 46px;
}
#nav-dropdown-guides-build-your-dribbble-audience {
top: 212px;
height: 46px;
}
#search-bar {
position: absolute;
left: 895px;
width: 280px;
height: 35px;
padding: 20px;
z-index: 2;
}
#search-field {
left: 915px;
top: 103px;
width: 240px;
height: 15px;
padding: 10px 20px;
border: hidden;
background-color: #F8F8F8;
font-family: Arial;
font-weight: 400;
font-size: 13.3333px;
color: #757575;
}
#search-field:focus {
outline: none;
}
h1, h3, h4, p, a {
margin: 0;
font-weight: 500;
}
h1, h3, h4 {
text-align: center;
}
h1 {
font-size: 30px;
line-height: 30px;
color: #333333;
}
h3 {
font-size: 22px;
line-height: 22px;
color: #333333;
}
h4 {
font-size: 14px;
line-height: 24px;
color: #666666;
}
p {
font-size: 16px;
line-height: 27px;
color: #666666;
text-align: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Main - Icon Utopia</title>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="index-styles.css">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet">
</head>
<body>
<header>
<nav>
<img id="nav-main-icon" src="icon-utopia.png">
<a id="nav-anchor-blog" href="blog.html">Blog</a>
<a id="nav-anchor-guides" href="www.iconutopia.com">Guides</a>
<a id="nav-anchor-free-icons" href="https://iconutopia.com/free-icons/">Free Icons</a>
<a id="nav-anchor-free-wallpapers" href="https://iconutopia.com/free-phone-wallpapers/">Free Wallpapers</a>
<a id="nav-anchor-about-me" href="https://iconutopia.com/about/">About Me</a>
<a id="search-icon_anchor"><svg id="nav-search-icon" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32.2 32.2"><path d="M19 0C11.8 0 6 5.8 6 13c0 3.1 1.1 5.9 2.9 8.2l-8.6 8.6c-0.5 0.5-0.5 1.4 0 2 0.5 0.5 1.4 0.5 2 0l8.6-8.6C13.1 24.9 15.9 26 19 26c7.2 0 13-5.8 13-13S26.2 0 19 0zM19 24C12.9 24 8 19.1 8 13S12.9 2 19 2 30 6.9 30 13 25.1 24 19 24z"/></svg></a>
<div id="dropdown-guides" class="nav-dropdown">
<a id="nav-dropdown-guides-icon-design-guide" href="free-icon-design-guide.html">Icon Design Guide</a>
<a id="nav-dropdown-guides-crafting-pixel-perfect-icons" href="crafting-pixel-perfect-icons-the-right-way.html">Crafting Pixel Perfect Icons – The Right Way!</a>
<a id="nav-dropdown-guides-build-your-dribbble-audience" href="build-your-dribbble-audience.html">Build your Dribbble audience</a>
</div>
<div id="search-bar" class="nav-dropdown">
<form id="search-form">
<input id="search-field" type="text" placeholder="Search ...">
</form>
</div>
</nav>
</header>
</body>
<script src="nav.js"></script>
</html>
Please note that since the top set in CSS for the div is originally 80px, if I remove the code for changing the top of the div, after scrolling down, I can't reach it if I am hovering over the anchor, before it disappears. That's why I was not able to tell whether the eventListener stopped working because I scrolled down, or because I changed the top of the div.
I figured out the problem. In my javascript, I was setting the height of each navAnchor to fit only the text of the anchor. That's why the dropdown disappeared before I could hover over it. So I increased the height to extend the anchors to reach the bottom of the navigation bar. (I increased the height from 23px to 39px)

Javascript Dropdown Staying open

In my site I made a simple dropdown menu, but my problem is that it won't close if mouseleave happens on the <span> that triggers the dropdown.
Here is my code:
//Find the dropdown span
var header = document.getElementById('drop');
//Find the ul with the links
var ul = document.getElementById('nav-dropdown');
//Get the width and apply it to the dropdown items
var width = drop.getBoundingClientRect().width;
ul.style.minWidth = width + "px";
//Round the corners on the last link
var links = document.getElementsByClassName('dropdown-link');
links[links.length - 1].style.borderRadius = "0 0 7px 7px";
var open = 0;
//Onhover, display the dropdown;
header.addEventListener("mouseover", function() {
ul.style.display = "block";
header.style.borderRadius = "7px 7px 0 0";
if (links[0].getBoundingClientRect().width > width) {
links[0].style.borderRadius = "0 7px 0 0";
}
open = 1;
});
//When the mouse leaves the menu, close it.
ul.addEventListener("mouseleave", function() {
ul.style.display = "none";
header.style.borderRadius = "7px";
open = 0;
});
//What I've tried to fix it:
/*
header.addEventListener("mouseleave", function() {
ul.style.display = "none";
header.style.borderRadius = "7px";
});
*/
/*Stylesheet for this stuff*/
* {
font-family: arial;
margin: 0;
padding: 0;
box-sizing: border-box;
font-size: 20px;
text-decoration: none;
list-style: none;
}
a:visited {
color: white;
}
a,
#drop {
color: white;
}
a:hover {
color: coral;
}
.header-links-container {
position: relative;
top: 0;
background: rgb(63, 83, 95);
width: 100%;
height: 80px;
opacity: .8;
z-index: 999;
}
.title {
font-weight: bold;
font-size: 30px;
padding: 20px 50px;
position: relative;
float: left;
color: white;
}
.header-links {
position: relative;
float: right;
vertical-align: middle;
}
.nav-links {
margin: auto;
padding-top: 20px;
padding-right: 30px;
}
.nav-link {
position: relative;
float: right;
padding: 0 20px;
font-size: 23px;
padding: 5px 10px;
margin: 5px;
background: #4471ba;
border-radius: 7px;
}
.nav-link:hover {
background: #4480ba;
color: #d1d1d1;
}
#nav-dropdown {
display: none;
margin-top: 42px;
margin-left: 5px;
position: absolute;
}
.dropdown-link {
color: black;
background-color: #ccc;
padding: 5px 10px;
}
.dropdown-link:hover {
color: #000;
background-color: #a7a7a7;
}
.dropdown-link:active {
color: white;
background-color: #3b8cfa;
}
<div class="header-links-container">
<h2 class="title">Title</h2>
<div class="header-links">
<ul class="nav-links">
<li class="nav-link">Photo Gallery</li>
<li class="nav-link">SLAP</li>
<li id="drop" class="nav-link"><span>Dropdown</span></li>
<ul id="nav-dropdown" class="jim">
<a href="#">
<li class="dropdown-link">Link 1</li>
</a>
<a href="#">
<li class="dropdown-link">Link 2</li>
</a>
<a href="#">
<li class="dropdown-link">Longer Link</li>
</a>
<a href="#">
<li class="dropdown-link">Vacuum</li>
</a>
</ul>
</ul>
</div>
</div>
<p>
Relavent JS lines start at Line 16
</p>
And here is the fiddle that might make more sense: https://jsfiddle.net/dLw1hu5n/6/
I've tried closing the dropdown like in the last code block, but then it won't stay open when you go to hover over the links. I've also tried making the menu close when the mouse hovers over the navbar div, but no luck there either.
Can I fix this or do I need to start from square 1?
I would prefere to solve this via css. However, in your case you can try the following:
function displayDropdown() {
ul.style.display = "block";
header.style.borderRadius = "7px 7px 0 0";
if (links[0].getBoundingClientRect().width > width) {
links[0].style.borderRadius = "0 7px 0 0";
}
open = 1;
}
function hideDropdown() {
ul.style.display = "none";
header.style.borderRadius = "7px";
open = 0;
}
//Onhover, display the dropdown;
header.addEventListener("mouseover", function() {
displayDropdown();
});
ul.addEventListener("mouseover", function() {
displayDropdown();
});
//When the mouse leaves the menu, close it.
ul.addEventListener("mouseleave", function() {
hideDropdown();
});
header.addEventListener("mouseleave", function() {
hideDropdown();
});
Your JS is fine but your event listener for mouseleave needs to be on the enclosing div. This way your element stays open until you hover outside of the header
t.addEventListener("mouseleave", function() {
ul.style.display = "none";
header.style.borderRadius = "7px";
open = 0;
});
What is t?
var t = document.getElementById(t);
What element has id T?
Try this fiddle to find out https://jsfiddle.net/dLw1hu5n/12/

html/css interactive calendar next/prev day button coding

I am using a tutorial I found online to fit my needs for a project. I would like to add in functional buttons that select between days (changing "active" days in calendar) and select between months.
My first question is how do I code the prev day and next day buttons to change the previous and next day to active according to css?
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
ul {
list-style-type: none;
}
body {
font-family: Verdana, sans-serif;
}
.month {
padding: 70px 25px;
width: 100%;
background: #1abc9c;
}
.month ul {
margin: 0;
padding: 0;
}
.month ul li {
color: white;
font-size: 40px;
text-transform: uppercase;
letter-spacing: 3px;
}
.month .prev {
float: left;
padding-top: 10px;
}
.month .prevDay {
float: left;
}
.month .next {
float: right;
padding-top: 10px;
}
.month .nextDay {
float: right;
}
.weekdays {
margin: 0;
padding: 10px 0;
background-color: #ddd;
}
.weekdays li {
display: inline-block;
width: 13.6%;
color: #666;
text-align: center;
}
.days {
padding: 10px 0;
background: #eee;
margin: 0;
}
.days li {
list-style-type: none;
display: inline-block;
width: 13.6%;
text-align: center;
margin-bottom: 5px;
font-size: 12px;
color: #777;
}
.days li .active {
padding: 5px;
background: #1abc9c;
color: white !important
}
/* Add media queries for smaller screens */
#media screen and (max-width: 720px) {
.weekdays li,
.days li {
width: 13.1%;
}
}
#media screen and (max-width: 420px) {
.weekdays li,
.days li {
width: 12.5%;
}
.days li .active {
padding: 2px;
}
}
#media screen and (max-width: 290px) {
.weekdays li,
.days li {
width: 12.2%;
}
}
</style>
<style>
.pM_button {
padding: 15px 25px;
font-size: 24px;
text-align: center;
cursor: pointer;
outline: none;
color: #cc9900;
background-color: #ffff00;
border: none;
border-radius: 15px;
box-shadow: 0 9px #999;
}
.pM_button:hover {
background-color: #ffff00
}
.pM_button:active {
background-color: #ffcc00;
box-shadow: 0 5px #666;
transform: translateY(4px);
}
.nM_button {
padding: 15px 25px;
font-size: 24px;
text-align: center;
cursor: pointer;
outline: none;
color: #ffffff;
background-color: #9900cc;
border: none;
border-radius: 15px;
box-shadow: 0 9px #999;
}
.nM_button:hover {
background-color: #9900cc
}
.nM_button:active {
background-color: #660066;
box-shadow: 0 5px #666;
transform: translateY(4px);
}
.nD_button {
padding: 15px 25px;
font-size: 24px;
text-align: center;
cursor: pointer;
outline: none;
color: #ffffff;
background-color: #ff0000;
border: none;
border-radius: 15px;
box-shadow: 0 9px #999;
}
.nD_button:hover {
background-color: #ff0000
}
.nD_button:active {
background-color: #800000;
box-shadow: 0 5px #666;
transform: translateY(4px);
}
.pD_button {
padding: 15px 25px;
font-size: 24px;
text-align: center;
cursor: pointer;
outline: none;
color: #ffffff;
background-color: #33cc33;
border: none;
border-radius: 15px;
box-shadow: 0 9px #999;
}
.pD_button:hover {
background-color: #33cc33
}
.pD_button:active {
background-color: #009900;
box-shadow: 0 5px #666;
transform: translateY(4px);
}
</style>
</head>
<body onkeydown="GetKey()">
<div class=" month ">
<ul>
<li class="prev ">
<button class="pM_button ">Prev Month</button>
</li>
<li class="prevDay ">
<button class="pD_button ">Prev Day</button>
</li>
<li class="next ">
<button class="nM_button ">Next Month</button>
</li>"
<li class="nextDay ">
<button class="nD_button ">Next Day</button>
</li>
<li style="text-align:center ">
August
<br>
<span style="font-size:18px ">2016</span>
</li>
</ul>
</div>
<ul class="weekdays ">
<li>Mo</li>
<li>Tu</li>
<li>We</li>
<li>Th</li>
<li>Fr</li>
<li>Sa</li>
<li>Su</li>
</ul>
<ul class="days ">
<li><span class="active ">1</span>
</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>11</li>
<li>12</li>
<li>13</li>
<li>14</li>
<li>15</li>
<li>16</li>
<li>17</li>
<li>18</li>
<li>19</li>
<li>20</li>
<li>21</li>
<li>22</li>
<li>23</li>
<li>24</li>
<li>25</li>
<li>26</li>
<li>27</li>
<li>28</li>
<li>29</li>
<li>30</li>
<li>31</li>
</ul>
<script type="text/javascript ">
function GetKey(e) {
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
//var character = String.fromCharCode(code);
setTimeout('ShowTree(' + code + ');', 100);
}
function nextDay() {
}
function ShowTree(character, k) {
//Main Menu Key
if (character == 106) {
cWindow.close();
}
//Close Key
if (character == 111) {
alert(" Key: / ");
responsiveVoice.speak("Close ", "UK English Male ");
}
//PageUP Key, next month
if (character == 98) {
alert(" Key: 2 ");
responsiveVoice.speak("Page Up ", "UK English Male ");
}
//PageDOWN key, previous month
if (character == 99) {
alert(" Key: 3 ");
responsiveVoice.speak("Page Down ", "UK English Male ");
}
//Previous Key, Previous Day
if (character == 65) { //keypad key 101
responsiveVoice.speak("Previous ", "UK English Male ");
//alert(" Key: 5 ");
}
//Next Key, Next Day
if (character == 68) { //keypad key 102
responsiveVoice.speak("Next ", "UK English Male ");
$(".pD_button ").click(.days.active++);
//alert(" Key: 6 ");
}
//Select Key
if (character == 83) { //keypad key 104
responsiveVoice.speak("Select ", "UK English Male ");
}
//alert(" Key: 8 ");
}
</script>
</body>
</html>
You can easily add the active class to any DOM element by using addClass("class") and remove the class with removeClass("class"), provided you're using jQuery.
So if you wish to mark the pD_button as active, you can do it like this with jQuery
$(".pD_button").addClass("active");
If you wish to accomplish this with vanilla JavaScript you would have to write your own functions that add and remove a class.
You could do that with
classList https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
or
className https://developer.mozilla.org/en-US/docs/Web/API/Element/className.
This approach is better than using the :active pseudo-class in CSS.
The :active pseudo-class is triggered when the mouse is being clicked, but only while the mouse button is held down. That means after you release the mouse click, :active is turned off. By using the approach mentioned above you just add another class when an onclick event happens to get the desired behavior.
var prevButton = $(".pD_button");
prevButton.on("click", function() {
if (!prevButton.hasClass("active")) {
prevButton.addClass("active");
} else /*if (prevButton.hasClass("active"))*/ {
prevButton.removeClass("active");
}
});
You can check it out here: https://jsfiddle.net/cqm26q1n/.
It is important that the .active in your CSS comes after the .pD_button class, so that it overwrites its CSS when the active class get's attached to it.
EDIT:
Use the approach I suggested combined with jQuery .keydown(handler):
var prevButton = $(".pD_button");
prevButton.on("keydown", function(e) {
if (e.which === 102 || e.which === 68) {
if (!prevButton.hasClass("active")) {
prevButton.addClass("active");
} else /*if (prevButton.hasClass("active"))*/ {
prevButton.removeClass("active");
}
}
});
Check the documentation of keydown here.
Assuming you are jQuery ( noticing presence of $ sign and .click event), There are couple of errors in this section you wrote.
//Next Key, Next Day
if (character == 68) { //keypad key 102
responsiveVoice.speak("Next ", "UK English Male ");
$(".pD_button ").click(.days.active++);
//alert(" Key: 6 ");
}
More specifically in this like $(".pD_button ").click(.days.active++);
General jQuery event works like, $("SELECTOR").EVENT(CALLBACK_FUNCTION);
So, in your case, to go to next date, the code should be like
//Next Key, Next Day
if (character == 68) { //keypad key 102
responsiveVoice.speak("Next ", "UK English Male ");
$(".pD_button ").click(function(e){
$('ul.days').find("li.active").removeClass('active').next().addClass('active');
e.preventDefault();
});
//alert(" Key: 6 ");
}
Similarly, to go make previous date active, you just need to use .after() method instead of .next() at this line $('ul.days').find("li.active").removeClass('active').next().addClass('active');
To know more about jQuery .next() methods, check the links
https://api.jquery.com/click/ to know about .click event

Expand/collapse a menu list with animation

Here's what I'm trying to make:
Click on "PORTFOLIO";
Pushes everything down smoothly;
New links fade-in smoothly;
Click on "PORTFOLIO" again, do everything in reverse;
My current code;
$(function(){
$('[data-toggle]').on('click', function(){
var id = $(this).data("toggle"),
$object = $(id),
className = "open";
if ($object) {
if ($object.hasClass(className)) {
$object.removeClass(className)
} else {
$object.addClass(className)
}
}
});
});
#list{
display: none;
}
#list.open{
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<ul>
<li>Home</li>
<li>A Empresa</li>
<li>Portfolio
<ul id="list">
<li>Comerciais</li>
<li>Residenciais</li>
<li>Institucionais</li>
<li>Edifícios</li>
</ul>
</li>
<li>Contato</li>
</ul>
</nav>
It's possible to accomplish this without JS, only with CSS? I have no clue whatsoever how to do the animation part, I tried CSS Transitions propriety, but didn't work.
Also, any tips for the markup and JS? I don't thinks I'm doing it the "right way"... any tips would be appreciated.
Edit: Never mind someone answered your question in the comments.
I am not sure I fully understand your question as it looks like your code works well.
I think that if you do not want to not have the text changed for the + and - you could add a new element/tag that shows that at the end of the list items.
I am not so grate at JS but the text() or html() can be used on a span within the list text that way it does not get changed.
HTML:
<nav>
<ul>
<li>Home</li>
<li>A Empresa</li>
<li>Portfolio <span id="item3icon">+</span>
<ul id="list" class="subClassHide">
<li>Comerciais</li>
<li>Residenciais</li>
<li>Institucionais</li>
<li>Edifícios</li>
</ul>
</li>
<li>Contato</li>
</ul>
JS:
$('#item3link').on('click', function(){
if (toggleOn == 1) {
toggleOn = 0;
$('#item3icon').html('+');
$('#list').addClass("subClassHide");
}
else
{
toggleOn = 1;
$('#item3icon').html('-');
$('#list').removeClass("subClassHide");
}
});
var toggleOn = 0;
CSS:
(I changed this a bit but it is not important, it was just close to the solution I had locally)
.subClassHide {
display: none;
}
$('#item3link').on('click', function() {
if (toggleOn == 1) {
toggleOn = 0;
$('#item3icon').html('+');
$('#list').addClass("subClassHide");
} else {
toggleOn = 1;
$('#item3icon').html('-');
$('#list').removeClass("subClassHide");
}
});
var toggleOn = 0;
.subClassHide {
display: none;
}
#import url(http://fonts.googleapis.com/css?family=Lato:900);
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
-o-box-sizing: border-box;
box-sizing: border-box;
margin: 0;
padding: 0;
-webkit-touch-callout: none;
-webkit-text-size-adjust: none;
-webkit-user-select: none;
-webkit-highlight: none;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
html {
font-size: 16px;
-webkit-transition: all .15s ease;
}
body {
font-family: 'Lato', Arial, sans-serif;
text-transform: uppercase;
}
a {
color: inherit;
text-decoration: none;
}
nav {
margin: .5em;
padding: .5em;
}
nav:before,
nav:after {
content: '';
display: block;
background: #000;
height: .3em;
width: 1.2em;
}
nav ul {
list-style: none;
}
nav>ul>li {
cursor: pointer;
line-height: 1.8em;
}
#list li {
color: #800;
font-size: .7em;
position: relative;
margin-left: 1.5em;
}
#list li:after {
content: '';
display: block;
background: #800;
height: 2px;
width: .4em;
position: absolute;
top: 50%;
left: -.8em;
}
#list.open {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav>
<ul>
<li>Home</li>
<li>A Empresa</li>
<li>Portfolio <span id="item3icon">+</span>
<ul id="list" class="subClassHide">
<li>Comerciais</li>
<li>Residenciais</li>
<li>Institucionais</li>
<li>Edifícios</li>
</ul>
</li>
<li>Contato</li>
</ul>
</nav>
If I misunderstood I hope this post will (at the least) give you ideas.

Categories

Resources