简体   繁体   English

滚动到顶部按钮显示不在页面顶部时

[英]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.我想使用 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.但是,当单击“滚动到顶部”时,它仅在第一次起作用,因为 url 更改为 ...#search-bar,当您再次单击它时,没有任何反应。 So how do I scroll to top without changing the url?那么如何在不更改 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.我正在考虑使用 $anchorScroll 并在 li 上使用 id 编号,如果它高于“element-3”,则显示按钮,但不确定这是否可行。

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.更新:我正在考虑遵循这个例子,即使用导航栏,即#search 和#results,并使#search href 在#results 上可见,而#results 则隐藏。

You can do this without jQuery as well:您也可以在没有 jQuery 的情况下执行此操作:

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: 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: 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 .所提供的来源取自此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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