I have used some JavaScript on a previous site and would like to re-use the effect - the effect being a background image on a div changes when hovering over various links.
However this time, I want the images to be ACF fields. I have read through the JavaScript API documentation and other posts but I can't get it to work.
This is what I have. I think maybe I have correctly fetched the field but need to add the action of showing it on hover.
Each table cell on the left is an ACF field that consists of a title, description, link and image. I want the large image on the right to change to the ACF image applied to each cell when each one is hovered over.
I have included my attempts, though I have very limited php/js knowledge so it's likely not even close. I am also new to ACF.
Attempt 1
const defaultImg = "/wp-content/default-image.jpg";
const newImg = acf.getField('featured_item_one_image');
const newImgb = acf.getField('featured_item_two_image');
$('.feat-block-1')
.on('mouseover', function() {
$('.main-image').css('background-image', "url(" + newImg + ")");
})
$('.feat-block-2')
.on('mouseover', function() {
$('.main-image').css('background-image', "url(" + newImgb + ")");
})
Attempt 2
const newImg = acf.getField('field_5de5a95617941');
const newImgb = acf.getField('field_5de5abd20cb3f');
$('.feat-block-1')
.on('mouseover', function() {
$('.main-image').css('background-image', "url(" + newImg + ")");
})
$('.feat-block-2')
.on('mouseover', function() {
$('.main-image').css('background-image', "url(" + newImgb + ")");
})
And Also this without any of the 'const' values at the top
$('.feat-block-1')
.on('mouseover', function() {
$('.main-image').css('background-image', "acf.getField('field_5de5abd20cb3f')");
})
$('.feat-block-2')
.on('mouseover', function() {
$('.main-image').css('background-image', "acf.getField('field_5de5abd20cb3f')"); })
Any help would be much appreciated
presudo-selector
.feat-block-1:hover
– Ali Qorbani