Achieving success with the "silent-scroll" technique

I've been struggling to implement the 'scroll-sneak' JavaScript code for quite some time now. This code is designed to prevent the page from jumping to the top when an anchor link is clicked, while still allowing the link to function as intended. I'm specifically looking to stop the page from scrolling to the top when a navigation link within a table row below is clicked. The code works on the developer's demo page, but lacks detailed documentation. Any volunteers willing to help tackle this issue?

<tr id="tabs">

<th><a href="index.htm">Information</a></th>

<th><a href="plan.htm">Research</a></th>

<th><a href="councils.htm">Sources</a></th>

<th><a href="university.htm">Institution</a></th>

<th><a href="contact.htm">Contact</a></th>

</tr>

<script>
(function() {
var sneaky = new ScrollSneak(location.hostname), tabs = 
document.getElementById('tabs').getElementsByTagName('th'), i = 0, len = tabs.length;
for (; i < len; i++) {
tabs[i].onclick = sneaky.sneak;
}
    document.getElementById('next').onclick = sneaky.sneak; 
})();
</script>

UPDATE

After encountering several issues and bugs mentioned in the comments, the accepted answer did not provide the desired behavior consistency. However, I managed to find a simple solution that works across different browsers such as IE6, FF3, QZ6, and Webkit 537.21.

(function() {
var sneaky = new ScrollSneak(location.hostname);
document.getElementById('tabs').onclick = sneaky.sneak;
})();

Answer №1

Just one more edit:

If you're still having trouble with the last image, try making this change to see if it helps:

Replace the following code:

$(active).show();
$(active).siblings().hide();

With this:

$("#gallery-interior li").hide(0, function(){
        $("#gallery-interior " + active).show();
});

Prior Version

Below is the complete JavaScript code where I combined the script from another answer and the scroll-sneak for the tabs. After testing both in FF3, I can confirm that they are functioning properly:

var sneaky = new ScrollSneak("scrolltrack");

$(document).ready(function() {

    var urlHash = window.location.hash;

    if(urlHash) {
        $(".thumbs a[href='" + urlHash + "'] img").addClass("dimmed");
    } else {
        $(".thumbs a:first-child img").addClass("dimmed");
    }

    $("#tabs th a").on("click", function(e) {
        sneaky.sneak();
    });

    $(".thumbs a").on("click", function(e) {
        changeImage($(this).attr("href"), e);
    });

    $("#gallery-interior area").on("click", function(e) {   
        changeImage($(this).attr("href"), e);
    });

});

function changeImage(active, e) {
    var e = window.event || e;
    preventNav(active, e);
    $(".thumbs a img").removeClass("dimmed");
    $(".thumbs a[href='" + active + "'] img").addClass("dimmed");
    $(active).show();
    $(active).siblings().hide();
}

function preventNav(hash, e) {
    if (e) {
        if (typeof e.preventDefault != 'undefined') {
            e.preventDefault();
        } else {
            e.returnValue = false;
        }
    }
    var node = $(hash);
    node.attr("id", "");
    document.location.hash = hash;
    node.attr("id", hash.replace("#", ""));
}

Prior Version

This script has been tailored for use with tabs. In the solution provided for the gallery (Click anchor link and keep page position / prevent page-jump to top?), the page jump occurs because hash links do not reload the page and scrolling takes place by default and needs to be reversed. Although normal links won't have the same jumping effect, I've included the ability to capture hash links in case they are needed. It's recommended to place this at the bottom of your site's HTML, just before the closing body tag.

var sneaky = new ScrollSneak("tabs", false);
var capture = true;

$(document).ready(function() {

    var urlHash = window.location.hash;

    $("#tabs th a").on("click", function(e) {
        sneaky.sneak();
        capture = true;
    });

    capture = false;

});

window.onscroll = function () {
    if (capture) sneaky.scroll(), capture = false; // This part is only necessary for hash links
};

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

Border becomes dashed when dragging and dropping

I am working on enabling drag and drop functionality for users. When an item is lifted from its original position, a gray dashed box should appear in its place. As the item is moved closer to another spot, the containers adjust to create a target area (gra ...

Struggling to input data into Excel using Selenium WebDriver

I encountered an issue while attempting to write two strings to an Excel sheet using the following code. The error message I received was: java.lang.IllegalArgumentException: Sheet index (0) is out of range (no sheets) FileOutputStream fout=new FileOutput ...

What was the reason for node js not functioning properly on identical paths?

When the search route is placed at the top, everything works fine. However, when it is placed at the end, the route that takes ID as a parameter keeps getting called repeatedly in Node. Why does this happen and how can it be resolved? router.get('/se ...

Implementing pagination using getServerSideProps in NextJS allows for dynamic

I'm currently using NextJS along with Supabase for my database needs. I'm facing a challenge with implementing pagination as the solution I'm seeking involves passing queries to the API. However, since I'm fetching data directly from th ...

Playing with background hues

Can anyone help me with this issue? I've been attempting to change the background color using jQuery upon document load, but it doesn't seem to be working. Any thoughts on what I might be doing wrong? http://jsfiddle.net/KczZd/2/ HTML: <d ...

Is it possible to execute a function within an HTML page simply by clicking on it?

As someone who is new to the world of HTML, CSS, and JS, I'm currently facing a challenge: On my website's page1.html, I have implemented a feature that allows users to sort different articles on the page by selecting a sub-menu from the navigat ...

Looking for a way to customize it using @emotion/styled? Dealing with the deprecated makeStyles from MUI problem?

I have been working on setting up my styling using @emotion since makestyles were removed in React 18. Despite making changes as per my research, I am not seeing any updates reflected on my page. The expected result looks like this: https://i.stack.imgur. ...

Troubleshooting problems with a JavaScript game that involves guessing numbers

I have been given a challenging Javascript assignment that involves using loops to create a counting betting game. The concept is simple: the User inputs a number, and the computer randomly selects a number between 1 and 10. The User can then bet up to 10 ...

Adding turbolinks to an HTML document can be achieved without the need for a

Recently delving into the world of turbolinks, I discovered that it can be employed independently without relying on a client-side javascript framework. Eager to test this out, I created a bootstrap 4 template and attempted to install it. Firstly, I downl ...

React Animation Library With Smooth Initial Render and No Dependency on External Props

I'm currently implementing a Modal component within my app, utilizing Portals. My goal is for the Modal to smoothly fade in when it is rendered and fade out when it is no longer displayed. Upon examining the documentation for react-transition-group, ...

Refreshing all parts of a webpage except for a single div

Currently, I am in the process of creating a simple web page that includes a small music player (niftyPlayer). The individuals I am developing this for request that the player be located in the footer and continue playing when users navigate to different s ...

Combining and adding together numerous objects within an array using JavaScript

I'm looking to combine two objects into a single total object and calculate the percentage change between the two values. I'm encountering some difficulties while trying to implement this logic, especially since the data is dynamic and there coul ...

Switch image upon hover within a sprite

I have a sprite that I utilize for displaying various images. Currently, my aim is to change the sprite image upon hovering using solely CSS. .sprE { background-color: transparent; background-image: url(/i/fW.png); background-repeat: no-repeat; height: 30 ...

Tips for accessing a website and logging in to extract important data for scraping purposes

Currently facing an issue with scraping data from a website that requires logging in. I've been attempting to use the node request package for the login process, but so far have been unsuccessful. The code snippet I'm currently using is: var j = ...

Position a component in relation to another component using AngularJS

Utilizing ng-show and ng-hide, I created a descriptive box that appears below text when clicked. However, there is an issue as the description box does not align directly under the text, similar to what is shown in this image https://i.stack.imgur.com/phBh ...

Implementing authorization middleware using Express.js in Ajax

My app has a straightforward authorization middleware that functions flawlessly with regular GET and POST requests. However, when I send a POST request via AJAX, the middleware fails to redirect to a 401 page and instead bypasses it, allowing the data to b ...

Are you facing difficulties while trying to utilize useContext in your React application?

I have a basic React application where I need to implement useContext. (by the way, I am using Vite + React) Below is the code for my Context.jsx file: import React, {useContext} from 'react'; const emailContext = React.createContext(); expor ...

In search of a React.js/Redux OpenID library or example, particularly focused on Steam OpenID integration

Currently, I am developing a Node/Express REST + React/Redux application that requires Steam OpenID for authentication. Despite searching extensively for tutorials, libraries, or example code related to Steam OpenID (or any other OpenID), I have come up em ...

Steps for retrieving the attribute values of <i> tags enclosed within a designated <div> element

Currently, I am extracting data from a website where I encounter a recurring pattern with a varying number of values. For example, an instance of this pattern is shown below: <div class="lang"> <i class="flag fr" qtip-tooltip= ...

Linking asynchronous functions does not function properly

I've been attempting to link two asynchronous functions together, but it appears that the second function is running before the first one. Below is the code snippet: function performAction(e) { const ZIP = document.getElementById('zip').valu ...