I'm using Bootstrap and I get the JavaScript code for the filterable div elements from W3Schools. It's an easy mode to filter but it's a bit bored without animations.
Because of this, I'm trying to add some animations by CSS or js in my filter code and I don't know how to run it. I tried CSS and js animations but nothing, it's not working.
HTML
<div class="container-fluid">
<div id="myBtnContainer">
<ul>
<li class="list-inline-item"><button class="btn active" onclick="filterSelection('all')">All</button></li>
<li class="list-inline-item"><button class="btn" onclick="filterSelection('category1')">Category1</button></li>
<li class="list-inline-item"><button class="btn" onclick="filterSelection('category2')">Category2</button></li>
</ul>
</div>
<div class="row row-cols-1 row-cols-sm-2 row-cols-lg-3 g-3 pt-2 pb-5">
<div class="col fade-in-image filterDiv category1 show">
<a href="http://localhost:8888/test01/projects/project1/">
<div class="card">
<img width="480" height="320" src="http://localhost:8888/test01/wp-content/uploads/2021/06/img1.jpeg" class="img-fluid wp-post-image" alt="" loading="lazy">
<div class="hover-project">
<h3 class="titol-projecte">Project 1</h3>
<p class="location pb-4">Location 1</p>
<p class="category">Category 1</p>
</div>
</div>
</a>
</div>
<div class="col fade-in-image filterDiv category-2 show">
<a href="http://localhost:8888/test01/projects/project2/">
<div class="card">
<img width="480" height="320" src="http://localhost:8888/test01/wp-content/uploads/2021/06/img2.jpeg" class="img-fluid wp-post-image" alt="" loading="lazy">
<div class="hover-project">
<h3 class="titol-projecte">Project 2</h3>
<p class="location pb-4">Location 2</p>
<p class="category">Category 2</p>
</div>
</div>
</a>
</div>
</div>
</div>
The JavaScript I used:
filterSelection("all")
function filterSelection(c) {
var x, i;
x = document.getElementsByClassName("filterDiv");
if (c == "all") c = "";
// Add the "show" class (display:block) to the filtered elements, and remove the "show" class from the elements that are not selected
for (i = 0; i < x.length; i++) {
w3RemoveClass(x[i], "show");
if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
}
}
// Show filtered elements
function w3AddClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {
element.className += " " + arr2[i];
}
}
}
// Hide elements that are not selected
function w3RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
while (arr1.indexOf(arr2[i]) > -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(" ");
}
// Add active class to the current control button (highlight it)
var btnContainer = document.getElementById("myBtnContainer");
var btns = btnContainer.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
The CSS i used
.filterDiv {
display: none; /* Hidden by default */
visibility: hidden;
opacity: 0;
}
/* The "show" class is added to the filtered elements */
.show {
display: block;
visibility: visible;
opacity: 1;
}
Thank you!!
Here is one ready example you can use for your code, its pretty amazing in my opinion
<div class="wrap">
<div class="container">
<div class="row">
<div class="twelve columns">
<h4>Filter Programs</h4>
</div>
</div><!-- end of row -->
<div class="row">
<div class="twelve columns">
<div class="programs">
<button class="filter-btn" data-filter="all">All</button>
<button class="filter-btn" data-filter=".undergraduate">Undergraduate</button>
<button class="filter-btn" data-filter=".graduate">Graduate</button>
<button class="filter-btn" data-filter=".phd">PhD</button>
</div>
</div>
</div><!-- end of row -->
<div class="row">
<div class="columns twelve">
<h4>Sort Programs</h4>
</div>
</div><!-- end of row -->
<div class="row">
<div class="columns twelve">
<div class="programs">
<button class="sort-btn" data-sort="default:asc">Default</button>
<button class="sort-btn" data-sort="random">Random</button>
<button class="sort-btn" data-sort="order:asc">Ascending</button>
<button class="sort-btn" data-sort="year:desc order:desc">Descending<span
class="multi">(Multi)</span></button>
</div>
</div>
</div><!-- end of row -->
<div class="row">
<div class="twelve columns">
<h4>Programs List</h4>
</div>
</div><!-- end of row -->
<div class="row">
<div class="twelve columns">
<ul class="courses" id="mix-wrapper">
<li class="mix-target undergraduate" data-order="5" data-year="4">Economics<span>(U)</span></li>
<li class="mix-target graduate" data-order="14" data-year="2">Pharmacology<span>(G)</span></li>
<li class="mix-target graduate" data-order="7" data-year="1">Informatics<span>(G)</span></li>
<li class="mix-target phd" data-order="4" data-year="3">Criminology<span>(P)</span>
</li>
<li class="mix-target undergraduate" data-order="16" data-year="3">Sociology<span>(U)</span></li>
<li class="mix-target undergraduate" data-order="6" data-year="4">Greek<span>(U)</span></li>
<li class="mix-target phd" data-order="1" data-year="3">Astrophysics<span>(P)</span>
</li>
<li class="mix-target undergraduate" data-order="12" data-year="4">Nursing<span>(U)</span></li>
<li class="mix-target undergraduate" data-order="10" data-year="5">Microbiology<span>(U)</span></li>
<li class="mix-target undergraduate" data-order="9" data-year="4">Mathematics<span>(U)</span></li>
<li class="mix-target graduate" data-order="11" data-year="3">Nanoscience<span>(G)</span></li>
<li class="mix-target phd" data-order="2" data-year="2">Biochemistry<span>(P)</span>
</li>
<li class="mix-target phd" data-order="13" data-year="3">Pathology<span>(P)</span>
</li>
<li class="mix-target graduate" data-order="8" data-year="1">Management<span>(G)</span></li>
<li class="mix-target graduate" data-order="3" data-year="2">Biostatistics<span>(G)</span></li>
<li class="mix-target phd" data-order="15" data-year="4"><a href="#">Public
Health<span>(P)</span></a></li>
</ul>
</div>
</div>
</div>
</div>
The CSS:
#import url(https://fonts.googleapis.com/css?family=Alegreya+Sans+SC);
body {
background: #ededed;
font-family: 'Alegreya Sans SC', sans-serif;
}
h4 {
font-size: 21px;
border-radius: 5px;
margin: 0 auto;
text-align: center;
background: #d2d2d2;
color: white;
font-weight: 700;
padding: 3px 0;
}
li {
margin: 0;
}
a {
font-size: 18px;
text-decoration: none;
}
span {
display: block;
font-size: .75em;
font-style: italic;
position: relative;
top: 5px;
}
.multi {
display: inline;
top: 0;
left: 3px;
}
img {
max-width: 100%;
}
.programs,
.courses {
margin: 8px 0 0 0;
}
.programs {
font-size: 0;
margin-bottom: 15px;
}
.programs button {
outline: none;
margin-bottom: 0;
background: whitesmoke;
width: 50%;
height: auto;
font-weight: normal;
border: 1px solid #ededed;
color: #000000;
font-size: 14px;
padding: 4px 0;
text-shadow: 0px 0px 0px #2f6627;
}
.programs button:hover {
background: #99cfe0;
}
.programs button.programs-filter-btn-active,
.programs button.programs-sort-btn-active {
box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.5) inset, 0px 0px 1px transparent;
background: #3a9fbf;
color: white;
}
.courses {
margin-bottom: 15px;
font-size: 0;
background: whitesmoke;
}
.courses li {
text-align: center;
font-size: 14px;
display: inline-block;
width: 46%;
margin: 0 2%;
}
.courses a {
display: block;
height: 60px;
margin: 15px 0;
padding-top: 12.5px;
background: whitesmoke;
color: black;
border: 1px solid white;
transition: all .4s ease;
}
.courses a:hover {
background: #99cfe0;
}
.p, .p a {
font-family: Arial, sans-serif;
font-size: 14px;
text-align: center;
}
/* MEDIA QUERIES STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */
#media only screen and (min-width: 750px) {
.container {
width: 95%;
}
.courses a {
background: #EBEBEB;
border: none;
}
h4 {
width: 50%;
margin-top: 12px;
}
img {
margin-bottom: 0;
}
.programs {
margin-bottom: 0;
}
.programs button {
width: 25%;
margin-bottom: 15px;
}
.courses li {
width: 21%;
}
}
The JS:
mixitup('#mix-wrapper', {
load: {
sort: 'order:asc'
},
animation: {
effects: 'fade rotateZ(-180deg)',
duration: 700
},
classNames: {
block: 'programs',
elementFilter: 'filter-btn',
elementSort: 'sort-btn'
},
selectors: {
target: '.mix-target'
}
});
Related
I have some divs generated dynamically. Once all the divs are selected submit button has to be enabled, can someone please help me?
let card = document.getElementsByClassName('card');
let allSelected = document.querySelectorAll('selected');
let btn = document.getElementsByClassName('btn');
for (var i = 0; i < card.length; i++) {
card[i].addEventListener('click', function(e) {
e.target.classList.add('selected');
})
}
.card.selected{
background: #0173fb;
}
<section class='container'>
<ul class="cards">
<li class="cardItm">
<div class="card">
card items
</div>
</li>
</ul>
<button class='btn'>Submit</button>
</section>
let card = document.getElementsByClassName('card');
let allSelected = document.querySelectorAll('selected');
let btn = document.getElementsByClassName('btn');
for (var i = 0; i < card.length; i++) {
card[i].addEventListener('click', function(e) {
e.target.classList.add('selected');
})
}
In this code I am adding 'selected' class for the div onclick. I want this in pure javascript.. no jquery or any other library.
Thanks in advance.
try like this. Pure javascript.
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementsByClassName('btn')[0].setAttribute("disabled","true");
});
let card = document.getElementsByClassName('card');
let btn = document.getElementsByClassName('btn');
for (var i = 0; i < card.length; i++) {
card[i].addEventListener('click', function(e) {
e.target.classList.toggle('selected');
if(card.length === document.getElementsByClassName('selected').length){
btn[0].removeAttribute("disabled");
}
else{
btn[0].setAttribute("disabled","true");
}
})
}
.card{
padding: 8px;
border: 1px solid #eee;
cursor: pointer;
}
ul, li{
list-style: none;
}
.card.selected{
background-color: rgb(155,155,155);
}
.btn{
padding: 10px;
margin: 10px;
}
<body>
<section class='container'>
<ul class="cards">
<li class="cardItm">
<div class="card">
card items 1
</div>
<div class="card">
card items 2
</div>
<div class="card">
card items 3
</div>
</li>
</ul>
<button class='btn'>Submit</button>
</section>
</body>
Or you can add eventListener without for. Like below:
let card = document.getElementsByClassName('card');
let btn = document.getElementsByClassName('btn');
document.body.addEventListener('click', function(e) {
if(e.target.classList.contains('card')){
e.target.classList.toggle('selected');
if(card.length === document.getElementsByClassName('selected').length){
btn[0].removeAttribute("disabled");
}
else{
btn[0].setAttribute("disabled","true");
}
}
});
Compare the available divs with the selected divs and enable the button if it matches.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
div{
height: 10px;
background-color: red;
margin-bottom: 10px;
}
.selected{
background-color: blue;
}
</style>
</head>
<body>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<button class="btn" disabled>Click</button>
<script>
let card = document.getElementsByClassName('card');
let btn = document.getElementsByClassName('btn');
for (var i = 0; i < card.length; i++) {
card[i].addEventListener('click', function(e) {
// add seclected class to clicke div
e.target.classList.add('selected');
// get all selected/clicked divs
let allSelected = document.querySelectorAll('.selected');
// compare available divs with selected divs
if(card.length === allSelected.length){
console.log("Enable button", btn[0])
btn[0].onclick = () => {
alert("Button clicked");
};
// enable button
btn[0].disabled = false;
}
});
}
</script>
</body>
</html>
EDIT: Troubleshooting (https://github.com/Bhanumathi-a/cards)
Your problem with your code is that you add the "selected" class on sub elements too! `` When you add a console log on the event handler you see that there are other elements than the divs get selected which gives you the filled array that matches the length of the available divs:
You can clearly see that the array contains more elements than only divs.
To fix this, find the nearest div.card element from the clicked children:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Member FDIC</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Roboto', sans-serif;
margin: 0;
padding: 0;
}
.container {
width: 100%;
margin: 0 auto;
min-width: 360px;
}
header {
display: block;
}
a.logo {
display: inline-block;
font-size: 50px;
color: #0173fb;
text-decoration: none;
padding: 15px;
}
nav {
display: inline-block;
}
.loanDetails {
background: #f8f8f8;
display: flex;
}
.cardBlk {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.cards {
display: flex;
flex-wrap: wrap;
list-style: none;
margin: 0;
padding: 0;
}
.cardItm {
display: flex;
padding: 10px;
}
#media(min-width:1240px) {
.container {
max-width: 1140px;
}
.cardItm {
width: 24%;
}
}
#media(min-width:1920px) {
.container {
max-width: 1820px;
}
.cardItm {
width: 33%;
}
}
.card {
background: #f8f8f8;
color: #2a2b2d;
border-radius: 10px;
padding: 15px 15px 30px 15px;
box-sizing: border-box;
width: 100%;
display: block;
overflow: hidden;
position: relative;
text-align: center;
cursor: pointer;
justify-content: center;
}
.card .info, .card .checked {
height: 39px;
width: 39px;
position: absolute;
top: 10px;
right: 10px;
border-radius: 50%;
}
.card .info {
background: url(assets/exclamation.png) no-repeat 0 0;
}
.card .checked {
background: url(assets/checked\ active.png) no-repeat 0 0;
}
.card img {
width: auto;
height: auto;
max-width: 50px;
margin: 25px auto;
}
.card.selected, .card:hover {
background: #0173fb;
color: #fff;
}
.card:hover .info, .card.selected .info {
background-image: url(assets/exclamation\ white.png);
}
.card:hover .checked, .card.selected .checked {
background-image: url(assets/checked\ inactive.png);
}
.btnBlk {
text-align: center;
display: block
}
.btn {
background: #0173fb;
border-radius: 25px;
padding: 10px 25px;
color: #fff;
text-align: center;
border: 1px solid #0173fb;
margin: 10px auto;
cursor: pointer;
display: inline-block;
text-transform: uppercase;
}
.btn:disabled {
background: #9ac5fb;
border-color: #9ac5fb;
cursor: default;
}
</style>
</head>
<body>
<header>
<div class="container">
Logo
<!-- <nav class="nav">
<ul>
<li>Dashboard</li>
<li>Welcome, John Doe</li>
</ul>
</nav> -->
</div>
</header>
<main>
<header class="loanDetails">
<div class="container">
loan
<progress value="30" max="100" id=progress>30%</progress>
</div>
</header>
<section class='container'>
<ul class="cards">
<li class="cardItm">
<div class="card 1">
<span class="checked"></span>
<img src="assets/location.png" alt="">
<div>
<h2>Get started</h2>
<span>(100% completed)</span>
</div>
</div>
</li>
<li class="cardItm">
<div class="card 2">
<span class="info"></span>
<img src="assets/property.png" alt="">
<div>
<h2>Get started</h2>
<span>(Not Started)</span>
</div>
</div>
</li>
<li class="cardItm">
<div class="card">
<span class="info"></span>
<img src="assets/income.png" alt="">
<div>
<h2>Get started</h2>
<span>(Not Started)</span>
</div>
</div>
</li>
<li class="cardItm">
<div class="card">
<span class="checked"></span>
<img src="assets/assets.png" alt="">
<div>
<h2>Get started</h2>
<span>(50% completed)</span>
</div>
</div>
</li>
<li class="cardItm">
<div class="card">
<span class="checked"></span>
<img src="assets/real estate.png" alt="">
<div>
<h2>Get started</h2>
<span>(70% completed)</span>
</div>
</div>
</li>
<li class="cardItm">
<div class="card">
<span class="info"></span>
<img src="assets/declarations.png" alt="">
<div>
<h2>Get started</h2>
<span>(90% completed)</span>
</div>
</div>
</li>
<li class="cardItm">
<div class="card">
<span class="checked"></span>
<img src="assets/e-concent.png" alt="">
<div>
<h2>Get started</h2>
<span>(Not Started)</span>
</div>
</div>
</li>
</ul>
<div class="btnBlk">
<button class="btn" id="submitBtn" disabled>Submit Application</button>
</div>
</section>
</main>
<footer>
</footer>
<script>
let card = document.getElementsByClassName('card');
let btn = document.getElementsByClassName('btn');
let progress = document.getElementById('progress');
for (var i = 0; i < card.length; i++) {
card[i].addEventListener('click', function (e) {
let closest = e.target.closest("div.card");
closest.classList.add('selected');
progress.value = progress.value + 10;
let allSelected = document.querySelectorAll('div.selected');
if (card.length === allSelected.length) {
btn[0].onclick = () => {
console.log('clicked');
};
btn[0].disabled = false;
}
});
}
</script>
</body>
</html>
I want to have a div that is displayed when I click a button.
Right now I am only able to display something that is within the button I will click, like so:
var header = document.getElementById("myDIV");
var btns = header.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
if (current.length > 0) {
current[0].className = current[0].className.replace(" active", "");
}
this.className += " active";
});
}
.mainmenu {
display: block;
width: 100% !important;
background: red;
height: auto;
}
.btn {
border: none;
outline: none;
padding: 10px 16px;
background-color: #f1f1f1;
cursor: pointer;
font-size: 18px;
height: auto;
widht: 100%;
}
/* Style the active class, and buttons on mouse-over */
.active, .btn:hover {
background-color: #666;
color: #fff;
}
.submenu > p {
display: none;
background: green;
}
.active p {
display: block;
}
<div id="myDIV">
<div class="mainmenu">
<button class="btn">1
<div class="submenu">
<p> TEST1</p>
</div>
</button>
</div>
<div class="mainmenu">
<button class="btn">2 <div class="submenu">
<p> TEST2</p>
</div></button>
</div>
<div class="mainmenu">
<button class="btn">3 <div class="submenu">
<p> TEST3</p>
</div></button>
</div>
<div class="mainmenu">
<button class="btn">4 <div class="submenu">
<p> TEST4</p>
</div></button>
</div>
<div class="mainmenu">
<button class="btn">5 <div class="submenu">
<p> TEST5</p>
</div></button> </div>
</div>
If I move the div I want to display out of the button, I know that I have to adjust the javascript code to still display the div which is now not in the button anymore. unfortunately I don't know how.
So I am basically trying to display the div (which is working in the first jsfiddle) when clicking a button, but now I want to move the div out of the button. (which I tried in the second jsfiddle)
When I moved the div out of the button the code is not working. I am sure that there is a problem with my javascript but I don't know how to fix that.
var header = document.getElementById("myDIV");
var btns = header.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
if (current.length > 0) {
current[0].className = current[0].className.replace(" active", "");
}
this.className += " active";
});
}
.mainmenu {
display: block;
width: 100% !important;
background: red;
height: auto;
}
.btn {
border: none;
outline: none;
padding: 10px 16px;
background-color: #f1f1f1;
cursor: pointer;
font-size: 18px;
height: auto;
widht: 100%;
}
/* Style the active class, and buttons on mouse-over */
.active, .btn:hover {
background-color: #666;
color: #fff;
}
.submenu > p {
display: none;
background: green;
}
.active p {
display: block;
}
<div id="myDIV">
<div class="mainmenu">
<button class="btn">1 </button>
<div class="submenu">
<p> TEST1</p>
</div>
</div>
<div class="mainmenu">
<button class="btn">2</button> <div class="submenu">
<p> TEST2</p>
</div>
</div>
<div class="mainmenu">
<button class="btn">3</button> <div class="submenu">
<p> TEST3</p>
</div>
</div>
<div class="mainmenu">
<button class="btn">4</button> <div class="submenu">
<p> TEST4</p>
</div>
</div>
<div class="mainmenu">
<button class="btn">5</button> <div class="submenu">
<p> TEST5</p>
</div> </div>
</div>
Is it possible to do this and even align the buttons to the right but the div to the left?
Selecting the <p> tag in the CSS is wrong, as you have now moved the div outside so you have to select that like,
.active + .submenu p {
....
}
+ means the element immediately after the specified element but not
inside the particular elements.
Here is your updated code,
var header = document.getElementById("myDIV");
var btns = header.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
if (current.length > 0) {
current[0].className = current[0].className.replace(" active", "");
}
this.className += " active";
});
}
.mainmenu {
display: block;
width: 100% !important;
background: red;
height: auto;
}
.btn {
border: none;
outline: none;
padding: 10px 16px;
background-color: #f1f1f1;
cursor: pointer;
font-size: 18px;
height: auto;
widht: 100%;
}
/* Style the active class, and buttons on mouse-over */
.active, .btn:hover {
background-color: #666;
color: #fff;
}
.submenu > p {
display: none;
background: green;
}
.active + .submenu p {
display: block;
}
<div id="myDIV">
<div class="mainmenu">
<button class="btn">1 </button>
<div class="submenu">
<p> TEST1</p>
</div>
</div>
<div class="mainmenu">
<button class="btn">2</button> <div class="submenu">
<p> TEST2</p>
</div>
</div>
<div class="mainmenu">
<button class="btn">3</button> <div class="submenu">
<p> TEST3</p>
</div>
</div>
<div class="mainmenu">
<button class="btn">4</button> <div class="submenu">
<p> TEST4</p>
</div>
</div>
<div class="mainmenu">
<button class="btn">5</button> <div class="submenu">
<p> TEST5</p>
</div> </div>
</div>
I am using a plugin from w3schools to filter elements. It works perfectly in one of my projects. But now I am trying to use it in another project of mine but it doesn't work. Here is the code pen:
https://codepen.io/zakero/pen/mZYBPz
can anyone find an issue in this, please?
Javascript:
filterSelection("all")
function filterSelection(c) {
var x, i;
x = document.getElementsByClassName("work-images");
if (c == "all") c = "";
for (i = 0; i < x.length; i++) {
w3RemoveClass(x[i], "show");
if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
}
}
function w3AddClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {element.className += " " + arr2[i];}
}
}
function w3RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
while (arr1.indexOf(arr2[i]) > -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(" ");
}
// Add active class to the current button (highlight it)
// change is-checked class on buttons
$('.button-group').each( function( i, buttonGroup ) {
var $buttonGroup = $( buttonGroup );
$buttonGroup.on( 'click', 'button', function() {
$buttonGroup.find('.is-checked').removeClass('is-checked');
$( this ).addClass('is-checked');
});
});
Sorry, but it was much easier to write a new filter, than to troubleshoot one.
I changed the HTML (added a data-filtertype attribute), and wrote a jQuery click function that callst the filter (filterByType).
Much simpler code, less places to err. If the only thing your wanted was simply filtering the images, then this will do. (This is why it's suggested to write out what you want as a result - the way to that result is not always what you had in mind.)
$(".button").on('click', function(e) {
e.preventDefault();
filterByType($(this).attr('data-filtertype'))
})
function filterByType(type) {
$('.work-images').each(function(i, e) {
if (type !== 'all' && !$(e).hasClass(type)) {
$(e).hide()
} else {
$(e).show()
}
})
}
* {
box-sizing: border-box;
}
.button {
display: inline-block;
padding: 10px 40px;
background: #FF6760;
border: none;
font-weight: bold;
color: #fff;
font-family: lato_lightregular;
font-size: 14px;
cursor: pointer;
text-transform: uppercase;
}
.button:hover {
background: #616161;
color: #fff;
}
.button:active,
.button.is-checked {
background-color: #616161;
outline: 0;
}
.button.is-checked {
color: white;
text-shadow: 0 -1px hsla(0, 0%, 0%, 0.8);
}
.button:active {
box-shadow: inset 0 1px 10px hsla(0, 0%, 0%, 0.8);
}
.button-group {
margin-bottom: 20px;
}
.button-group:after {
content: '';
display: block;
clear: both;
}
.button-group .button {
border-radius: 5px;
margin-left: 0;
margin-right: 8px;
}
.grid {
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
margin: 10px -16px;
}
/* clear fix */
.grid:after {
content: '';
display: block;
clear: both;
}
.work-images {
-ms-flex: 25%;
flex: 25%;
max-width: 25%;
border: 11px solid #fff;
}
.work-images img {
vertical-align: middle;
width: 100%;
height: 210px;
}
.work-images img:hover {
filter: none;
}
.close {
text-decoration: none;
float: right;
font-size: 24px;
font-weight: bold;
color: white;
}
.content {
display: inline-block;
}
.show {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="portfoliosection">
<div class="container">
<div class="myworks text-center">
<h2>Our Featured Works</h2>
<p>Curabitur blandit tempus porttitor. Maecenas faucibus mollis interdum.</p>
<div class="work-filter">
<div id="filters" class="button-group">
<button class="button is-checked" data-filtertype="all"> All</button>
<button class="button" data-filtertype="web"> Web Design</button>
<button class="button" data-filtertype="mob"> Mobile Design</button>
<button class="button" data-filtertype="photo"> Photography</button>
</div>
<div class="grid">
<div class="work-images web">
<img src="https://i.imgur.com/FZED8Yb.jpg" onclick="onClick(this)" alt="Web Design">
</div>
<div class="work-images mob">
<img src="https://i.imgur.com/Jzts3Bm.jpg" onclick="onClick(this)" alt="Mobile Design">
</div>
<div class="work-images photo">
<img src="https://i.imgur.com/IC4tsi0.jpg" onclick="onClick(this)" alt="Photography">
</div>
<div class="work-images mob">
<img src="https://i.imgur.com/1x1X5S6.jpg" onclick="onClick(this)" alt="Mobile Design">
</div>
<div class="work-images web">
<img src="https://i.imgur.com/RckvhDi.jpg" onclick="onClick(this)" alt="Web Design">
</div>
<div class="work-images photo">
<img src="https://i.imgur.com/zjteEnm.jpg" onclick="onClick(this)" alt="Photography">
</div>
<div class="work-images web">
<img src="https://i.imgur.com/i34YqDD.jpg" onclick="onClick(this)" alt="Web Design">
</div>
<div class="work-images photo">
<img src="https://i.imgur.com/2qwcGam.jpg" onclick="onClick(this)" alt="Photography">
</div>
<div class="work-images mob">
<img src="https://i.imgur.com/CoEb43e.jpg" onclick="onClick(this)" alt="Mobile Design">
</div>
<div class="work-images photo">
<img src="https://i.imgur.com/EemBYgT.jpg" onclick="onClick(this)" alt="Photography">
</div>
<div class="work-images web">
<img src="https://i.imgur.com/RdDfJUp.jpg" onclick="onClick(this)" alt="Web Design">
</div>
<div class="work-images photo">
<img src="https://i.imgur.com/FeKM3fp.jpg" onclick="onClick(this)" alt="Photography">
</div>
</div>
</div>
</div>
</div>
</section>
It seems you have forgotten display: none; in .work-images.
And for the is-checked class. Simply use the w3school code if you do not want to use jQuery.
var btnContainer = document.getElementById("filters");
var btns = btnContainer.getElementsByClassName("button");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("is-checked");
current[0].className = current[0].className.replace(" is-checked", "");
this.className += " is-checked";
});
}
I have three tabs which add or remove active class on anchor tag not li when click on next button.
I tried that when I click on next button its disabled for some seconds then move to next tab button but issue is that moving is not working.
Here is code of html
<ul class="nav nav-pills" style="width: 1050px">
<li class="nav-item"><a class="active" href="#Race" data-toggle="tab">Race</a>
</li>
<li class="nav-item"><a class="active" href="#Ace" data-toggle="tab">Ace</a>
</li>
<li class="nav-item"><a class="active" href="#Deep" data-toggle="tab">Deep</a>
</li>
</ul>
<div class="tab-content">
<!-- Race -->
<div class="tab-pane active" id="Race">
<button class="btn-style" type="submit">Next</button>
</div>
<!-- ACE -->
<div class="tab-pane" id="Ace">
<button class="btn-style" type="submit">Next</button>
</div>
and jquery code
$(".btn-style").click(function () {
$(".btn-style").prop("disabled", true );
setTimeout(function() {
$(".btn-style").prop("disabled", false );
var target = $(".nav-pills li.nav-item a.active");
var sibbling;
if ($(this).text() === "Next") {
sibbling = target.next();
} else {
//sibbling = target.prev();
}
if (sibbling.is("li")) {
target.removeClass("active")
sibbling.addClass("active").children("a").tab("show");
}
}, 3000);
});
Target will be an array since there is more than 1 anchor with an active class.
You can try something easier like this CodePen
<style>
/* Checked Tabs */
.tabs-checked input:nth-of-type(1):checked ~ .tab:nth-of-type(1),
.tabs-checked input:nth-of-type(2):checked ~ .tab:nth-of-type(2),
.tabs-checked input:nth-of-type(3):checked ~ .tab:nth-of-type(3)
{
display: block;
}
.tab-area {
width: 21%;
margin: 2%;
}
input {
display: none;
}
.tab {
clear: both;
background: #ddd;
padding: 25px;
display: none;
height: 180px;
border: 1px solid #bbb;
}
.tab-link:focus,
.tab-link:hover,
#tab-C:checked ~ label:nth-of-type(3),
#tab-B:checked ~ label:nth-of-type(2),
#tab-B:not(:checked) ~ #tab-C:not(:checked) ~ label:nth-of-type(1) {
background: #ddd;
}
.tab-link:focus:after,
.tab-link:hover:after,
#tab-C:checked ~ label:nth-of-type(3):after,
#tab-B:checked ~ label:nth-of-type(2):after,
#tab-B:not(:checked) ~ #tab-C:not(:checked) ~ label:nth-of-type(1):after {
position: absolute;
content: "";
margin: 5px 0 0 0;
width: 55px;
height: 1px;
display: block;
background: #ddd;
}
.tab-link {
text-transform: uppercase;
font-size: 10px;
cursor: pointer;
color: #555;
font-weight: bold;
text-decoration: none;
display: block;
float: left;
width: 55px;
padding: 5px 0;
text-align: center;
background: #ccc;
border: 1px solid #bbb;
border-left: 0;
border-bottom: 0;
}
.tab-link:hover {
background: #eee;
color: #666;
}
.tab-link:nth-of-type(1) {
border-left: 1px solid #bbb;
}
</style>
<form class="tab-area tabs-checked" name="temp">
<input checked type="radio" name="tab" id="tab-A" value="1"/>
<input type="radio" name="tab" id="tab-B" value="2"/>
<input type="radio" name="tab" id="tab-C" value="3"/>
<label class="tab-link" for="tab-A">Tab 1</label>
<label class="tab-link" for="tab-B">Tab 2</label>
<label class="tab-link" for="tab-C">Tab 3</label>
<article class="tab">
<h3>I love :checked. :checked is great. If only I could toggle any item and not just input elements we'd be golden.</h3>
</article>
<article class="tab">
<h3>Tab 2</h3>
<p>Lorem Ipsum Dolor Sit</p>
</article>
<article class="tab">
<h3>Tab 3</h3>
<p>Lorem Ipsum Dolor Sit</p>
</article>
<h2 class="title">Checked Tabs</h2>
<p><strong>Independent, Default Selected</strong></p>
<p>Works In: Chrome(latest), Opera(Latest), FireFox(Latest), IE9</p>
</form>
<div >
<button onclick="doNext();" id="btn">NEXT</button>
</div>
<script>
function doNext()
{
btn.disabled = true;
setTimeout(function()
{
document.temp.tab.value = document.temp.tab.value % 3 + 1;
btn.disabled = false;
},3000)
}
</script>
So I'm trying to create multiple slideshows on the same page and I managed to obtain this code by now:
HTML
<div class="portfolio-slideshow">
<a class="prev" onclick="slide(0,-1)">❮</a>
<div class="slide"> <img src="img/dailyui/008.png" class="slideimg_0" id="0"> </div>
<div class="slide"> <img src="img/dailyui/007.jpg" class="slideimg_0" id="1"> </div>
<div class="slide"> <img src="img/dailyui/006.jpg" class="slideimg_0" id="2"> </div>
<div class="slide"> <img src="img/dailyui/003.jpg" class="slideimg_0" id="3"> </div>
<a class="next" onclick="slide(0,1)">❯</a>
</div>
<div class="portfolio-slideshow">
<a class="prev" onclick="slide(1,-1)">❮</a>
<div class="slide"> <img src="img/dailyui/008.png" class="slideimg_1" id="0"> </div>
<div class="slide"> <img src="img/dailyui/007.jpg" class="slideimg_1" id="1"> </div>
<div class="slide"> <img src="img/dailyui/006.jpg" class="slideimg_1" id="2"> </div>
<div class="slide"> <img src="img/dailyui/003.jpg" class="slideimg_1" id="3"> </div>
<a class="next" onclick="slide(1,1)">❯</a>
</div>
Also my body:
<body onLoad="showSlides()">
SCSS
.portfolio-slideshow{
width: 30%;
display: flex;
justify-content: space-between;
align-items: center;
}
.slide{
display: none;
padding:0 35px;
img{
max-height:40vh;
max-width:100%;
border-radius:5px;
-webkit-filter: drop-shadow(5px 5px 5px #000);
filter: drop-shadow(0px 2px 5px #222);
cursor: zoom-in;
display: none;
}
}
.prev,.next{
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.2s ease;
opacity: 0.25;
&:hover{ opacity: 1; }
}
Javascript
var slideIndex = [0,0]
function showSlides(){
var aux = slideIndex.length;
var i;
for(i=0; i<aux; i++){
slide(i,0);
}
}
function slide(n,m){
var i;
var aux = document.getElementsByClassName("slideimg_" + n);
var aux2 = aux.length - 1;
slideIndex[n] += m;
if (slideIndex[n] < 0) slideIndex = aux2;
else if (slideIndex[n] > aux2) slideIndex = 0;
for(i=0; i<aux.length; i++){ aux[i].style.display = "none"; }
aux[slideIndex[n]].style.display = "block";
}
Another thing to mentions is that when I click next/previous buttons I get this:
Uncaught TypeError: Cannot read property 'style' of undefined
at slide (websitejs2.js:17)
at HTMLAnchorElement.onclick (index2.html:87)
So, can any of you please help me getting things done?
Go through comments in code. add Comment if some doubt
var slideIndex = [0, 0]
function showSlides() {
var aux = slideIndex.length;
var i;
for (i = 0; i < aux; i++) {
slide(i, 0);
}
}
function slide(n, m) {
var i;
var aux = document.getElementsByClassName("slideimg_" + n);
var aux2 = aux.length - 1;
/*added logic */
if (slideIndex[n] == 3 && m == 1) {
slideIndex[n] = -1
}
if (slideIndex[n] == 0 && m == -1) {
slideIndex[n] = 4
}
/*added logic end*/
slideIndex[n] += m;
if (slideIndex[n] < 0) slideIndex = aux2;
else if (slideIndex[n] > aux2) slideIndex = 0;
for (i = 0; i < aux.length; i++) {
aux[i].style.display = "none";
}
aux[slideIndex[n]].style.display = "block";
}
.portfolio-slideshow {
width: 30%;
display: flex;
justify-content: space-between;
align-items: center;
}
.slide {
display: block;
/*changed to block*/
padding: 0 35px;
img {
max-height: 40vh;
max-width: 100%;
border-radius: 5px;
-webkit-filter: drop-shadow(5px 5px 5px #000);
filter: drop-shadow(0px 2px 5px #222);
cursor: zoom-in;
display: none;
}
}
.prev,
.next {
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
color: black;
/*changed to black*/
font-weight: bold;
font-size: 18px;
transition: 0.2s ease;
opacity: 1;
/*changed to 1*/
&:hover {
opacity: 1;
}
}
/*added*/
img {
height: 100px;
width: 100px;
display: none;
}
<body onLoad="showSlides()">
<div class="portfolio-slideshow">
<a class="prev" onclick="slide(0,-1)">❮</a>
<div class="slide"> <img src="https://static.pexels.com/photos/158507/marigold-calendula-orange-blossom-158507.jpeg" class="slideimg_0" id="0"> </div>
<div class="slide"> <img src="https://static.pexels.com/photos/59836/snowflake-flower-blossom-bloom-59836.jpeg" class="slideimg_0" id="1"> </div>
<div class="slide"> <img src="https://static.pexels.com/photos/93013/pexels-photo-93013.jpeg" class="slideimg_0" id="2"> </div>
<div class="slide"> <img src="https://static.pexels.com/photos/101538/dandelion-seeds-nature-spring-101538.jpeg" class="slideimg_0" id="3"> </div>
<a class="next" onclick="slide(0,1)">❯</a>
</div>
<div class="portfolio-slideshow">
<a class="prev" onclick="slide(1,-1)">❮</a>
<div class="slide"> <img src="https://static.pexels.com/photos/158507/marigold-calendula-orange-blossom-158507.jpeg" class="slideimg_1" id="0"> </div>
<div class="slide"> <img src="https://static.pexels.com/photos/59836/snowflake-flower-blossom-bloom-59836.jpeg" class="slideimg_1" id="1"> </div>
<div class="slide"> <img src="https://static.pexels.com/photos/93013/pexels-photo-93013.jpeg" class="slideimg_1" id="2"> </div>
<div class="slide"> <img src="https://static.pexels.com/photos/101538/dandelion-seeds-nature-spring-101538.jpeg" class="slideimg_1" id="3"> </div>
<a class="next" onclick="slide(1,1)">❯</a>
</div>
</body>