Switching the Background Image with ACF on Mouseover

I've been wracking my brain trying to find a solution to this issue for quite some time now, but so far I haven't had any luck.

My objective is to change the background image of a specific div when it's hovered over to a darker version of the original image. I initially tried using the filter property, but it ended up affecting all the nested divs with content inside (via JavaScript).

This task is being carried out within a repeater field using ACF. The background-image is selected through a sub-field within the repeater.

I've made attempts with JavaScript, but unfortunately, I haven't found a successful solution yet.

I also considered replacing the divs, which is why you see a hover-div implemented. However, that method didn't yield any positive results.

You can review my code below for further reference.

Please feel free to provide any assistance or guidance!

<div class="member-wrap grid-x grid-margin-x grid-margin-y">
            <?php if ( have_rows( 'branch_team_members' ) ) : ?>
                <?php while ( have_rows( 'branch_team_members' ) ) : the_row(); ?>
                    <div class="branch-member-main small-12 medium-6 large-3 cell">
                        <?php $team_member_hover_image = get_sub_field( 'team_member_hover_image' ); ?>
                        <div class="main-member-hover" style="background-image: url('<?php echo $team_member_hover_image['url']; ?>');">    
                        </div>
                        <?php $team_member_image = get_sub_field( 'team_member_image' ); ?>
                        <div class="main-member-area" style="background-image: url('<?php echo $team_member_image['url']; ?>');">
                            <h4 class="member-name-hover"><?php the_sub_field( 'team_member_name' ); ?></h4>
                            <span class="member-position-hover"><?php the_sub_field( 'team_member_position' ); ?></span>        
                            <?php the_sub_field( 'team_member_bio' ); ?>
                        </div>
                        <div class="main-member-contact">
                            <span class="member-name"><?php the_sub_field( 'team_member_name' ); ?></span>
                            <span class="member-contact"><?php the_sub_field( 'team_member_contact' ); ?></span>
                        </div>                      
                    </div>
                <?php endwhile; ?>
            <?php else : ?>
                <?php // no rows found ?>
            <?php endif; ?>
        </div>

Answer №1

I haven't fully debugged this yet, but it could provide some guidance on how to approach the task at hand.

The following method will generate a block for each box along with a counter.

<div class="member-container grid-x grid-margin-x grid-margin-y">
        <?php if ( have_rows( 'branch_team_members' ) ) : ?>
            <?php $counter; ?>
            <?php while ( have_rows( 'branch_team_members' ) ) : the_row(); ?>
                <?php $team_member_hover_image = get_sub_field( 'team_member_hover_image' ); ?>
                <?php $counter++; ?>

                <style type="text/css">
                    .blockNum-<?php echo $counter; ?>{
                        background-image: url('<?php echo $team_member_image['url']; ?>');
                    }
                    .blockNum-<?php echo $counter; ?>:hover{
                        background-image: url('<?php echo $team_member_hover_image['url']; ?>');
                    }

                </style>

                <div class="branch-member primary-12 middle-6 large-3 cell">


                    <?php $team_member_image = get_sub_field( 'team_member_image' ); ?>
                    <div class="main-member-section blockNum-<?php echo $counter; ?>">
                        <h4 class="member-name-hover"><?php the_sub_field( 'team_member_name' ); ?></h4>
                        <span class="member-position-hover"><?php the_sub_field( 'team_member_position' ); ?></span>        
                        <?php the_sub_field( 'team_member_bio' ); ?>
                    </div>
                    <div class="main-member-contact">
                        <span class="member-name"><?php the_sub_field( 'team_member_name' ); ?></span>
                        <span class="member-contact"><?php the_sub_field( 'team_member_contact' ); ?></span>
                    </div>                      
                </div>
            <?php endwhile; ?>
        <?php else : ?>
            <?php // No rows were found ?>
        <?php endif; ?>
    </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging three.js for visualizing temporal data series

What is the best approach for using time series data to control the animation of a three.js scene? For instance: Time | ObjA(x,y,z) | ObjB(x,y,z) | ... 00:00:00 | 0,9,0 | 1,1,1 | ... 00:00:10 | 0.1,0,0.1 | 1,0.5,1 | ... 00:0 ...

Display modal after drop-down selection, triggered by API response

Currently, I am working on integrating an API to enable users to make payments through a modal. Users should be able to enter an amount and select a payment frequency. I have successfully extracted various payment frequencies from the API response and pop ...

What is the best way to keep vnodes saved in the browser indefinitely?

Is there a way to redirect pages in Vue.js without refreshing the page while also keeping the component alive? I've tried using JSON.stringify(vnode) to store the vnode but I keep getting a TypeError: Converting circular structure to JSON error. TypeE ...

The error is popping up as I try to deploy my Next.js application on Vercel

warning " > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d0f181c1e095009120d5011121c1914131a501f1c0f3d4f534c534d">[email protected]</a>" has an incorrect peer dependency "react@^16 || ^17" ...

tips for utilizing namespaced getter filtering within a Vuex module in vueJs

In my custom module named ShopItemCategory, I have a Getter getters: { shopItemsCategories: state => state.ShopItemsCategories.data, }, Inside the component, there is a computed function I defined computed: { shopItemsCategories ...

Guide to automatically loading a default child route in Angular 1.5 using ui-router

Hello, I am looking to set a default child route to load as soon as the page loads. Below is the code snippet: $stateProvider.state('userlist', { url: '/users', component: 'users', data:{"name":"abhi"}, resolv ...

Is there a way for my HTML file to connect with my CSS and JavaScript files stored in an Amazon S3 Bucket?

My current project involves hosting an HTML file as a website on AWS S3 in order to circumvent the CORS Policy. However, when I uploaded all my files into a new bucket, the HTML file was able to open but without accessing the accompanying CSS and JavaScrip ...

Having trouble locating an external Javascript file in a Node.JS/Express app with Jade template?

In my Node.JS/Express app, I am using the Jade template engine. The issue arises when trying to reference a server-side Javascript file named common_routines. Despite placing the Javascript file in the directory above my views directory and referencing it ...

Deleting object property within Angular ng-repeat loop

I have information structured like this: $scope.cart={"4": {"cost": 802.85, "description": "test", "qty": 1}, "5": {"cost": 802.85, "description": "test", "qty": 1}}; My goal is to iterate through this data and show it with a remove button. How can I s ...

Aggregating quantities of various items using Redux

I'm in the process of setting up a shopping cart using Redux. Essentially, my React code retrieves product details from a database and displays them for the user. Below is a snippet of my App.js: function GetProductsHtml() { initProducts() con ...

My script for retrieving the playlist is experiencing difficulties in calling upon the "btoa" and "item.slice" methods

Having trouble using the "btoa" method to encode the client id and client secret in base64, which are retrieved from the ".env" file. I've also attempted using the Buffer method but encountered an "invalid form" error with Buffer. Any assistance woul ...

Hassle with iOS Phonegap selecting multiple lines of text?

For my Phonegap app, I need users to be able to select and modify text inside a p element. The issue arises when trying to select text within the p tag. The text is fetched from my server and displayed as follows: <p style="white-space:pre-wrap">Lo ...

How can you select all elements within a class except for the following element?

Here is my jQuery code snippet: $(function(){ $(".details").hide(); $("li").mouseenter(function(){ $(this).css("color","blue"); }); $("li").mouseleave(function(){ $(this).css("color","black"); }); $("li").click(func ...

What is the process for subscribing to setInterval() in an Angular application?

I'm currently working on developing an iOS location tracking application using the Ionic 5 Framework, Angular, and the Cordova Geolocation Plugin. In the past, I was able to track user location changes using the watchPosition() function, which worked ...

Tips for rearranging table columns using drag and drop in react js

I have been experimenting with various libraries to create a drag-and-drop feature for changing table columns order. Here is my current implementation: import React, { useState } from 'react'; import './list_de_tournees.css' const Table ...

After running the map.fitBounds function, you can retrieve the current boundary coordinates using map.getBounds() in

After calling fitBounds() to recenter and zoom the map to fit the bounds, I expected getBounds() to return a valid bound. However, it is returning nil. Below is the code snippet that reproduces this issue: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...

Adding Urls in a specific format using JavaScript: A Comprehensive Guide

I am looking to insert variable data in JavaScript in the given code within the URL $(document).on('change','.sort_rang',function(){ var url = "ajax_search.php"; //console.log($("#search_form").serialize()); var data = $("#sea ...

What is causing Puppeteer to incorrectly capture this page when attempting to take a screenshot?

Utilizing the Node.JS library puppeteer, this code captures a screenshot of a webpage and saves it as a png image: #!/usr/bin/env node const puppeteer = require("puppeteer"); (async () => { const browser = await puppeteer.launch({args: ['--no- ...

Tips for placing a div at the bottom of a parent td element

In my project, I am facing a challenge in placing a <div> with a fixed height of 100px and full width (100% of the parent <td>) at the bottom of a <td>. The issue is that the content of the other <td>s may be larger than the browser ...

The art of controlling iframe elements with jquery

I've been researching various topics related to this issue, but I'm still unable to achieve the desired outcome. Currently, I am embedding an iframe within an HTML document like so: <iframe class="full-screen-preview__frame" id="nitseditpre ...