Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6843099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:12:19+00:00 2026-05-27T00:12:19+00:00

Trying to make simple jQuery function to create a scrollToTop button that fades in

  • 0

Trying to make simple jQuery function to create a scrollToTop button that fades in as you scroll down.

$(document).ready(function() {
    var start = 300;
    var duration = 200;
    var scrolled;

    $('.scrollUp').css('opacity', '0.0');

    $(window).scroll(function(){ 

        var opacity = (scrolled - start) / duration;

        scrolled = $(window).scrollTop();

        if (0 < opacity < 1) {
            $('.scrollUp').css('display', 'block').css('opacity', opacity);
        } else if (1 < opacity) {
            $('.scrollUp').css('display', 'block').css('opacity', '1.0');
        } else {
            $('.scrollUp').css('display', 'none').css('opacity', '0.0');
        }
    });

    $('.scrollUp').click(function(){
        $('html, body').animate({
            scrollTop: 0
        }, 500);
    });
});

See it in action here: http://jsfiddle.net/JamesKyle/fBvGH/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:12:19+00:00Added an answer on May 27, 2026 at 12:12 am

    This works, tested in jsfiddle:

    $(document).ready(function() {
        var start = 300;
        var duration = 200;
        var scrolled;
    
        $('.scrollUp').css('opacity', '0.0');
    
        $(window).scroll(function(){ 
    
            var opacity = (scrolled - start) / duration;
    
            scrolled = $(window).scrollTop();
    
            if (0 < opacity < 1) {
                $('.scrollUp').css('display', 'block').css('opacity', opacity);
            } else if (1 < opacity) {
                $('.scrollUp').css('display', 'block').css('opacity', '1.0');
            } else {
                $('.scrollUp').css('display', 'none').css('opacity', '0.0');
            }
        });
    
        $('.scrollUp').click(function(){
            $('html,body').animate({
                scrollTop: 0
            }, 500);
        });
    });
    

    Update:

    And here’s a working example with the opacity animation.
    Looks like this guy was looking for the same equation.

    Better to use some math in situation’s like this:

    scrolled = $(window).scrollTop();
    height = $('body').height();
    height = Math.ceil((scrolled / height) * 100);
    height = height / 100;
    

    Second Update

    Ok, you want it to start appearing after the dark blue section. Ok, so what you need to do is exclude that portion of the top before the gradient. You can do that by making an if clause that checks if the scrollTop value has hit the top part of the light blue gradient; around 300px from the top of the document. jsFiddle diagram

    Then instead of using the body height in the above equation, use the total height of the gradient section; about 210px. This value will replace the var height in the jQuery above. Let me know if you have issues implementing this. Didn’t notice you’re comment on the above answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a simple scroll to top button that smoothly scrolls up and
I'm trying to make a simple JQuery button from a div which will change
Trying to make a simple jquery drop down here, here's my code so far:
I'm trying to make a simple function with js & jquery but I can't
I'm trying to make a simple C# web server that, at this stage, you
I'm trying to make a simple in in-page popup called like this: var test
I am trying to make a simple tool tip function (I'm trying to learn
I'm trying to make a simple MySQL Admin thing with php and jQuery. I've
I trying to make a simple image gallery, where I have several radio button
I'm trying to make a drop down table using jQuery, with a similar code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.