Gradually make the table row and its contents disappear as you scroll, based on how hidden the row is within the containing div

Uncertainty clouds my mind as to the practicality of this approach, the potential performance implications, and the frequency of scroll events firing to make this technique viable. Nevertheless, within a fixed height div (overflow-y: auto) lies a table.

My aspiration is to introduce an effect where each row and its contents fade out proportionally based on how far they are off the screen.

The core of the jQuery solution I've devised appears as follows:

 // on scroll event  
 $("#someDiv").on('scroll', function () {

     // grab each row
     $('tr', this).each(function () {

         var mainHeight = $("#someDiv").height();
         var rowTop = $(this).offset().top;
         var rowHeight = $(this).height();
         var rowBottom = rowTop + rowHeight;


         // the row has completely scrolled out of view
         if (rowBottom < 0 || rowTop > mainHeight) {
             $(this).css({
                 opacity: 0
             });
             return;
         }

         // the row is entirely visible
         if (rowTop > 0 && rowBottom < mainHeight) {
             $(this).css({
                 opacity: 1
             });
             return;
         }

         // apply fade out effect based on scroll position
         if (rowTop < 0) 
             $(this).css({opacity: rowBottom / rowHeight});
         else if (rowBottom > mainHeight) 
             $(this).css({opacity: (mainHeight - top) / height });

     });
 });

Despite the functionality of this code, achieving the opacity transition on a tr element remains elusive.

Important: My priority is for this to function flawlessly in Chrome.

See demonstration on JS Fiddle

Answer №1

There were a few conceptual issues with my initial code implementation.

  • I forgot to consider the top position of the containing divs.
  • Also, I encountered variable errors such as using 'top = rowTop'.

After some modifications, the code now works, albeit a bit slowly. I may need to address this in another question.

$("#someDiv").on('scroll', function () {
    var mainHeight = $(this).height();
    var mainTop = $(this).offset().top;
    $('tr', this).each(function () {

        var $this = $(this);
        var rowTop = $this.offset().top - mainTop;
        var rowHeight = $this.height();
        var rowBottom = rowTop + rowHeight;


        // Check if the row is fully off the screen
        if (rowBottom < 0 || rowTop > mainHeight) {
            return;
        }

        // Check if the row is fully visible
        if (rowTop >= 0 && rowBottom <= mainHeight) {
            $this.css({
                opacity: 1
            });
            return;
        }

        // Apply fade out effect based on position
        if (rowTop < 0) 
            $this.css({ opacity: rowBottom / rowHeight});
        else if (rowBottom > mainHeight) 
            $this.css({ opacity: (mainHeight - rowTop) / rowHeight});

    });
});

JSFiddle

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

Submitting late leads to several consecutive submissions

I am working on a jQuery code that aims to introduce a short time delay to allow for the proper execution of an AJAX request: $('#form_id').submit(function(e) { e.preventDefault(); $submit_url = $(this).data('submitUrl'); ...

Maintaining Proper Header Dimensions

Having some issues with aligning my fixed widget and floating navigation. The fixed widget is not respecting the height of the floating navigation and is appearing at the top when in its fixed position. I'm currently using "Q2W3 Fixed Widget" for the ...

Arranging the navigation bar at the top of my chatbot in HTML

Is there a way to position the navigation bar above the chatbot on my website? The following CSS code excerpt is from the "navigation bar css" file: *{ padding: 0; margin: 0; text-decoration: none; list-style: none; box-sizing: border ...

Tips for toggling the visibility of a flexbox-styled popup using jQuery

I am trying to implement a popup on my website and I want to use flexbox styling for it. I have the necessary scss mixins for flexbox properties, however, I encountered an issue. The problem arises when I try to hide the popup using display: none, as the ...

Phonegap on iOS Simulator experiencing issues with executing cross-domain ajax requests

As I embark on my journey with Phonegap and mobile development, I am encountering a roadblock when trying to make a call to $.ajax(...) from the iOS simulator or an iOS device. Surprisingly, everything works perfectly when running the app from a browser or ...

Delete the line-height property from the initial line

I have created text that is aligned using the "justify" option, and I would like to add some leading to the text. However, I want the first line of the text to remain at x:0px y:0px. Does anyone know how to remove the line-height from the first line of a ...

Is there a way to use CSS to swap out the content of one HTML element with another HTML element's content?

Having an issue with editing or locating a plugin's HTML template on WordPress. Wondering if it is possible to replace the content of one HTML element with another using just CSS? Appreciate any help! ...

Organize the Div elements in the form of a message

Here is the code I am working with: https://jsfiddle.net/bdqgszv5/1/ This is the same code without jsfiddle: <section id="aussteller" class="row separated"> <div class="section-header text-center"> <h2& ...

The alignment of Bootstrap table headers in a Vue.js component needs adjusting for better display

Is there a way to ensure proper alignment of headers and column bodies in my Vue.js DataGrid component when utilizing the vuedraggable package for column reordering? Below is the code snippet for my DataGrid.vue component: <template> <div class ...

Steps to center the search form in the navbar and make it expand in Bootstrap 5

I'm utilizing Bootstrap v5.1.3 and have included a navbar as shown below: navbar Here is the code for that implementation: <div class="container"> <nav class="navbar navbar-expand-lg navbar-light bg-light& ...

PHP is unable to fetch JSON data from an ajax request

My JavaScript code is as follows: var person = []; person[0] = "John"; person[1] = "Doe"; person[2] = 46; var myData = JSON.stringify(person); $.ajax({ type: "POST", url: "test.php", dataType : "text", ...

Utilizing jQuery to submit the form

After clicking the search icon, it displays an alert with the message ok, but not h. Based on the code snippet below, it is intended to display alerts for both ok and h. <?php if(isset($_POST['search'])){ echo "<script type='text/ ...

Creating a ToggleButton in C#Is a Togglebutton the

I am trying to create a togglebutton for a website with Microsoft Visual Studio and am not sure if this would be the correct code to apply. I am working in C#, so the button is going to be generated in C# (and a bit of jquery) code and eventually styled in ...

Ways to recycle a section of Javascript for multiple uses on a single page

As a newcomer to website building, I have a query regarding the usage of a JavaScript function designed for a slideshow. My one-page website comprises multiple slideshow units with their own divs, holders, and counters (e.g., 1/4). A JavaScript code contro ...

Troubleshooting HTML Output Display Issues

I've been trying to post my content exactly as I submit it, but for some reason, it's not working. When I enter two paragraphs in a post, the output doesn't maintain that formatting. Instead, it removes the paragraph breaks and displays the ...

Solve the problem with SCSS at the component level in NextJS

I've decided to transition my regular React app to Next.js. In the past, I would simply import SCSS files using: import from '.componentName.scss' However, now I need to import them using: import style from 'componentName.module.scss ...

Retrieve an image using screen resolution parameters [using only HTML]

During a recent interview, the interviewer posed an interesting question regarding 2 images: There is one large resolution image that needs to be displayed on laptops and desktops. There is also a small image that should only be shown on mobile devices. ...

What is the best way to achieve consistent width in a material-ui card component?

How can I ensure that all these cards have the same width? https://i.stack.imgur.com/Ns3zw.png I'm experiencing an issue with achieving uniform card widths when using flexbox in material-ui. How can I resolve this? Is it necessary to utilize Grid fo ...

Populate Select2 with data after making an AJAX call for insertion

Currently, I am implementing the Select2 plugin with AJAX functionality using the code snippet below: $(".select2-ajax").select2({ placeholder: "Search user", minimumInputLength: 1, ajax: { url: $('#url-search-clie ...

Tips for changing between two texts within a button when clicked

Seeking a solution for toggling between two different texts inside a button when making an ajax call, with only one text displayed at a time. I'm facing difficulty in targeting the spans within the button specifically. Using 'this' as conte ...