问题描述:

I would like to have a scroll to top arrow fixed in my page using angular bootstrap.

Currently I have

<div class="col-md-1">

<div class="affix">

<div>

<a th:href="@{/}" href="#" target="_self"><img id="image" src="source" alt="yoli" width="50px" /></a>

</div>

<a href="#search-bar">Scroll to top</a>

</div>

</div>

<div class="col-md-6">

<div id="search-bar" ng-include="blabla"></div>

<li ng-repeat="something"></li>

</div>

However when the "Scroll to top" is click it only works first time since the url changes to ...#search-bar and when you click it again nothing happens. So how do I scroll to top without changing the url?

And also question how do I make the "Scroll to top" only show when the search-bar is not showing?

I was thinking about using $anchorScroll and using id'ed numbers on li and if it's higher then "element-3" then show the button, however not sure if that would work.

UPDATE:

I am thinking of following this example, that is using navigation bars that is #search and #results and make the #search href visible on #results active and #results one hidden.

网友答案:

Here is how you can do it. Create a button:

<a href="#" class="scrollToTop">Scroll To Top</a>

CSS:

.scrollToTop{
    width:100px; 
    height:130px;
    padding:10px; 
    text-align:center; 
    background: whiteSmoke;
    font-weight: bold;
    color: #444;
    text-decoration: none;
    position:fixed;
    top:75px;
    right:40px;
    display:none;
    background: url('arrow_up.png') no-repeat 0px 20px;
}
.scrollToTop:hover{
    text-decoration:none;
}

JavaScript:

$(document).ready(function(){

    //Check to see if the window is top if not then display button
    $(window).scroll(function(){
        if ($(this).scrollTop() > 100) {
            $('.scrollToTop').fadeIn();
        } else {
            $('.scrollToTop').fadeOut();
        }
    });

    //Click event to scroll to top
    $('.scrollToTop').click(function(){
        $('html, body').animate({scrollTop : 0},800);
        return false;
    });

});

You can find a demo here. The source presented is taken from here.

相关阅读:
Top