Image transformed by hovering effect

I've been attempting to add a hover effect to the images in my WordPress theme. The images are displayed in a grid format, created by the featured image on the posts.

The grid layout is controlled within content.php

<?php
/**
 * controls main grid images
 */
?>

<article id="post-<?php the_ID(); ?>" <?php post_class('col-md-4 col-sm-4 pbox '); ?>>

    <div class = "box-ovrly">
      <h2 class="box-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
      <div class="box-meta"><?php the_category(', '); ?></div>
    </div>

    <?php
        $thumb = get_post_thumbnail_id();
        $img_url = wp_get_attachment_url( $thumb,'full' ); //get full URL to image (use "large" or "medium" if the images too big)
        $image = aq_resize( $img_url, 750, 560, true ); //resize & crop the image
    ?>

    <?php if($image) : ?>
    <a href="<?php the_permalink(); ?>"> <img class="img-responsive hover-decal" src="<?php echo $image ?>"/></a>
    <?php endif; ?> 

</article><!-- #post-## -->

I am hoping to transform the 'box-overly' div into an overlay that appears on top of the image when hovered over, but I'm struggling to find the appropriate JavaScript and CSS.

Here is the current CSS code...

.box-ovrly {
z-index: 100;
position: absolute;
top: 0px
left: 0px;
}

Currently, this places the title and category on top of the image. However, my goal is to have a background cover the image on hover without displaying any text until it's hovered over.

An example of the desired effect (eventually with different background colors, etc.) can be seen on this portfolio page:

Answer №1

To create this effect, utilize only CSS.

For a visual demonstration, visit JSFiddle - http://jsfiddle.net/y3z4pojv/6/

In the provided code snippet, pay close attention to the

.element:hover > .elementHover
portion. This states "show .elementHover, which is nested under .element, WHEN .element is hovered over".

Observe how the HTML elements are structured in the example and replicate it within your WordPress code (modifying element names as needed).

CSS for Testing:

.element {
    position: relative;
    display: inline-block;
    z-index: 0;
    width: 300px;
    height: 300px;
    overflow: hidden;
}
.strawberries {
    background: url('http://images4.fanpop.com/image/photos/16300000/Delicious-pretty-fruit-fruit-16382128-670-562.jpg');
}
.watermelon {
    background: url('http://fyi.uwex.edu/safepreserving/files/2013/08/watermelon.jpg');
}
.pineapple {
    background: url('http://static.giantbomb.com/uploads/original/7/71129/2672509-6564604021-pinea.jpg');
}
.element:hover > .elementHover {
    opacity: 1;
    z-index: 1;
}
.elementHover {
    position: absolute;
    opacity: 0;
    z-index: -1;
    height: 100%;
    width: 100%;
    -webkit-transition: all 500ms;
    -moz-transition: all 500ms;
    transition: all 500ms;
}
.hoverContent {
    position: relative;
    z-index: 10;
    color: #fff;
    font-size: 200%;
    padding: 1em;
}
.hoverBackground {
    position: absolute;
    z-index: 5;
    width: 100%;
    height: 100%;
    opacity: 0.8;
}
.green {
    background: green;
}
.red {
    background: red;
}

Sample HTML Code:

<div class="element strawberries">
    <div class="elementHover">
        <img class="hoverBackground" src="http://www.aux.tv/wp-content/uploads/2012/09/how-to-rick-roll-somebody.jpg"/>
        <span class="hoverContent">
            Hello there
        </span>
    </div>
</div>
<div class="element watermelon">
    <div class="elementHover">
        <div class="hoverBackground green"></div>
        <span class="hoverContent">
            Goodbye now
        </span>
    </div>
</div>
<div class="element pineapple">
    <div class="elementHover">
        <div class="hoverBackground red"></div>
        <span class="hoverContent">
            ...Not yet
        </span>
    </div>
</div>

It's worth noting that you can replace the IMG tag with a colored DIV during hover effects by altering the element content within .hoverBackground.

Answer №2

I had a similar task to tackle, but I hope this solution will work for you. Feel free to make adjustments as needed for your specific use case. Here is a simple version:

Version 1 (html, JS and CSS included in the link, JS provided below): http://jsfiddle.net/SnvMr/6/

$('.effect').hover(
    function() {
        $('.effect').not(this).stop().fadeTo(500, 0.33);
        $(this).find('p').fadeIn(500);
    }, function() {
        $('.effect').stop().fadeTo(500, 1);
        $('.effect p').fadeOut(500);
    }
);

Version 2: http://jsfiddle.net/deelite/SnvMr/12/

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

The system encountered an error stating that the required component "Footer" in the main directory was not located in the specified node_modules

Issue I'm Facing: Error found in components/main/Footer within the file path: ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/App.vue Components Tree as shown in the screenshot below: Cod ...

The jQuery AJAX post request is displaying an error message stating that the website xxx is not permitted by

I encountered a roadblock while attempting to use AJAX to call the eBay FindProducts API with a POST request. The error message that I got stuck on is as follows: XMLHttpRequest cannot load . Origin is not allowed by Access-Control-Allow-Origin. This ...

Ways to ensure the first div always expands when sorting in AngularJS

In my AngularJS application, I have a set of div elements generated using ng-repeat. The first div is always expanded by default upon loading the page. Now, when I click a button to sort the divs based on their ID in the JSON data, I want the top div to ...

How can we develop a strategy to select products based on specific features while keeping costs minimized?

I've got a collection of products with varying costs and features. Each product offers a unique set of features. I'm in need of an algorithm that, when given the specific features I desire, can recommend the most cost-effective combination of pr ...

Creating a Unique CSS Grid Border with Custom Images

I have a client project using nextjs and the design calls for a component with a custom border on a responsive CSS grid. I've successfully created the CSS grid with all the necessary content, but I'm struggling to add the required border as per t ...

Objects within the Prototype in Javascript

While delving into the world of AngularJS, I stumbled upon an interesting revelation - when objects are placed in a prototype object, any instances inheriting from that prototype will alter the prototype's objects upon assignment. For example: funct ...

Minimize the number of clicks needed to navigate using the HTML/JavaScript navigation

On my website, I have a navigation bar that changes the contents of a div when a user clicks on an item. However, if the user clicks multiple times in quick succession, the content changes too many times. I want to ensure that the content only changes once ...

Challenges encountered while using JQuery Validation for a form submission for the second time (JQuery Steps)

Currently, I am implementing JQuery Steps for the registration form on my website. The registration process involves three steps: Personal > Company > Confirm. I have configured the form to display error messages in the "placeholder" if a textbox is ...

Mastering parameter passing in Node.js functions: A comprehensive guide

As I embark on my journey with node js (refer to the question), please be patient as I navigate through this new territory. To clarify my query, I have developed a function to be invoked in another JS file: exports.test = function(req, res){ connection ...

What is the best way to eliminate the white margin that is showing up on the right side of my website?

Here's a question that has been bugging me recently... So, I've been working on creating my own version of an Airbnb website called 'tombnb' for a few weeks now. Everything was going smoothly until I encountered a persistent issue. Th ...

Mongoose Does Not Allow for Duplicate Data Submissions

I'm currently working on a project to develop a basic blog application. Everything works smoothly when submitting content to the database for the first time, but if you try to submit again, Node crashes unexpectedly. I've been struggling to pinpo ...

The data within my <ui:define name="content"> element is not displaying on the JSF view

I'm currently working on a project that involves JSF/Facelets and a Style Sheet for styling my JSF view. I'm trying to add some graphical components like "h:inputText" and "h:commandButton" tags to my view XHTML, but for some reason, they are not ...

How to implement various middlewares in Express.js depending on the current environment using NODE_ENV?

My dilemma involves utilizing two different middlewares based on NODE_ENV: router1 for production and router2 for testing and development environments. I'm currently using the following code snippet: if( process.env.NODE_ENV === 'prod' ) { ...

Utilizing Selenium automation to navigate a website that features a custom jQuery plugin-checkbox functionality

Utilizing Selenium WebDriver along with JavaScript to automate the testing of a website that features a custom jquery plugin named "jcf" (JavaScript Custom Forms) for aesthetically pleasing forms. You can find more information about this plugin here. I am ...

The functionality of triggers is not applicable to content that is dynamically loaded through ajax requests

I encountered a problem recently that required me to rewrite some functions. The original code looked like this: $('#type_of').on('change', function () { However, due to issues with manipulating the DOM and loading content via ajax, I ...

Having trouble with the option:selected function in jQuery 1.9?

Here is a query that works perfectly with versions of jQuery prior to 1.9: $("select option:contains(fish)").attr('selected', true); Unfortunately, this query does not function at all with jQuery 1.9 and newer versions. Despite searching for ...

Using React Native to iterate over a multidimensional array with the array map function

I want to iterate through a two-dimensional array like the one below: var array=[["1"],["3","8"],["4","8","3"],["4","8","3","9"],["1","8","3","9","2"],["6","8","3","9","2","1"],["4","8","3","9","2","11","2"]] Currently, this code only loops through the & ...

iOS is currently experiencing issues with touch events not functioning properly within iFrames

Events like touchstart or touchend do not work when attached to the window inside an IFrame on iOS devices. Check out this straightforward example: mainpage.html <!DOCTYPE HTML> <html style="height: 100%;"> <head> <meta charset= ...

Move a file from one directory to another directory that is two levels higher

Here is the script file path: C:\xampp\htdocs\wordpress\wp-content\plugins\test\test.php The goal is to move images from this location: C:\xampp\htdocs\wordpress\wp-content\uploads\2017&bs ...

Dealing with errors when chaining promises in a react-redux application

This is related to a question asked on Stack Overflow about Handling async errors in a react redux application In my react-redux setup, I am facing a scenario where I need to chain multiple API calls upon successful completion of each. How can I achieve ...