Javascript and CSS combo for creating a stylish fade effect

Hey everyone, I have a design challenge where I need to change the color of a picture inside a box when the user hovers over it. I have four boxes like this and my goal is to make everything else fade out when a user hovers over a specific box. Anyone know how to achieve this effect using JavaScript or CSS?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="eng" lang="eng">
<head>
<script type="text/javascript" src="jquery-1.8.0.min.js"></script>
<style type="text/css">
.meh1
{
    height: 200px; /* Specify Height */
    width:  200px; /* Specify Width */
    border: 3px solid blue; /* Add 1px solid border, use any color you want */
    text-align: center; /* Align the text to the center */
}

.meh1:hover
{
    border: 3px solid orange;
}   

.meh2
{
    height: 200px; /* Specify Height */
    width:  200px; /* Specify Width */
    border: 3px solid blue; /* Add 1px solid border, use any color you want */
    text-align: center; /* Align the text to the center */
}

.meh2:hover
{
    border: 3px solid orange;
}
</style>

<script type="text/javascript">
var $mehs = $('.meh');
$mehs.hover(function(){
    $mehs.not(this).fadeTo(200, 0.25);
}, function(){
    $mehs.fadeTo(200, 1);
});
</script>

</head>
<body>

<div id="meh">

    <div class="meh1">
        <img src="cw3.jpg" alt="Name">
    </div>

    <div class="meh2">
       <img src="cw2.jpg" alt="Name">
    </div>
</div>

</body>
</html>

Answer №1

If you're working with Javascript, the simplest way to achieve this is by utilizing jQuery (which, based on your previous inquiries, I can tell you are already comfortable using).

Assuming all divs have the class meh:

var $mehs = $('.meh');
$mehs.hover(function(){
  $mehs.not(this).fadeTo(200, 0.25);
}, function(){
  $mehs.fadeTo(200, 1);
});

I opted for fadeTo() over fadeOut() since it maintains your layout (fadeOut() will change the element's display property to none;, impacting your page layout)

Answer №2

Here is an example:

$(document).ready(function() {
    $(".hover-effect").on('mouseover', function(){
        $("Element to be faded out").fadeOut('slow');
    });
}); 

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

Is there a way to access the Context in the _app.js file in Next.js with React?

Hey there! I'm currently working with a context provider file and _app.js file. I need to access AppContext in the _app.js file. Can anyone provide guidance on how to successfully access the AppContext within the _app.js file? I have imp ...

Is a Javascript-only application compatible with Amazon S3 cloud storage?

I am currently investigating the validity of the following statement: Based on my research, it seems unlikely to create a web application using only JavaScript - without any server-side logic - hosted on Amazon S3 that can also store data solely on S3 whi ...

Is there a way to align one <article> next to another <article> to create a horizontal layout?

Can anyone help me figure out how to display two <article>s on the same line or side-by-side, with the FirstArticle positioned to the left and the SecondArticle to the right? .MainContent { border-radius: 5px; -moz-border-radius: 5px; ...

Disable text input in Flatpickr and remove the default iPhone calendar

We are facing an issue with the iPhone and iPad when using flatpickr for our calendars. When the calendar is opened, the cursor automatically focuses on the text box, triggering the default iPhone calendar to appear below. This problem does not occur on An ...

Merge adjacent CSS blocks seamlessly without the need for JavaScript

I am facing an issue with styling div elements that have editable content through a WYSIWYG inline editor on my website. Users often do not understand the importance of enclosing similar blocks of code in containing DIVs for proper formatting, resulting in ...

Why isn't Meteor.call functioning in the stub?

As I delve into the realm of async JavaScript coding, I have encountered a gist that has left me puzzled: https://gist.github.com/dariocravero/3922137 Specifically within client_save.file.js - there are parts of this code snippet that baffle me: fileRead ...

Dynamic Searching in ASP.NET Core MVC Select Component

I need assistance with implementing a dynamic search feature on my Login page. Here's the scenario: When a user enters a username, let's say "Jh" for Jhon, I want to display a select list next to the login form that lists all the usernames from t ...

Angular 4's Panel Window: A User-Friendly Interface

Having experience with Adobe Flex, I am familiar with creating new panel windows in Action Script by using the 'new' keyword and popping them up using popups. The customization of these windows was achieved through functions provided in the Panel ...

When attempting to call Firebase Functions, ensure that Access-Control-Allow-Origin is set up correctly

Although it may seem straightforward, I am confused about how Firebase's functions are supposed to work. Is the main purpose of Firebase functions to enable me to execute functions on the server-side by making calls from client-side code? Whenever I t ...

update the markers on a leafletjs map

I am aiming to create a dynamic map with markers that update every 10 minutes. The marker positions are stored in a spreadsheet document that is regularly updated. After successfully retrieving the data from the spreadsheet and positioning the markers in ...

Rendering issue with component

I am encountered with a situation where one component is failing to render its sub-component. Surprisingly, there are no visible errors in the console. The data I'm expecting from the web call is also coming through without any issues. However, for so ...

Retrieve the file path of an uploaded image in a Razor view using jQuery Ajax

I have been facing challenges while working on an ASP.NET MVC3 application where I am developing a custom image gallery. The browser being targeted is IE9, which rules out the option of using HTML5 File API. All my code resides within a strongly typed Razo ...

Error in typescript: The property 'exact' is not found in the type 'IntrinsicAttributes & RouteProps'

While trying to set up private routing in Typescript, I encountered the following error. Can anyone provide assistance? Type '{ exact: true; render: (routerProps: RouterProps) => Element; }' is not compatible with type 'IntrinsicAttribu ...

Struggling to make jQuery code function properly in Wordpress, despite attempting to use noConflict

I have created a custom image grid gallery in WordPress using HTML and CSS, complete with popups and sliders. I had to code it myself because I couldn't find a suitable plugin that matched my design preferences. I have tested the functionality on my ...

How a JavaScript function handles the scope of a for loop index

Here's some Javascript code I'm working with: func1() { for(i = 2; i < 5; i ++) { console.log(i); func2(i); } } func2(x) { for(i = 100; i < 200; i ++) { do something } } I've noticed that when runni ...

Encountering a TypeError while trying to run Pythonshell on my Mac device

When I run a python script in node.js using python shell, it works perfectly on my Windows system. However, I encounter an error when trying to run the same thing on my Macbook: Error: TypeError: can't multiply sequence by non-int of type 'float ...

Is there a way to verify if a React hook can be executed without triggering any console errors?

Is there a way to verify if a React hook can be invoked without generating errors in the console? For example, if I attempt: useEffect(() => { try { useState(); } catch {} }) I still receive this error message: Warning: Do not call Hooks i ...

Achieving a single anchor link to smoothly scroll to two distinct sections within a single page using React

There is a sidebar section alongside a content section. The sidebar contains anchor links that, when clicked, make the corresponding content scroll to the top on the right-hand side. However, I also want the sidebar link that was clicked to scroll to the ...

Is there a way to create a new prettyphoto window by clicking on a link within the original prettyphoto window?

I have an HTML table that is dynamically constructed on the server side using AJAX. The table is displayed using prettyphoto, everything works fine up to this point. However, the last column of the table contains a link that should open an image using pret ...

The data in MongoDB is organized using unique identifiers called ObjectId

I am currently working on a MEAN Crud project and encountering some issues. Despite the data being received in the mongodb, it is displaying as an objectId instead of the actual content. You can see this issue in the image below: https://i.stack.imgur.com ...