Navigate to an element by scrolling from the bottom of the page

I am trying to implement a feature where the page will automatically scroll to a button that is positioned around 300px from the bottom of the page.

https://i.sstatic.net/XN0yL.png

Whenever this particular button is clicked, I want the body of the page to scroll either up or down in order to keep the button always at the bottom of the screen.

I'm struggling with using the common jquery animate function for my specific case:

 $(".hamburger").click( function(event) {     
     $('html, body').animate({
                    scrollTop: $(document).height() - x //need help here
                }, 400);
      });
   });

Any guidance on how to achieve this would be greatly appreciated.

Answer №1

To find the scroll point, calculate the bottom position of the button by adding the top position to the button height and then subtracting the viewport height.

$('button').on('click',function() {
  var bt = $(this).offset().top,
      bh = $(this).outerHeight(),
      bb = bt + bh,
      vh = $(window).height(),
      sp = bb - vh;
  $('html, body').animate({
    scrollTop: sp
  }, 400);
});
*{margin:0;}
section {
  height: 200vh;
}
footer {
  height: 200vh;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
</section>
<footer>
  <button>asdf</button>
</footer>

Answer №2

Give this a try

Here is the code snippet:

<div class="wrapper"></div>

<a href="#" class="top" id="backBtn">Back to Top</a>

</div>

CSS Styles:

.wrapper {
  background-color: lightblue;
  height: 600px;
}

JavaScript function:

$('a#backBtn').click(function() {
    $('html, body').animate({
        scrollTop: 0
    }, 800);
    return false;
});

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

Are If-instanceof-statements considered harmful in Typescript?

selectAction: (actionEvent) => { if (actionEvent instanceof Action1) { // action1 body implementation } else if (actionEvent instanceof Action2) { // action2 body implementation } } This code snippe ...

Tips for keeping Sub Menu visible at all times

Is there a way to keep the sub menu always visible on the homepage of my website's navigation? Thank you for your help! Check out my website here ...

import template from file using django

With 3 pages containing the same menu at the top of each HTML file, managing updates to all links in the menu can become challenging. To simplify this process, one solution is to create a separate file called menu.txt to store the menu content. By using a ...

PhantomJS Alert: UnresolvedPromiseRejectionNotice

My main objective is to extract data from a website using Node.js. I have successfully managed to gather information using the request package, however, the website I am targeting contains dynamic content which cannot be accessed solely with request. Aft ...

What methods can be used to conceal and protect the source code of CSS and JavaScript files from being downloaded

Is there a way to keep the source code for my website's CSS, JS, and image files hidden and prevent them from being downloaded? I noticed a website that seems to have achieved this successfully: www[dot]sales.4080.ir/themes/corporate3/index.html I w ...

Showcasing a JSON attribute in the title using AngularJS

I'm struggling to display the Title of a table. Here is where I click to open a "modal" with the details: <td><a href="#" ng-click="show_project(z.project_id)">{{z.project}}</a></td> This is the modal that opens up with det ...

PHP redirection cannot be directly translated to JavaScript redirection

I'm having an issue with my PHP file redirecting to an HTML file. The structure of my website is as follows: HTML ==> separate JS file (using Angular) ===> PHP file When the PHP file calls the header function, instead of redirecting, it displ ...

What is the optimal method for creating and testing AJAX applications on a local server, then effortlessly deploying them online?

Exploring AJAX development is new to me. The challenge I've encountered so far is dealing with the same-origin policy, which requires modifying host information strings like absolute URLs in JavaScript files every time I deploy local files to remote s ...

Arrange a pair of div containers side by side on the webpage without the need to alter the existing

Is there a way to align these two div side by side? <div class="main_one"> <div class="number_one">Title A</div> </div> <div class="main_two"> <div class="number_two">Title B</div> </div> <div class=" ...

The index.html file is unable to load the transpiled JavaScript code generated by B

For my learning purposes, I am working on creating a pure ES6 app with node/express as the web server. I have successfully implemented module imports in my server.js file, but when I try to use these scripts in index.html, I encounter an issue where the tr ...

What is the relationship between JavaScript node modules and vanilla script references within the browser environment?

When using the modular javascript approach with Node.JS + npm and browserify, how do you handle situations where you need to use javascript libraries that are not available on npm? Some of these libraries may not be compatible with modularity (amd/commonJs ...

The accordion feature fails to function properly when incorporated into an ajax response

When I click a button, an Ajax response is loaded. The response is successfully appended where it should be, but the issue arises with the accordion not working for the response part. Below is the structure of my response: <div class="articles-content ...

Having difficulty importing a react module as a node module

In my project, I have a React component located in the path src/components/test import React from 'react'; import ReactDom from 'react-dom'; class TestComp extends React.Component {} export default TestComp; I am exposing this compon ...

Adjustable height for the absolute element

Having a bit of an issue with the height of the absolute div. About: .wrapper - I want the wrapper to automatically adjust its height but be limited by a max-height. (No scrolling for the wrapper) .title - Just a title .content - The problem aris ...

I'm looking to locate the API documentation for AngularJS TypeScript

After transitioning from using AngularJS 1.4 and plain JavaScript to now working with AngularJS 1.5 but utilizing TypeScript, I have found it challenging to find helpful documentation. For instance, when trying to inject services like $q or $timeout into m ...

Feasible: Embedding my complete website using custom HTML code into an iframe

I have the HTML for a website saved in a JavaScript string and I want to display it within an iframe. How can I insert this HTML string into the iframe and make the website appear? I have attempted to use this.src = HTML_STR; this.innerHTML = HTML_STR; but ...

Utilizing the power of jQuery's .each() method to assign a function to numerous slideshow containers

On my one-page website, I have numerous small jQuery Cycle slideshow divs with varying data-values, like: <div class="foo bar" data-value="1000"> <img src="directory/img_0.jpg" alt="img 0" /> <img src="directory/img_1.jpg" alt="img ...

The hyperlink is unresponsive and cannot be activated with a click

I encountered an issue with my website where I am unable to click on the href link. Oddly enough, the link works fine on my offline test version but once uploaded to the hosting server, it stops working. I have inspected the html and css coding on my webs ...

Google Analytics Experiments implemented on the server-side

Curious about the necessity of including the JavaScript cxApi when conducting experiments server-side. Is it possible to send the selected experiment and variations using PHP, or perhaps by injecting a JavaScript snippet internally (without relying on exte ...

What is the process for choosing corresponding values in jQuery?

Hello, I am a beginner in programming and I am currently working on developing a word guessing game. Here is the code I have written so far: The (letter) represents the key being typed by the player Var compareLetters = $('.letter') represents ...