Reach the bottom of the page using window.scrollBy

My goal is to create a page that smoothly scrolls continuously, where once it reaches the bottom, it goes back to the top and repeats the scrolling cycle. Currently, I have set up the basic structure of the page.

$(document).ready(function() {
  var scroll = setInterval(function() {
    window.scrollBy(0, 1);
  }, 20);
});
.content {
  height: 500px;
  width: 100%;
  background-color: #cccccc;
}

.inner-content {
  height: 80px;
  width: 100%;
  margin: 10px 0 10px 0;
  background-color: #ff00ff;
}

h1 {
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<div class="row content">
  <div class="col-md-12 inner-content">
    <h1>One</h1>
  </div>
  <div class="col-md-12 inner-content">
    <h1>Two</h1>
  </div>
  <div class="col-md-12 inner-content">
    <h1>Three</h1>
  </div>
  <div class="col-md-12 inner-content">
    <h1>Four</h1>
  </div>
  <div class="col-md-12 inner-content">
    <h1>Five</h1>
  </div>
</div>

Now, my challenge lies in finding a way for JavaScript to detect when the page has reached the bottom, so that I can trigger the autoscrolling from the top again. Any assistance on this matter would be greatly appreciated.

Answer №1

To determine if the window has reached the bottom, you can use the code snippet below:

 $(window).scroll(function() {
     if ( $(window).scrollTop() + $(window).height() == $(document).height() ) {
        $(window).scrollTop(0);
    }
  });

When scrolling, this code checks if the page is at the bottom and then resets the scroll position to 0 (top).

https://jsfiddle.net/qjvkgx9t/

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

The issue of ajax data not being sent during an update in jQuery sortable has been identified

Having an issue with a piece of javascript/jQuery code that utilizes the sortable function of jQuery. I am using it to arrange divs whose number is unknown, and currently attempting to send this data to the database through ajax: Using Javascript with jQu ...

Customizing Material-UI styles using makeStyles

Currently, I am in the process of building a small portfolio website. Despite my attempts to style an avatar using the makeStyles class, it seems that the Mui avatar root is overriding my desired styling. I am wondering if there is a way to achieve this w ...

Automatically navigate to a specific element as the user scrolls down the page

I am attempting to achieve a similar effect as seen on the App Builder Website. When the user reaches the header with the background image/video and scrolls down, the website smoothly transitions to the next div/section. If the user scrolls back up and ret ...

Access the value of a submitted form using jQuery, when there are multiple forms with identical class names

I've looked for a solution here but couldn't find one that meets my needs. I have multiple forms with the class name .sbt-form: <form class='sbt-form'> <input name='kord' val=1/> </form> <form class= ...

Is there a way for me to retrieve the input that the datepicker is being used for?

My approach involves applying the jquery datepicker to input elements with the class txt-date: $('.txt-date').datepicker({ showAnim: "blind", changeMonth: true, changeYear: true, dateFormat: "dd.mm.yy", numberOfMonths: 2 }); ...

Incorporating a variety of functions into an external javascript file

I am currently working on enhancing the functionality of a basic HTML page by incorporating JavaScript with multiple buttons. The problem arises when I attempt to introduce another function in my external JS file, as it causes the existing code to stop wor ...

Tips for dynamically assigning a class to a span tag within a popover tooltip that utilizes Bootstrap Tagsinput based on the result of an if-else statement

//The contents of my JavaScript file are as follows: $mytag.popover({ html:true, trigger: hover, container: $mytag, template: '<div class="popover" role="showDetails">' + ...//other '</div> ...

Retrieve the content within a div tag

Hey there, I have a div set up as follows: <div>1+1</div> I'm looking to send this '1+1' value over to my javascript code and calculate it to get '2'. Appreciate any help in advance! ...

Apply a unique design to a class by clicking on a button

There are 3 identical boxes with the same classes and a button: function changeColorAndAddPadding() { /* ??? */ } .box { border: 1px solid; display: inline; } <button onclick="changeColorAndAddPadding();">Click Here</button> <d ...

Enhance your Excel experience with custom add-ins that streamline your workflow and simplify tasks,

Currently, I am developing Excel add-ins with Office.js, Javascript, and React. My goal is to integrate a single sign-on process into the ribbon button functionality. The task pane should only be displayed to users after they have successfully signed in. ...

Outdated ExpiresByType directive in .htaccess leads to website layout distortion. What is the best way to prompt the browser cache to refresh?

I recently updated my website and had the following configurations in the old .htaccess file: <IfModule mod_expires.c> ExpiresActive On # Images ExpiresByType image/jpeg "access plus 1 year" ExpiresByType image/gif "access plus 1 year" Expi ...

Ways to adjust image placement and size post-rotation with CSS/JS to ensure it stays within the containing div and avoids being cut off

Check out this jsfiddle I've been experimenting with. The jsfiddle is designed to rotate an image by 90 degrees in either clockwise or anti-clockwise direction upon button click. However, the rotated image currently appears outside of its parent div. ...

Using jQuery to smoothly fade in elements and slide them up with a delay

Seeking assistance in adding a slideUp effect to my set of posts/divs in Wordpress that currently fade in one by one using jQuery. I want the posts to slide up and fade in simultaneously, similar to this example: where they animate on filter click. I&ap ...

Modify the appearance of a span element when an input is in focus

I am trying to display the word "test" by focusing on a text input. However, when the text input is focused, the z-index of the text should change to 0 but it's not working as expected. I have provided my code below: .text{ position: absolute; ...

ESLint encountered an error while attempting to load the configuration "next/babel" for extension

Every time I try to generate a build of my Next.js app by running "npm run build," I keep encountering this error. Check out the error I'm getting while running npm run build here. Also, take a look at my .eslintrc.json file here and my .babelrc file ...

Encountered an issue while trying to build Angular 6 with the --prod flag. The error message states: "ERROR Error: ngIfElse was expecting a TemplateRef, but received 'true' instead

ERROR Error: ngIfElse must be a TemplateRef, but received 'true' HTML File <select class="form-control"> <option selected value="0">Select Manufacturer</option> <option *ngFor="let brand of allMa ...

Obtaining environment variables from the host and accessing them in a react .env file

For my ReactJs application, which incorporates multiple profiles, I am in the process of setting it up to run across a development environment, QA environment, and production environment. To achieve this, I am looking to establish some environment variabl ...

Is there a way to replace the message 'no rows to show' with a custom component in ag-Grid for react?

Currently, I am utilizing ag-grid in my React application https://www.ag-grid.com. When the table or grid is empty, it displays the default message "No rows to show." However, I am interested in customizing this message or possibly incorporating a custom ...

fuzzy edges on an HTML5 canvas

When attempting to draw vertical lines on a canvas with a 1px width, some lines may appear wider or blurry. Various solutions, such as subtracting 0.5px or translating coordinates, have been suggested but do not seem to work in this case. Attempts have be ...

The JavaScript code malfunctions when I introduce a new HTML tag

I am attempting to create a simple photo gallery using PhotoSwipe. However, I have encountered an issue where certain functions like Previous and Next buttons stop working when I add new HTML elements. Here is the original code: <div class="my-gallery ...