Enhance text by hovering over it

I am currently working on implementing a unique feature using jQuery and CSS. Rather than just inheriting the width, I want to create a magic line that extends to the next index item.

Scenario:

1: Hover over Element one

ELEMENT ONE    ELEMENT TWO   ELEMENT THREE
___________

2: Hover over Element two

ELEMENT ONE    ELEMENT TWO   ELEMENT THREE
__________________________

3: Hover over Element three

ELEMENT ONE    ELEMENT TWO   ELEMENT THREE
__________________________________________

Although I have made some progress, I am facing challenges in getting the elements to expand properly based on the width of the entire list.

       
        // JavaScript code for the carousel functionality

       

#magic-line {
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 100px;
  height: 2px;
  background: #fe4902;  
}
// Additional CSS styles for the carousel display

.slick-slider {
  position: relative;
  overflow: hidden;
}
.carousel{
  // Additional CSS styles for the carousel container
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.min.js"></script>
<div class="carousel">
  <div class="carousel-image">
    <div class="container-big">
      <ul>
        <li>
          <img src="https://placeholdit.imgix.net/~text?txtsize=101&txt=Example+1&w=400&h=300" />
        </li>
        <li>
          <img src="https://placeholdit.imgix.net/~text?txtsize=101&txt=Example+2&w=400&h=300" />
        </li>
        <li>
          <img src="https://placeholdit.imgix.net/~text?txtsize=101&txt=Example+3&w=400&h=300" />
        </li>
      </ul>
    </div>
  </div>

  <div class="carousel-info">
    <div class="container">
      <ul>
        <li class="slick-current">
          <div class="c-header">About Us</div>
          <div class="c-container">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
        </li>
        <li>
          <div class="c-header">Others</div>
          <div class="c-container">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
        </li>
        <li>
          <div class="c-header">Main</div>
          <div class="c-container">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
        </li>
      </ul>
    </div>
  </div>

</div>

Answer №1

To adjust the magicline, focus on recalculating its width rather than its left position - which should always equal 0;

$magicLine.stop().animate({
        left: 0,
        width: leftPos+newWidth
});

Check out the demo here!

Answer №2

One method to achieve this effect is by utilizing CSS along with some JavaScript :

<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
</head>

<body>
  <div class="carousel">
    <div class="carousel-info">
      <div class="container">
        <div style="display:inline-block; position:relative;">
          <ul>
            <li class="slick-current" onmouseover="$('#line').css('width','34%');">
              <div class="c-header">About Us</div>
              <div class="c-container">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ut tristique lorem, et volutpat elit. Morbi leo ipsum, fermentum ut volutpat ac, pharetra eget mauris.</div>
            </li>
            <li onmouseover="$('#line').css('width','68%');">
              <div class="c-header">Others</div>
              <div class="c-container">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ut tristique lorem, et volutpat elit. Morbi leo ipsum, fermentum ut volutpat ac, pharetra eget mauris.</div>
            </li>
            <li onmouseover="$('#line').css('width','100%');">
              <div class="c-header">Main</div>
              <div class="c-container">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ut tristique lorem, et volutpat elit. Morbi leo ipsum, fermentum ut volutpat ac, pharetra eget mauris.</div>
            </li>
          </ul>
          <div id="line" style="position:absolute; bottom:0; left:0; width:34%; height:2px; background:red; transition:all 0.2s;"></div>
        </div>
      </div>
    </div>
  </div>
</body>

</html>

A creative approach was taken by incorporating an absolute div with transition effects, which dynamically increases in width upon hovering over each element - resulting in a visually appealing design!

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 waypoints.js snippet seems to be malfunctioning in this specific context

After experimenting with waypoints.js (jQuery.waypoints) for some time, I've been attempting to utilize this library to incorporate animations on children elements within a specific container. Here is the approach I have taken: var waypoints = $( ...

Preventing Form Submission from Redirecting to the Top in an HTML Document with PHP

Recently, I integrated a php form into my html code and changed the file from index.html to index.php. The contact form is functioning perfectly and sending all information as expected. However, after submitting the form, users are receiving the message "T ...

Using scriptlet based IDs in jQuery selectors involves incorporating JavaScript syntax within the jQuery selector to

I need to incorporate dynamic ids in my form, which are based on jsp variables within a scriptlet. How do I correctly select the desired element using jQuery's id selector without encountering any errors? Below is the code snippet: <form name="in ...

Combining various array values into a single key in JSON格式

Issue: I am working on a sign-up form for new users using HTML. The goal is to store multiple arrays (each containing "username: username, password: password, topScore: 0) within one JSON key ("user" key). However, the current functionality only allows f ...

Learn how to toggle between two different image sources with a single click event in JavaScript

If the button is clicked, I want to change the image source to one thing. If it's clicked again, it should change back to what it was before. It's almost like a toggle effect controlled by the button. I've attempted some code that didn&apos ...

Retrieving the text from the img tag of a bs4.element.Tag

I have a question that needs to be addressed. My concern involves a list of bs4.element.Tags, similar to the one illustrated in the image below. The issue at hand is filtering out only the href tags that are preceded by an <img> tag. How can this s ...

Combining and arranging numerous items in a precise location in three.js

I am currently working on a web application using three.js with Angular and facing some challenges when trying to set the precise positions of objects. The requirement is to load various objects and position them in specific locations. In order to load di ...

Combining the Power of Bootstrap with Yii Framework

As a newcomer to the Yii framework, I am looking to incorporate my Bootstrap design into this platform. One issue I have encountered is related to my custom CSS. In Bootstrap, we use navbar-default for styling the navbar. I customized my navbar color by ...

Update the picture displayed in the parent div when it is in an

My code currently changes the image when a parent div is in an active state, but I'm struggling to figure out how to revert it back to the original image when clicked again. I attempted to use 'return false' at the end of the script, but it ...

Tips for ensuring that the toJSON method in Sails.js waits for the find operation to complete before returning the object

When I try to run a find inside the toJSON function in the model, the object is returned before the find completes. How can I ensure that the return happens only after the find operation has completed? toJSON: function() { var obj = this.toObject(); Com ...

Change all instances of the subtraction symbol to parentheses using jQuery

--html-- <table> <tr> <th>a</th> <th>b<th> </tr> <tbody class = "tabledata"> <tr>a</tr> <tr>b</tr> </tbody> </table> --jquery-- $('.tabledata').empty(); for ( ...

The .each function in JavaScript is creating conflicts with other classes on the webpage

HTML <ul class="courseDates"> <li class="dateOne col-sm-2"> {tag_course date 1} </li> <li class="dateTwo col-sm-2"> {tag_course date 2} </li> <li class="dateThree col-sm-2"> { ...

Using forEach in React to simultaneously set multiple properties and return destructured output within the setState function

The following is the initial code snippet: setRows((rows) => rows.map((row) => selected && row.node === selected.id ? { ...row, row.a: "", row.b: "", row.c: "" } ...

Impeccable method to safeguard element from external css interference

Currently, I am working on a script that will be utilized across various pages and I want to ensure that the elements it generates are not affected by the CSS styles of those pages. Some individuals suggest using CSS like this: body>*{min-height:200px; ...

The Ajax request functions correctly in Chrome and Safari but encounters issues in Firefox and Internet Explorer

I've encountered an issue with jQuery where the call is failing when making a request with base64 authorization header in the beforeSend function. It's a simple request to retrieve projects. function GetProjects(full){ var query = "/Projects"; $ ...

Is it possible to successfully pass a parameter from a servlet to a JavaScript function, but encounter issues when trying to view the database

In my view servlet, I am displaying user data from the database in a table. Each row of the table has buttons that allow users to change the cells in that row to text boxes. The issue I am encountering is that when I retrieve the data and loop through to ...

AngularJS Form Validation Error Handling

I am in the process of implementing Form Validation using AngularJS, and I have come across a scenario involving ng-class that is confusing me. Could someone please explain why they are utilizing ng-class in this manner? The presence of a map and an arra ...

Customize the appearance of Woocommerce's blockUi feature with your

During an Ajax request, blockUI adds a style to the blocks of the checkout form and cart with "background: '#fff'". However, my entire website theme is black and I do not want the background color of the blocks to be white. Is there a way to remo ...

Unable to properly execute object calls with promises

I was facing a challenge with my code where I wanted to return two values in my promise chain, but was only able to call one of them successfully. Here is the section of my code causing the issue: app.get('/projects', (req, res) => { practic ...

Preloading error alert message displayed during AJAX request

When I send an ajax request with a dropdown change, the loader div is shown using "before send". However, the issue is that the loader only displays for the first time, even though the ajax functionality works all the time. If you want to check this issue ...