I'm doing some testing with the product page. I want to when hover each product and change style with the JavaScript.
In my research instead of adding them 1 by 1, I found For Loop
method is the fastest way and easier to add more products in the future.
Here's my snippet:
var pistiProductA = document.getElementsByClassName('pisti-product-a');
var pistiProductItem = document.getElementsByClassName('pisti-product-item');
var pistiProductItemLabel = document.getElementsByClassName('pisti-product-item-label');
for (i = 0; i < pistiProductA.length; i++) {
pistiProductA[i].addEventListener("mouseover", function() {
mouseOver(pistiProductA[i]);
});
pistiProductA[i].addEventListener("mouseout", function() {
mouseOut(pistiProductA[i]);
});
}
function mouseOver() {
pistiProductItem[0].style.transition = '0.3s all ease';
pistiProductItemLabel[0].style.transition = '0.3s all ease';
pistiProductItem[0].style.backgroundColor = '#888888';
pistiProductItemLabel[0].style.color = '#f7cc1b';
pistiProductItemLabel[0].style.fontWeight = '600';
}
function mouseOut() {
pistiProductItem[0].style.transition = '0.3s all ease';
pistiProductItemLabel[0].style.transition = '0.3s all ease';
pistiProductItem[0].style.backgroundColor = '#111111';
pistiProductItemLabel[0].style.color = '#000000';
pistiProductItemLabel[0].style.fontWeight = '400';
}
/* CSS used here will be applied after bootstrap.css */
.pisti-product-item {
border-radius: 6pt;
background-color: #111111;
}
.pisti-product-item-label {
color: #000;
font-size: 14pt;
font-weight:400;
}
.pisti-product-a:hover {
text-decoration: none;
}
<div class="container">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<div class="row pisti-products-wrapper">
<div class="col text-center pl-0">
<a class="pisti-product-a" href="#">
<img class="pisti-product-item img-fluid mb-2" width="140px" height="140px" src="https://media.gogoprint.sg/catalog/category/business-card_blue_1.png" alt="">
<p class="pisti-product-item-label">Sticker<i class="ml-3"></i></p>
</a>
</div>
<div class="col text-center pl-0">
<a class="pisti-product-a" href="#">
<img class="pisti-product-item img-fluid mb-2" width="140px" height="140px" src="https://media.gogoprint.sg/catalog/category/business-card_blue_1.png" alt="">
<p class="pisti-product-item-label">Sticker<i class="ml-3"></i></p>
</a>
</div>
<div class="col text-center pl-0">
<a class="pisti-product-a" href="#">
<img class="pisti-product-item img-fluid mb-2" width="140px" height="140px" src="https://media.gogoprint.sg/catalog/category/business-card_blue_1.png" alt="">
<p class="pisti-product-item-label">Sticker<i class="ml-3"></i></p>
</a>
</div>
<div class="col text-center pl-0">
<a class="pisti-product-a" href="#">
<img class="pisti-product-item img-fluid mb-2" width="140px" height="140px" src="https://media.gogoprint.sg/catalog/category/business-card_blue_1.png" alt="">
<p class="pisti-product-item-label">Sticker<i class="ml-3"></i></p>
</a>
</div>
<div class="col text-center pl-0">
<a class="pisti-product-a" href="#">
<img class="pisti-product-item img-fluid mb-2" width="140px" height="140px" src="https://media.gogoprint.sg/catalog/category/business-card_blue_1.png" alt="">
<p class="pisti-product-item-label">Sticker<i class="ml-3"></i></p>
</a>
</div>
<div class="col text-center pl-0">
<a class="pisti-product-a" href="#">
<img class="pisti-product-item img-fluid mb-2" width="140px" height="140px" src="https://media.gogoprint.sg/catalog/category/business-card_blue_1.png" alt="">
<p class="pisti-product-item-label">Sticker<i class="ml-3"></i></p>
</a>
</div>
<div class="col text-center pl-0">
<a class="pisti-product-a" href="#">
<img class="pisti-product-item img-fluid mb-2" width="140px" height="140px" src="https://media.gogoprint.sg/catalog/category/business-card_blue_1.png" alt="">
<p class="pisti-product-item-label">Sticker<i class="ml-3"></i></p>
</a>
</div>
</div>
</div>
I have tried to replace the array [0]
into [i]
. But it looks like not working. I also did a console.log(i);
in my function
. But it seems like the i
is undefined
.