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

How can one determine the proper size for a border?

Can the border of a div be larger than the actual dimensions of the div itself? For instance, if the div is 10x10 in size, is it possible to have a border that is 20x10? ...

Capture all Fetch Api AJAX requests

Is there a way to intercept all AJAX requests using the Fetch API? In the past, we were able to do this with XMLHttpRequest by implementing code similar to the following: (function() { var origOpen = XMLHttpRequest.prototype.open; XMLHttpRequest.p ...

Adjusting the position of a stationary element when the page is unresponsive and scrolling

Managing a large web page with extensive JavaScript functionality can be challenging, especially when dealing with fixed position elements that update based on user scroll behavior. A common issue that arises is the noticeable jumping of these elements whe ...

Guide to creating varying component sizes using ReactJS and Styled Components

Is it possible to add variation to my button based on the prop 'size' being set to either 'small' or 'medium'? interface Props { size?: 'medium' | 'small'; } How can I adjust the size of the component us ...

Analyzing a text file against a string to identify discrepancies using Node.js

Recently, I came across a dilemma involving a text file containing information on all Wifi connections (such as ssid, mac no, strength, etc.) that were within my laptop's range just 2 minutes ago. Now, I've rerun the code and obtained the current ...

An exclusive execution of the JavaScript function is ensured

I have a JavaScript function that I want to execute 12 times in a row. Here's my approach: Below, you'll find a list of 12 images: <img id="img1" src=""> </img> <img id="img2" src=""> </img> <img id="img3" src=""> ...

Simulate internationalization for vue using jest

Currently, I am working on setting up jest unit tests for a Vue project within a complex custom monorepo. I am facing an issue with i18n, which I use for translation management in my application. The problem arises with the following code snippet for init ...

leveraging dependency injection to retrieve a function in JavaScript

I'm exploring a way to obtain a function in JavaScript without executing it, but still defining the parameters. My current project involves creating a basic version of Angular's dependency injection system using Node.js. The inject() method is us ...

Separating the login/register functionality from the main app using the MEAN Stack

Apologies for my poor English! I have developed an application using the MEAN stack (MongoDB + Express.js + Angular.js + Node.js) with authentication utilizing passport.js and JWT (jsonwebtoken and express-jwt). What I aim to achieve? The login and r ...

Executing a post request using axios

Having some trouble with a post request using axios and it's refusing to cooperate. Here is the specific request I need to make. This is what I attempted: await axios.post( 'https://api.searchads.apple.com/api/v4/campaigns/XXXX/adgroups/targ ...

Using footable js will eliminate all form elements within the table

After incorporating the Footable jQuery library to create a responsive table, I encountered an issue with input tags and select boxes being removed by the Footable JavaScript. It turns all these tags into empty <td> elements. <table id="accordi ...

ReactJS: Checkbox status remains consistent through re-rendering of Component

I have developed a JSfiddle example Initially, this fiddle displays a list of checkboxes based on the passed props to the component. When you click the Re-render button, the same component is rendered with different props. Now, please follow these steps- ...

Replicate and $(document).ready()

My form has required fields that need to be completed. To alert users about blank fields, I have implemented the following code: $(document).ready(function() { $('input.required:text').focus(function() { $(this).css({'background ...

Can you explain to me the concept of "face indices" in Three.js and how it functions?

I am attempting to use Three.js to build a unique irregular polyhedron. In order to achieve this, I have decided to utilize the PolyhedronGeometry feature (refer to the documentation). However, I am encountering difficulty in understanding the concept of ...

Optimizing web development: Employing HTML templates to dynamically enhance website performance

My task involves the redesigning of an existing website (foo.com) using .NET and C#. The website caters to multiple companies such as abc.com, def.com, ghi.com, etc. Currently, the website utilizes html templates specific to each company's website in ...

Tips for Removing Padding in Material UI Container Component

I'm currently working on creating a hero banner using the material-ui framework and I've encountered an issue. Here's what I have so far: https://i.stack.imgur.com/UXTDY.png However, I'm facing an irritating problem with left and rig ...

Issues stemming from cross-domain AJAX have resulted in errors with Karma and Jasmine

I've been working on configuring tests that utilize Jasmine and Karma to test my JavaScript code. Karma operates in Node.js and initiates a Chrome browser for testing purposes. Unfortunately, I keep encountering an error message that reads "Chrome 28 ...

Partially loading CSS stylesheet

My directory structure looks like this.... iamdan > css > style.css The php file, named head.php, is located in a folder called includes within: iamdan > includes > head.php This file contains the following code: <head> <title ...

Sending an HTTP request from within an Express/Node.js application

I am currently in the process of setting up an express service for a program I am developing. The goal is to interact with an external API, retrieve the data, and store it in a MongoDB database that I have already set up. While this task may seem straight ...

Ways to include additional assurances depending on a specific circumstance

When my code is executed in edit mode, I have a selection of API calls that need to be made, as well as one specific API call that must be made in both create and edit modes. Here is the current code snippet: // other controller code var config = {}; ...