0

I'm trying to implement a simple "scroll to top" button on my site.

HTML:

<a href="#" id="scrolltotop"><img src="img/scrolltotop.png"></a>

CSS:

#scrolltotop {
width: 40px;
height: 40px;
position: fixed;
right: 100px;
bottom: 50px;
display: block;     
}

JS:

jQuery(document).ready(function($){
$(window).scroll(function(){
    if ($(this).scrollTop() > 150) {
        $('#scrolltotop').fadeIn('slow');
    } else {
        $('#scrolltotop').fadeOut('slow');
    }
});
$('#scrolltotop').click(function(){
    $("html, body").animate({ scrollTop: 0 }, 500);
    return false;
});
});

When I load my page, I see the button there in the right place for a split second, then it is covered by a background of one of my divs that is down the page in my HTML structure. In the end, I can't see it anywhere on my page, and when I inspect the element it is in the right spot, just not visible.

1
  • Can you show some of the code where that specific div you talk about is in. And the CSS for that div? It might indeed like an answer says be the z-index but it could be something else. Are you sure you are not scrolling down and the #scrolltotop gets faded out? Commented Jul 25, 2013 at 18:02

2 Answers 2

1

I think you just need to add an initial display:none; to #scrolltotop like so:

#scrolltotop {
    width: 40px;
    height: 40px;
    position: fixed;
    right: 100px;
    bottom: 50px;
    display:none;
}

Working Example

When using fadeIn() you need to have the element's initial display state set, so that you're not trying to fadeIn() an element that's already present.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch... if it is not set to display: none; it is visible until you scroll down to the bottom and back up.. then the fade out takes over...
0

Add z-index: 1000 to the back to top link so it is shown on top of everything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.