Alter the CSS of a particular ul li tag using jQuery when hovering over it

Just starting out with jQuery and struggling to accomplish my goal. Need to work with HTML only as the server doesn't support PHP or Ruby, and I'm still learning those languages. Working with the latest jQuery version 1.10.2.

My issue involves a menu with tiles, each containing a preview image and a title ribbon. My aim is to change the opacity of the title ribbon's background when the mouse hovers over a tile.

I've managed to make it work to some extent, but the problem is that when hovering over a tile, all the title ribbons change opacity, not just the one being hovered over. I tried using the .index method to get the index number of an 'li' element and use it as an identifier, but that didn't work as expected. I also attempted the following:

<script>
    $(function() {
        $('menu ul li').on({
            mouseenter: function () {
                $(this) <some code would come here to do stuff as mouse cursor enters the item area> ;
            },
            mouseleave: function () {
                $(this) <some code would come here to undo stuff as mouse cursor leaves the item area>;
            }
        });
    });
</script>

However, I couldn't figure out how to proceed from there to modify the $('.tt1').css.

Below are the relevant code snippets I have so far...

jQuery code:

<script>
    $(function() {
        $('menu ul li').on({
            mouseenter: function () {
                $('.tt1').css('opacity', '1.0');
            },
            mouseleave: function () {
                $('.tt1').css('opacity', '0.6');
            }
        });
    });
</script>

HTML code:

<menu>
    <ul class="collumn-left">
        <li><a href="#About"><div class="tt1">About</div></a></li>
        <li><a href="#Test"><div class="tt1">Test</div></a></li>
    </ul>
    <ul class="collumn-right">
        <li><a href="#Random"><div class="tt1">Random</div></a></li>
        <li><a href="#More"><div class="tt1">More</div></a></li>
    </ul>
</menu>

CSS code:

/* Your CSS styles go here */

Answer №1

Check out this piece of javascript code:

<script>
    $(function() {
        $('menu ul li').on({
            mouseenter: function () {
                $(this).find(".tt1").css('opacity', '1.0');
            },
            mouseleave: function () {
                $(this).find(".tt1").css('opacity', '0.6');
            }
        });
    });
</script>

edit:

Alternatively, a potentially cleaner way to do this is as follows:

CSS

.tt1:hover, .tt1.hover {
    opacity: 1.0;
}

Javascript

<script>
    $(function() {
        $('menu ul li').on({
            mouseenter: function () {
                $(this).find(".tt1").addClass("hover");
            },
            mouseleave: function () {
                $(this).find(".tt1").removeClass("hover");
            }
        });
    });
</script>

You can easily enhance this further by tweaking your CSS. For instance, adding transitions or different styles for smaller screens.

Answer №2

Avoid using javascript and rely on css

menu ul li .ttl:hover {
   opacity:1.0;
}

Answer №3

$(this).find('.tt1').css('opacity', '1.0');

find() locates a descendant element within the current hovered element with the class tt1.

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

Refresh the PartialView only after clicking submit

I am struggling with updating only the contents of my partial view after clicking an input type="submit button. My goal is to refresh just the partial view and update it with the updated model from the controller code, without refreshing the entire page. S ...

What causes a "UnhandledPromiseRejectionWarning" while using Puppeteer?

What causes the following warnings to appear, and how can I resolve them? Warnings: (node:26771) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Protocol error (Runtime.callFunctionOn): Target closed. (node:26771) ...

The `encodeAddress()` function in Google Geocode is used to ge

Encountering issues with extracting latitude and longitude values from Google's response. Google is providing XML-like data: "location" : { "lat" : 53.55914120, "lng" : 10.00923520 }, I am trying to parse this using var r = results[0].geome ...

Modify MUI's ListItemText component by targeting a specific span to implement customized styles using the useStyle hook

It's quite perplexing that although this is a straightforward task in regular CSS, it must be accomplished through MUI's useStyles for some peculiar reason. Essentially, I possess a ListItem containing a ListItemText. It appears as follows: cons ...

Detecting drag events between connected angular ui-trees is a complex yet achievable task

How can I trigger an action when an item is dragged from tree#1 to tree#2 and dropped? I want to make a specific HTTP call to save the transferred item. I have successfully implemented actions within one tree using the 'dropped' event, but strugg ...

Could not add metric widgets in Ambari

Having trouble adding metrics widgets for a component I created in Ambari. Any help would be appreciated... Running Ambari version 2.5.2 and encountered the following errors: On opening the component page: app.js:66933 Uncaught TypeError: Cannot read p ...

struggling to align menu items on both sides of a navbar

I am currently facing an issue with positioning elements in my HTML code. I have a File drop-down that I want to display on the left side, while the rest of the drop-downs should appear on the right side. It seems like my CSS files might be causing some in ...

The issue with jQuery's .after() method is that it is inserting content as a string instead

Having trouble inserting a button element after another element selected using jQuery. Here's how I have my element selected: let btn = $("a[translate='server.ipmi.kvm.LAUNCH']")[0]; When I try to insert the button after it, I'm using ...

What is the solution to preventing Angular 1.3 ngMessages from affecting the size of my form?

Hey there, I'm diving into front-end design for the first time. My issue is with a form that I want to use ng-messages for validation error messages. Currently, my form's size changes depending on whether there are errors displayed or not, and it ...

Error: The "res.json" method is not defined in CustomerComponent

FetchData(){ this.http.get("http://localhost:3000/Customers") .subscribe(data=>this.OnSuccess(data),data=>this.OnError(data)); } OnError(data:any){ console.debug(data.json()); } OnSuccess(data:any){ this.FetchData(); } SuccessGe ...

What is the best way to design a footer that remains fixed at the bottom of the screen but becomes unfixed when the user scrolls down?

Currently, I am working on creating a footer that remains fixed at the bottom of the user's screen regardless of the screen size. However, I also want the header to transition from a fixed position to being part of the page when the user scrolls down. ...

Running a function exclusively within a single div using Angular 2

I am currently using *ngFor to group items, and it's functioning correctly. However, I am having trouble displaying the "listofGroup" in the view even though it works in the console. Specifically, I need to run a function within a specific div in Angu ...

The process of downloading a webpage onto a computer is not completely successful

I recently created a webpage on WIX and attempted to download it using CTRL+S. However, when I loaded the site from my computer, certain elements were not functioning properly. Is there a specific piece of code that is missing when saving the webpage in th ...

Is it possible for a gradient between accessible colors to also be accessible?

Ensuring accessibility on websites is a top priority for me, with our goal being at least AA standard. We utilize tools like and to test the contrast between background colors and black or white text. For example, let's consider white (#fff) text a ...

Utilize cURL to retrieve a file from a webpage designed for automatic downloads

I am currently working on a script to automatically download and install the most up-to-date versions of specific applications. Is there a simple way to download the latest program from a website? For instance, if we look at the Firefox download page: On ...

The URL in a request is altered prior to execution

I am encountering an issue with my NodeJS application where it automatically appends my domain to the URL set in my http request. How can I prevent this from happening? I have tried to search for similar problems but have not found any relevant solutions. ...

After trying out the demonstration on the website, I am unable to get my submit button to work

Having trouble with my Formsubmit feature. Despite following the example on the website, my submit button doesn't send anything and the verification isn't reaching my email. Currently working with Angular. <form action="https://formsubmi ...

The user's input is not being bound properly when using $scope.$watch

Just started my Angular journey and I've been stuck for the past couple of days trying to figure out how to bind data from a new search using a service. Previously, I had the search functionality working with the code below before transitioning to a s ...

What is the best method for choosing all children's text except for a specific tag in Selenium's XPath selector?

Here is the HTML code I am working with: <div id="content"> <h1>Title 1</h1><br><br> <h2>Sub-Title 1</h2> <br><br> Description 1.<br><br>Description 2. <br><br ...

Creating a dynamic image slider that smoothly glides across a webpage

I've been working on a carousel (image slider) for my website and I need some help. Specifically, I want to reposition the entire slider slightly to the left on the webpage. You can see the slider in action on this site: and I also created a jsfiddle ...