Hovering over the paragraph will now allow you to shine a spotlight on

Can we highlight only the line being hovered over by the mouse pointer? For instance, if I have this paragraph:

<p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
    Nam in nisi eleifend, efficitur ligula vel, scelerisque risus. 
    Interdum et malesuada fames ac ante ipsum primis in faucibus. 
    Nam semper diam sodales eros dictum euismod. Duis ut hendrerit leo. Sed iaculis sem ac est porttitor facilisis. 
    In convallis facilisis libero ut interdum. Phasellus scelerisque ex in lacus tempus mollis. Integer scelerisque viverra elit id fringilla. Proin nibh arcu, imperdiet a lacus ac, feugiat interdum sapien. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse ultrices vestibulum orci, a varius quam cursus ut. Nulla ut suscipit sapien. Morbi et urna elementum, pretium augue non, tristique nulla. Nulla massa tellus, facilisis at fringilla et, rutrum ac libero.
</p>

Could we emphasize the first line when it is hovered over, followed by the second line and so on?

Illustration: https://i.sstatic.net/1D2T0.jpg Source:

I am interested in a solution that applies to multi-line paragraphs without manual span addition.

EDIT (21.06.2016): - (solution for entire paragraph highlighting)
Rory McCrossan's solution was effective, but I encountered issues with implementation on my WordPress blog. The highlighting position calculation went awry after scrolling. However, I stumbled upon an elegant solution while working on this website—you can use hover.css and add the class hvr-fade to each <p> tag. While this doesn't exactly address the original question, it offers a simple way to tackle a similar problem with impressive CSS hover effects collection. Hopefully, this proves beneficial to someone...

Answer №1

There isn't a direct method to extract a single line of text from a larger element. To accomplish this, you can wrap each word in the p tag with its own span. Then, by hovering over the span tags, you can highlight siblings that share the same offset().top value. Here's an example:

$('p').html(function() {
    return $(this).text().replace(/\w+[,.]?\s+?/g, "<span>$&</span>")
});

$('p').on('mouseenter', 'span', function() {
    var $span = $(this);
    $('p span').filter(function() {
        return $(this).offset().top == $span.offset().top;
    }).toggleClass('hover');
}).on('mouseleave', 'span', function() {
    $('p span').removeClass('hover');
})

View working demo

Alternatively, you could add a single div element to a parent of the p, which moves along with the mouse cursor when hovering over the p and serves as a guide for reading lines:

$('.text-block')
    .append('<div class="line-marker">&nbsp;</div>')
    .on('mousemove', function(e) {
        $(this).find('.line-marker').css('top', e.clientY);
    })
.line-marker {
    display: none;
}

.text-block:hover .line-marker {
    position: absolute;
    width: 100%;
    background-color: #CC0;
    display: block;
    z-index: -1;
}

View working demo

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

Coordinating several navigation tabs and navigation bars in Bootstrap 4 to work together seamlessly

I am in the process of developing a tool that will assist my colleagues in reviewing and comparing information. I aim to present this information consistently for each order, with multiple groups of information per order accessible through nav-tabs. I woul ...

Activate a Tab using JavaScript in Vue.js

Hello there, currently working with Vue and Bootstrap-Vue. The scenario is as follows: I have designed a registration page with 2 tabs. The user needs to complete the first tab before moving on to the second one by clicking a button. The second tab initia ...

Executing PHP code discreetly

Alright, I currently have a php script located at for creating a new user. What I typically do is send the user data via POST to add it to the MySQL database and then send an email to the user for account verification. However, due to the slow SMTP serv ...

Is there a reason why it's not feasible to merge child/descendant and grouping selectors together?

Understanding the rules of constructing selectors that consist of more than one selector can be quite challenging. It seems that sometimes it is possible to create such selectors, which may contain instances of other selectors made up of multiple parts. H ...

The functionality of Angular material input fields is compromised when the css backface property is applied

I utilized this specific example to create a captivating 3D flip animation. <div class="scene scene--card"> <div class="card"> <div class="card__face card__face--front">front</div> <div class="card__face card__face--ba ...

Having trouble getting the CSS footer from cssstickyfooter.com to function properly

I tried implementing some basic CSS from to create a sticky footer that remains at the bottom of the page regardless of content length. However, I am encountering a problem with the footer not behaving as expected. There are actually two issues with the f ...

Capture the response from an AJAX request and store it in a JavaScript variable

I've been struggling to find a solution for this issue for quite some time now without any success. Here's what I'm trying to accomplish: I need to retrieve an array from my PHP file so that I can utilize it in my JavaScript code. Example. ...

jQuery, the magical tool that can make forms disappear and reappear in a blink

As the heading suggests, here is the code I attempted. The forms are designed to be displayed one after the other, with each subsequent form's appearance being determined by the information provided in the previous forms. $(document).ready(function() ...

What steps should I take to address the issue with my navbar?

I've written some code that creates a hamburger icon on mobile devices. When the user clicks it, a wave effect covers the screen, but I'm facing an issue where the wave doesn't cover the input field. My query is: how can I make the input fi ...

Firefox keeps track of the state of page elements

I have noticed that Firefox seems to retain the css state of my webpage's elements. Even after reloading, it still remembers things like the content in a div that was previously modified and the visibility settings of specific elements. Is there a way ...

The 'success' message on the comment form gets displayed at the top of the page, but it is obscured by the fixed header

After submitting a comment on a blog article (such as ), the page refreshes with a 'success' message above the form. Due to the fixed header on my layout, the success message is hidden behind it, making it difficult for users to see and confirm ...

Ajax failing to trigger upon submission

I need assistance in creating a form that will first submit via AJAX before directing to a specified URL. Once submitted, an email should be sent to me through newsletter.php <script type='text/javascript'> $(function () { $("#signup") ...

What is the best way to dynamically hide a textbox in JSP based on the selection of a

On my JSP page, I have a textbox and a checkbox. I attempted to use jQuery and JavaScript to hide the textbox when the checkbox is checked, but it doesn't seem to be working. Below is the code snippet: <p class="contact"> <input id="check" n ...

Mouse over effect to highlight the crosshair on a table

My code currently enables a crosshair function, but I am looking for a way to limit the highlight effect only within the cursor (hover) area. Instead of highlighting in a "cross" shape, I would like it to show a backward "L" shape. This means that the hig ...

What is the best way to utilize a JavaScript variable as a background within an inline style sheet?

I have a fun project for this evening - I am trying to make my website load a different background image every time the page is refreshed. Earlier on in this project, I managed to make the background interact with window size and screen resolution similar ...

I am experiencing an issue where the Angular md-sidenav is not opening within my CodePen environment

Despite my efforts, I am unable to get my md-sidenav to open when clicking the burger-icon. I have checked for log messages and even created a simple sayHi function that does not output anything to the console. There are no visible JS errors either. My c ...

I cannot pinpoint the reason behind the strange offset on my page

<div> <div> <span class="card-title">New Item Form</span> </div> <form (ngSubmit)="onSubmit()" class="col s6"> <div class="row"> <div class="input-field col s6"> <input type="te ...

Unable to open file:/// on localhost in the browser

I have been working on a website that can display the folders and files on a client's machine in an HTML table. Everything was functioning correctly, but I encountered an issue with creating href links for files on the client's machine which were ...

Establishing the primary language on a multi-language website

I am currently in the process of developing a website and I intend to offer support for both English and French languages. Up to this point, I've been following the primary solution outlined in this question: How to localize a simple HTML website pag ...

Looping Angular Components are executed

I am currently developing an Angular application and encountering an issue with my navbar getting looped. The problem arises when I navigate to the /home route, causing the navbar.component.html components to duplicate and appear stacked on top of each oth ...