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

Achieve a full-width span for a single item in a CSS grid without any alterations to the HTML code

How can I make the first item in this grid span 100% without changing the HTML structure? The first item is assigned an extra class ".sub" Is it possible to achieve this? Click here for more information <div class="fd-col fd-5col"> <div class= ...

Tips for eliminating extra blank space under the footer on an HTML website

Just beginning my journey with bootstrap and I am encountering an issue on my website where there is space after the footer when resizing to a mobile size. I've tried setting margin: 0; padding: 0; but it hasn't resolved the problem. Here's ...

Struggling with updating a user in MongoDB using findOneAndUpdate and encountering a frustrating internal server error 500?

Presently, I am in the process of developing an API that is designed to update the current user in mongoDB based on the data provided in the request. However, whenever I execute findOneAndUpdate(), I encounter a 500 internal server error. (I will outline ...

Error in Mongodb: Unable to convert value into ObjectId

Currently, I am attempting to retrieve only the posts belonging to users using the following code snippet: router.get("/:username", async (req, res) => { try { const user = await User.findOne({ username: req.params.username }); const ...

Adhesive Navigation Bar

Check out this link: JSFIDDLE $('.main-menu').addClass('fixed'); Why does the fixed element flicker when the fixed class is applied? ...

Assigning the href attribute dynamically

How can you dynamically set the href attribute of the <a> tag using jQuery? In addition, what is the method for retrieving the value of the href attribute from the <a> tag with jQuery? ...

Issue encountered: The differ cannot recognize the provided object '[object Object]', which is of type 'object'. NgFor is limited to binding Iterables only

I have been following a tutorial on Ionic created by Paul Halliday, focusing on a shopping list project that utilizes Firebase and Angular. However, I am encountering an error every time I try to run the application: Error: Uncaught (in promise): Error: ...

Combine activities from a dynamic array of Observables

I'm currently utilizing the rxjs library. In my application, I have a Browser object overseeing multiple instances of Page objects. Each page emits a stream of events through an Observable<Event>. Pages can be opened and closed dynamically, le ...

Ensuring that your content expands to fit the sidebar dimensions

I have diligently searched for a solution to my issue both on Google and here, but have yet to find one that works. I've experimented with various CSS properties like display: table-cell and inline-block, as well as different overflow options, but not ...

Struggling to make fadeIn() function properly in conjunction with addClass

I've been struggling with this issue for quite some time now and I just can't seem to make it work. The JavaScript I'm working on involves using addClass and removeClass to display and hide a submenu element. While the addclass and removeCla ...

"Troubleshooting the issue of AngularJS $http patch request failing to send

The information is successfully logged in the console when passed to replyMessage, but for some reason, the API does not seem to be receiving the data. Is the input field perhaps empty? replyMessage: function(data) { console.log(data); ...

Is there a way to have just the data table automatically update every 2 minutes without affecting the rest of the

I'm working on a project where I need to create an inbox mail that automatically refreshes every 2 minutes to display any new mail in the table. Can anyone help me with refreshing my datatable? ...

What could be the reason for Safari generating larger videos than Chrome when utilizing the MediaDevices.getUserMedia() API?

Embarking on a small experiment to gauge the size of captured videos using the MediaDevices.getUserMedia() API. My Safari implementation yielded videos 5-10 times larger than those in Chrome. Here's the code snippet: index.html: <html lang=" ...

What steps can I take to streamline and simplify this tab controller code?

I'm looking to simplify this jQuery code because I feel like there's repetition. As someone new to JavaScript and jQuery, I've created two tabs with their respective containers containing miscellaneous information. My goal is to have each co ...

unable to employ angular ui-sortable

I got the most recent source code from https://github.com/angular-ui/ui-sortable. However, I am facing issues in using it. The demo.html file seems to be broken. Update: Upon inspecting the console when opening demo.html: Navigated to https://www.google. ...

Tips for executing forEach on a promise?

I am currently working with a function that returns a promise of an array: async function functionReturningPromiseOfUserIDs(): Promise<string[]> My question is, can I use the forEach method on the array returned by this function? async function runF ...

Error encountered while attempting to retrieve an environment variable: Invalid token found

I am currently facing an issue while trying to add an environment variable inside the .env file in my Nuxt project. The version of Nuxt.js I am using is 2.15.3 Below is a snippet from my nuxt.config.js: export default { publicRuntimeConfig: { baseU ...

"Implementing responsive design with Bootstrap's carousel through media queries

I'm having some trouble creating a basic carousel using bootstrap because the images are not displaying full screen. Do I need to use media queries to achieve this and if so, how can I implement them? <div id="myCarousel" class="carousel slide" da ...

Issue: Anticipated the primary reducer to be a function, but was presented with: 'undefined' in Vanilla JS with Redux

Exploring the world of Redux, I decided to try out some code from a tutorial on YouTube. Building a simple Vanilla JS App, I integrated Redux into it. However, upon running the app in the terminal, an error message popped up saying: "Error: Expected the ro ...

Optimizing CSS usage within Django: top recommendations

Throughout a 6-month-long project, I have continuously added new URLs. However, I am now encountering an issue when using the extend 'base.html' function on other pages where the CSS overlaps and creates confusion. My question is: What are the b ...