Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 49 additions & 15 deletions assets/js/secondary-product-image-for-woocommerce.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
// Access the product list
var productList = document.querySelector('.products');

if( ! productList ) {
productList = document.querySelector('.products-block-post-template');
if (!productList) {
productList = document.querySelector('.products-block-post-template');
}

// Attach mouseover and mouseout event listeners to each product item
if ( productList ) {
productList.addEventListener( 'mouseover', function (event) {

var target = event.target;
var parent = target.parentNode;
var secondaryWrapper = parent.querySelector('.wpzoom-secondary-image-container');

// Add the class to show the .wpzoom-secondary-image-container with animation
if ( secondaryWrapper ) secondaryWrapper.classList.add('show-secondary-image');

});
}
// Function to show the secondary image
function showSecondaryImage(event) {
var target = event.target;
var parent = target.closest('.product-item'); // Make sure to select the closest product-item
var secondaryWrapper = parent ? parent.querySelector('.wpzoom-secondary-image-container') : null;

// Add the class to show the .wpzoom-secondary-image-container with animation
if (secondaryWrapper) {
secondaryWrapper.classList.add('show-secondary-image');
}
}

// Function to hide the secondary image
function hideSecondaryImage(event) {
var target = event.target;
var parent = target.closest('.product-item'); // Make sure to select the closest product-item
var secondaryWrapper = parent ? parent.querySelector('.wpzoom-secondary-image-container') : null;

// Remove the class to hide the .wpzoom-secondary-image-container
if (secondaryWrapper) {
secondaryWrapper.classList.remove('show-secondary-image');
}
}

// Attach event listeners
if (productList) {
// Desktop events
productList.addEventListener('mouseover', showSecondaryImage);
productList.addEventListener('mouseout', hideSecondaryImage);

// Mobile events
productList.addEventListener('touchstart', function(event) {
showSecondaryImage(event);
});

// Hide the secondary image after touchend
productList.addEventListener('touchend', function(event) {
hideSecondaryImage(event);

// Allow the link to be followed after touch
var linkElement = event.target.closest('a');
if (linkElement) {
// Programmatically trigger the click after touchend to follow the link
linkElement.click();
}
});
}