Jquery back-to-top button instantly teleports to the top instead of smoothly scrolling

I recently tried my hand at creating a simple Back-To-Top button that appears when the page is scrolled past a certain point. I used jQuery

$(document).ready(function () {
    $(window).scroll(function() {
    if ($(this).scrollTop() > offset) {
        $('.back-to-top-button').fadeIn(200);
    } else {
        $('.back-to-top-button').fadeOut(200);
    }
});

});



/*Scroll Plugin*/
$('.back-to-top-button').click(function(event) { 
    $('html, body').animate({scrollTop: 0}, 'slow');
    event.preventDefault();
});

This is how I implemented it in the HTML

<a href="#" class="back-to-top-button" style="display: inline;">
                <i class="fa fa-chevron-circle-up"></i>
        </a>

However, I encountered an issue where instead of smoothly scrolling back to the top, the page jumps abruptly. Strangely, this problem only occurs on my website and not others I have tested it on.

Answer №1

The solution is now operational. I have defined the event handler within the document ready function

$(document).ready(function () {

var offset = 200;
var duration = 400;
$(window).scroll(function() {
    if ($(this).scrollTop() > offset) {
        $('.back-to-top-button').fadeIn(duration);
    } else {
        $('.back-to-top-button').fadeOut(duration);
    }
});

/*Scroll Plugin*/
$(".back-to-top-button").click(function(event) { 
        $('html, body').animate({scrollTop: 0}, 'slow');
        event.preventDefault();
    });

});

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

Angular2 Animation for basic hover effect, no need for any state change

Can anyone assist me with ng2 animate? I am looking to create a simple hover effect based on the code snippet below: @Component({ selector: 'category', template : require('./category.component.html'), styleUrls: ['./ca ...

What is the best way to retrieve the name of a dynamic form in a Controller

How can I retrieve the dynamic form name in my controller? See below for the code snippet: HTML <form name="{{myForm}}" novalidate> <input type="text" ng-model="username" name="username" required/> <span ng-show="(submit & ...

Can you help understand the unexpected output produced by this JavaScript code?

When looking at the following JavaScript code, you would expect an output of 32. However, the actual answer is 16. How is this possible? 5 + 1 = 6; 6 + 5 = 11; 11 * 2 = 22; 22 + 10 = 32; (this should have been the correct answer) var x = 5; x = (x++, a ...

Setting the color of all meshes in a group using color.setHex in Three.js

I am currently working on implementing a system for selecting objects in the scene by hovering or clicking on them. Within the scene, there are object groups that were created in the following way (please note that I am still learning three.js and this cod ...

Moving a div with arrow keys using percentages: A step-by-step guide

I found this amazing script on Stack Overflow that allows me to move elements around the page using arrow keys. It works flawlessly, and I love how it enables diagonal movement by combining different arrow key inputs. Now, my query is whether it's fe ...

How come the absolutely positioned element is nested within the first "row" relative div rather than its immediate parent div?

I have a specific goal in mind: I aim to create two rows, each containing three columns. I have used relative positioning for the rows, with three images in each row for a total of two rows, and this is working well. My next step is to add layered divs b ...

Move the content towards the left, out of view

Looking for ways to slide or scroll content left/right? Check out HTML Slider Example: <div class="row"> <div class="col-md-3"> <h4>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras tempus enim pellentesque susc ...

Expanding the Google Maps API

New to the Google Maps API and currently integrating data from a MySQL database to display markers on a map. Seeking help to adjust the map size to fit my website's layout. Below is the existing code: <!DOCTYPE html > <head> ... ...

Is there a way for me to receive user inputs, perform mathematical operations on them, and then display the result of the calculation? Additionally, is there a way to ensure that the output value automatically updates

As a newcomer to HTML and JavaScript, I'm unsure how to approach this task. Here is what I have so far: <div class="inputs"> <label for="#arena2v2">2v2 Arena Rating:&#9;</label><input class="pvp" type="number" step="1" m ...

What is the best way to apply typography theme defaults to standard tags using Material-UI?

After reading the documentation on https://material-ui.com/style/typography/ and loading the Roboto font, I expected a simple component like this: const theme = createMuiTheme(); const App = () => { return ( <MuiThemeProvider theme={theme}> ...

Adjust the Width of jQuery Data Table Headers Based on the Size of the Largest Content

My goal is to ensure that the column/header widths in my table are always consistent based on the content of the rows. Consider the example below: <table> <thead> <tr> <th>col 1</th> <t ...

Design adaptable HTML backgrounds

Hello, I am working on a webpage that uses two background images. Here is the CSS I'm using: body{ background: url('<?=base_url();?>/assets/header_bg.png') no-repeat, url('& ...

Sending JSON data through an AJAX post request will not work

After struggling for hours trying to debug this issue on my own, I've come to the point of seeking assistance. I need to send a substantial amount of data to an ashx handler and here's how it looks: var value = [{"start":["3,0"],"block":["0,0", ...

Ways to verify the presence of isBrowser in Angular 4

Previously, isBrowser from Angular Universal could be used to determine if your page was being rendered in a browser (allowing for usage of features like localStorage) or if it was being pre-rendered on the server side. However, it appears that angular2-u ...

The provided argument in Typescript does not match the required parameter type, which should be a string

Encountering a TypeScript error with the following code: if (this.$route?.query?.domainName) { this.setDomain(this.$route.query.domainName); } The code snippet above is causing the following error message: TypeScript - Argument of type 'string | ...

The functionality of jQuery's .hide method is not effective in this specific scenario

HTML section <div class="navigation-bar"></div> Jquery & JavaScript section function hideUserDiv(){ $('.ask-user').hide(); } var ask = '<div id="ask-user" style="block;position:absolute;height:auto;bottom:0;top ...

JSP not able to render CSS properly

Trying to apply some basic CSS to my JSP page. The JSP is located at WebContent > WEB-INF > pages > Login.jsp, while the CSS file is at WebContent > WEB-INF > css > Login.css. Below is my JSP code: <head> <link rel="stylesheet" ...

Consolidate all CSS links into a single line

Within my HTML document, I currently have the following CSS links: myPage.html <link href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,500,500i,700,700i" rel="stylesheet"> <link href="https://fonts.googleapis.com/icon?family ...

fixed div with scrolling content

My dilemma involves a menu located within a div on the side of the browser that I want to keep fixed in position. However, there is a concern that the height of the menu might exceed certain screen sizes, making links inaccessible. To address this issue, I ...

How can CSS height/width be verified according to set guidelines?

I am currently in the process of developing an ASP.NET control that generates <object> and <embed> tags. In order to ensure compatibility, I intend to add "Height" and "Width" properties to the control as both tags require these attributes. Add ...