Auto-Hide the "Scroll to View" Label After User Starts Scrolling

Last updated 3 weeks, 5 days ago | 53 views 75     5

Tags:- HTML JQuery CSS

Fifth page link: How to Make the "Scroll to View" Label Sticky Above the Table When Scrolling Horizontally
Yesss!
Let’s take this to a whole new level of smart UX.


✨ What We'll Do:

  1. Show the "Scroll to view" label if needed ✅

  2. When the user starts scrolling horizontally, fade out the label after a few seconds! ✅

  3. Reset everything properly if the user resizes or reloads ✅


Updated Full Plan:

  • Detect horizontal scroll on .table-wrapper.

  • Start a timer when the user scrolls.

  • Fade out the .scroll-notice after, say, 3 seconds.

  • Prevent multiple timers if user scrolls multiple times.


✅ Updated jQuery Code

$(document).ready(function() {
    $("table").each(function() {
        var columnCount = $(this).find('tr:first-child td, tr:first-child th').length;

        if (columnCount > 5) {
            if (!$(this).parent().hasClass('table-wrapper')) {
                $(this).wrap("<div class='table-wrapper'></div>");
                $(this).before('<div class="scroll-notice" style="display:none;">Scroll to view </div>');
            }
        }
    });

    function updateScrollNotices() {
        $(".table-wrapper").each(function() {
            var $wrapper = $(this);
            var $table = $wrapper.find('table');
            var $notice = $wrapper.find(".scroll-notice");

            if ($table.outerWidth() > $wrapper.width()) {
                $notice.show();
            } else {
                $notice.hide();
            }
        });
    }

    updateScrollNotices();

    $(window).on('resize', function() {
        updateScrollNotices();
    });

    // New: Auto-hide logic
    $(".table-wrapper").each(function() {
        var $wrapper = $(this);
        var $notice = $wrapper.find(".scroll-notice");
        var timer = null;

        $wrapper.on('scroll', function() {
            if ($notice.is(":visible")) {
                if (timer) {
                    clearTimeout(timer); // Clear existing timer
                }
                timer = setTimeout(function() {
                    $notice.fadeOut();
                }, 3000); // 3 seconds after scroll
            }
        });
    });
});

Full Example with Auto-Hide Label

(Same HTML + CSS as before — just this updated script!)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Sticky Scroll Notice for Wide Tables</title>
  <style>
    .table-wrapper {
      overflow-x: auto;
      max-width: 100%;
      border: 2px solid #ccc;
      margin: 20px 0;
      padding-top: 30px;
      position: relative;
    }
    .scroll-notice {
      position: absolute;
      top: 0;
      left: 0;
      background: #f9f9f9;
      padding: 6px 10px;
      font-size: 14px;
      color: #555;
      font-style: italic;
      white-space: nowrap;
      z-index: 10;
      display: none;
    }
    table {
      border-collapse: collapse;
      width: 100%;
      min-width: 600px; /* simulate wide tables */
    }
    th, td {
      border: 1px solid #aaa;
      padding: 8px 12px;
      text-align: left;
    }
  </style>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>

<h2>Small Table (No Scroll Notice)</h2>
<table>
  <tr><th>Name</th><th>Age</th><th>City</th></tr>
  <tr><td>John</td><td>25</td><td>New York</td></tr>
  <tr><td>Jane</td><td>30</td><td>Los Angeles</td></tr>
</table>

<h2>Large Table (Sticky Scroll Notice)</h2>
<table>
  <tr>
    <th>Product</th><th>Price</th><th>Stock</th><th>SKU</th><th>Category</th><th>Brand</th>
  </tr>
  <tr>
    <td>Phone</td><td>$699</td><td>Available</td><td>PH1234</td><td>Electronics</td><td>Samsung</td>
  </tr>
  <tr>
    <td>Laptop</td><td>$999</td><td>Available</td><td>LT5678</td><td>Electronics</td><td>Dell</td>
  </tr>
</table>

<script>
$(document).ready(function() {
    $("table").each(function() {
        var columnCount = $(this).find('tr:first-child td, tr:first-child th').length;

        if (columnCount > 5) {
            if (!$(this).parent().hasClass('table-wrapper')) {
                $(this).wrap("<div class='table-wrapper'></div>");
                $(this).before('<div class="scroll-notice" style="display:none;">Scroll to view </div>');
            }
        }
    });

    function updateScrollNotices() {
        $(".table-wrapper").each(function() {
            var $wrapper = $(this);
            var $table = $wrapper.find('table');
            var $notice = $wrapper.find(".scroll-notice");

            if ($table.outerWidth() > $wrapper.width()) {
                $notice.show();
            } else {
                $notice.hide();
            }
        });
    }

    updateScrollNotices();

    $(window).on('resize', function() {
        updateScrollNotices();
    });

    // New: Auto-hide logic
    $(".table-wrapper").each(function() {
        var $wrapper = $(this);
        var $notice = $wrapper.find(".scroll-notice");
        var timer = null;

        $wrapper.on('scroll', function() {
            if ($notice.is(":visible")) {
                if (timer) {
                    clearTimeout(timer); // Clear existing timer
                }
                timer = setTimeout(function() {
                    $notice.fadeOut();
                }, 3000); // 3 seconds after scroll
            }
        });
    });
});
</script>

</body>
</html>

Pro UX Tips:

  • Adjust the hide time:
    You can set it faster (e.g., 1500ms) or slower (e.g., 5000ms) depending on your audience.

  • Fade back in on resize:
    After a major resize (like turning mobile landscape), you can reset the notice if needed.

  • Sticky label height:
    Ensure your .scroll-notice and .table-wrapper have enough padding/margin so the sticky label doesn't overlap your content.


Common Pitfalls:

  • Clearing old timers is crucial:
    Otherwise, multiple scroll events could trigger weird multiple fade-outs.

  • Avoid showing/hiding labels during vertical scroll:
    We are listening to horizontal scroll inside .table-wrapper, so it’s scoped properly!


Final Result:

✅ Smart "scroll to view" label appears if needed
✅ Stays sticky above table
✅ Fades away automatically after user scrolls sideways
✅ Fully mobile and desktop friendly


Your Table UX Is Now Elite Level! 


See a bonus version:
"Show a tiny scroll progress indicator bar above the table as user scrolls horizontally"
That’s even fancier and looks super PRO! 

Click here