简体   繁体   中英

Scroll to top button showing when not on top of the page

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.

You can do this without jQuery as well:

function filterPath(string) {
    return string
        .replace(/^\//, '')
        .replace(/(index|default).[a-zA-Z]{3,4}$/, '')
        .replace(/\/$/, '');
}

const locationPath = filterPath(location.pathname);

document.querySelectorAll('a[href*="#"]').forEach(link => {
    let thisPath = filterPath(link.pathname) || locationPath;

    if ((location.hostname == link.hostname || !link.hostname)
        && (locationPath == thisPath)
    ) {
        let hashName = link.hash.replace(/#/, '');

        if (hashName) {
            let targetEl = document.getElementById(hashName);

            if (targetEl) {
                link.addEventListener('click', () => {
                    event.preventDefault();
                    targetEl.scrollIntoView({
                        top: 50,
                        left: 0,
                        behavior: "smooth"
                    });
                });
            }
        }
    }

});

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 .

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