简体   繁体   中英

Get div to show/hide when scrolling for a “back to top” link

I can't make my "go to top" id=arrow-up div to disappear when on eg top of the page.

At the top of the page I got

So I would like the arrow-up div to visible(show("slow")) when not on top of the page.

 var tmp = $(window).height();

Tmp is used to get the viewport height... Not sure if that is right?

I have seen other solutions but they are only kind of the same... and I can't make them work, also they font use :in-viewport . Can I make it with viewport or am I side tracked?

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">        </script>
<script src="bootstrap-3.1.1-dist/js/bootstrap.min.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script src="/jquery/isInViewport.min.js"></script>



$(window).scroll(function() {
  if($('#pageStart:in-viewport(tmp)')){
    $("#arrow-up").hide("slow");
  }else{
    $("#arrow-up").show("slow");
  }
});

"So I would like the arrow-up div to visible(show("slow")) when not on top of the page"

I do that this way:

http://jsfiddle.net/wf_4/GubeC/

SCRIPT:

// fade in #back-top
$(function () {
    $(window).scroll(function () {
        if ($(this).scrollTop() > 300) {
            $('.back-top').fadeIn();
        } else {
            $('.back-top').fadeOut();
        }
    });

    // scroll body to 0px on click
    $('.back-top').click(function () {
        $('body,html').animate({
            scrollTop: 0
        }, 1600);
        return false;
    });
});

CSS

.back-top {
    width:25px;
    height:25px;
    background:#ff0000;
    position:fixed;
    bottom:68px;
    right:5px;
    display:none;
    opacity:0.8;
}

HTML

<div class="back-top" title="Top of Page"></div>       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM