Navigation buttons that move the user either up or down the page

I am a beginner in programming and I wanted to create two buttons for my webpage. One button should appear when the page is scrolled more than 300px and take the user to the top, while the other button should be displayed immediately and scroll the user to the bottom.

After some trial and error, I managed to implement a button that scrolls the user down using Javascript. However, when I tried to add the second button, only the last button was displayed on the HTML page.

<div id="TotopButton">^<span id="test"></span></div>
        <div id="ToDownButton">^<span id="test2"></span></div>
        <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br...

CSS

#TotopButton
{
    background-color: red;
    color: black;
    font-size: 35px;
    padding:10px;
    position:fixed;
    bottom:15px;
    right:15px;
    height: 50px;
    width: 150px;
    border:1px solid black;
    text-align: center;
    display:none;
}
#TotopButton:hover
{
    color:white;
    cursor:pointer;
   
}
#ToDownButton
{
    Background-color: blue;
    color: black;
    font-size:35px;
    padding:10px;
    position:fixed;
    bottom:15px;
    left:15px;
    height:50px;
    width:150px;
    border: 1px solid black;
    text-align: center;
    display: none;
}
#ToDownButton:hover
{
    color:white;
    cursor:pointer;
   
}

Javascript

window.onload = function()
   {
     var TotopButton = document.getElementById("TotopButton");
     var test = document.getElementById("test");
     window.onscroll = function ()
     {
        var TotopButton = document.getElementById("TotopButton");
         var yScrollAxis = window.pageYOffset;

        var test = document.getElementById("test");
        if (yScrollAxis > 300)
        {
            TotopButton.style.display = 'block'
        }
        else
        {
            TotopButton.style.display = 'none'
        }
        test.innerHTML = " "  + window.pageYOffset
     }
     TotopButton.onclick = function()
     {
         window.scrollBy(0, -1 * window.pageYOffset);
     }
   };
   //Second button
   window.onload = function()
   {
     var ToDownButton = document.getElementById("ToDownButton");
     var test2 = document.getElementById("test2");
     window.onscroll = function()
     {
        var ToDownButton = document.getElementById("ToDownButton");
         var yScrollAxis = window.pageYOffset;

        var test2 = document.getElementById("test2");
        if (yScrollAxis > 50)
        {
            ToDownButton.style.display = 'block'
        }
        else
        {
            ToDownButton.style.display = 'none'
        }
        test2.innerHTML = " "  + window.pageYOffset
     }
     ToDownButton.onclick = function()
     {
         window.scrollBy(0, 1000 * window.pageYOffset);
     }
   };

Answer №1

Here is the setup I use for a similar button to the top button you described:

HTML:

[code]
<section class="floating-button">
    <div class="btn-wrapper">
        <a class="primary-btn" id="floating-btn" href="#bottom-form">Apply Now</a>
    </div>
</section>
[more code]
<section class="final-form" id="bottom-form">
    [form here]
</section>

jQuery

$(document).scroll(function() {
    var y = $(this).scrollTop();
    if ((y > 490) && (y < 5698)) {
        $('#floating-btn').css('visibility','visible').fadeIn();
    } else {
        $('#floating-btn').fadeOut();
    }
});

To adjust the fading point of the button, change the value from 490 in the script above. You can also add console.log(y) to determine the right position or remove the condition y < 5698 if you want the button to always remain visible.

SCSS

.floating-button {
    z-index: 1;
    position: fixed;
    bottom: 34px;
    width: 100%;
}

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

What causes a standard React component with a default render prop to not pass PropTypes validation successfully?

I'm currently working on a React component with a render-prop that has a generic type. To improve usability, I want to set a default value for the render-prop. The code is functioning correctly, but during type-checking, I encountered a warning regard ...

Is there a way to transform a time string, for instance "8:02 AM", into a sortable field?

I am currently working with a MongoDB database that stores times as strings, and I want to filter queries based on specific time ranges. For instance, the time fields in my database are formatted as "8:02 AM" and "10:20 PM", and I am looking to refine my ...

Tips for making your inner content as wide as possible

I'm currently working on developing collapsible tables, where an outer and inner table are displayed for every row that is clicked. This is the code I have implemented: HTML: <table class="table outerTbl mb-0"> <thead> <t ...

Does Node.js support backward compatibility?

There is a common belief that Node.js is backwards compatible, meaning scripts running in Node.js N should also work in Node.js N+1. I have been unable to find any documentation confirming this assumption. Is there another way to verify compatibility aside ...

The function Map() in React-Leaflet cannot be executed, despite the presence of data

I am attempting to replicate the React-Leaflet example of a list of markers. I have an array of objects that I am passing to MarkerList to be transformed into Fragments and displayed on the map. However, my mapping function is not functioning as expected ...

Is it possible to utilize ag-grid's API to filter multiple checkbox values simultaneously?

I am currently utilizing angularjs and have implemented a series of checkboxes to filter my ag-grid. So far, I have successfully utilized radio buttons and the api.setQuickFilter method for filtering based on individual values. However, I am facing an iss ...

Execute an if statement in PHP upon clicking an image, all while avoiding the need to refresh the

My goal is to trigger my PHP mail(); code with an if statement when an image is clicked. Here's what I've attempted so far: <?php $to = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="720013041c111d1f02131c0b321 ...

What is the simplest method for transferring data to and from a JavaScript server?

I am looking for the most efficient way to exchange small amounts of data (specifically just 1 variable) between an HTML client page and a JavaScript server. Can you suggest a script that I can integrate into my client to facilitate sending and receiving d ...

An error message is displayed when attempting to retrieve a json response

After implementing my flask app, I noticed that the following code snippet is being returned: return json.dumps({'status': 'OK','url': 'www.blahg.com'}) Upon inspecting my javascript code, I found it to be structur ...

Issue with Material-UI's DataGrid scrollbar not displaying on the screen

I have a DataGrid set up with at least 20 rows. My goal is to limit its height and allow users to navigate through the remaining rows using the scroll bar within the component. However, I'm facing an issue as the scrollbar for the DataGrid itself is n ...

What is the best way to declare module variables in a Node.js environment?

When it comes to declaring variables when requiring modules in nodejs, there are different styles followed by well-known developers. For instance, TJ Holowaychuk uses a style like this: (method1) var connect = require('connect') , Router = req ...

Building with Webpack on a started Express server can be achieved by following these steps

Hello there, I am diving into the world of React and Webpack. My configuration in `webpack.config.js` looks like this: const webpack = require('webpack'); const path = require('path'); module.exports = { cache: true, entry: { ...

Activate the Click in the Span Element with no Matching ID to the Final Segment of the URL

I'm facing an issue with triggering the click event on a specific span that lacks an id attribute. This particular span has an empty ID and doesn't respond when I try to click the back or forward buttons in the browser. All other spans trigger th ...

Having an absolute element can lead to overflow causing a scroll

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .wrap { ...

Incorporating the FLEX property into my code has been a challenge as it is not functioning properly on iOS 7 and 8 devices. Does anyone have any tips or suggestions on how to

While utilizing FLEX in my code, I noticed that the output is not what I expected on iOS 7/8 devices. <div className="modal-row"> <div className="col1"> <span>{dict.YourServiceNumber}</span> </div> <div ...

What is the best way to include my preferred links for reading with PHP Universal FeedParser?

Recently, I've been experimenting with the PHP Universal FeedParser to retrieve and display RSS feeds on my website. However, as a newcomer to this technology, I have encountered some challenges. While the initial link provided in the sample works wit ...

Tracking a user's path while redirecting them through various pages

Recently, I created a website with a login page and a home page using nodejs, javascript, and html. The client side sends the entered username and password to the server, which then replies based on the validation result. During navigation between pages, h ...

Accessing dropdown selection in Javascript-Json: A Comprehensive Guide

My dropdown list is being populated dynamically using json. The selected option from the first dropdown determines the options in a secondary dropdown. I need to use the selection from the second dropdown to automatically fill out two more fields. For exa ...

Limit access to a page only to designated users

Having a challenge creating a secure page for band members to access their individual band pages. In the band table, each band has four columns labeled $bandm1, $bandm2, $bandm3, and $bandm4. I attempted to create a script that extracted the session user ...

Utilizing CSS naming conventions to bring order to class inheritance

I'm a bit unclear about the rationale behind using this specific CSS structure: h2.class-name It seems like you could simply use .class-name and apply that class to the h2 element: <h2 class="class-name">Title thing</h2> Unless it&apos ...