【问题标题】:Scroll to top button showing when not on top of the page滚动到顶部按钮,当不在页面顶部时显示
【发布时间】:2015-03-04 16:47:42
【问题描述】:

我想使用 angular bootstrap 在我的页面中固定一个滚动到顶部的箭头。 目前我有

<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>

但是,当单击“滚动到顶部”时,它仅在第一次起作用,因为 url 更改为 ...#search-bar 并且当您再次单击它时没有任何反应。那么如何在不更改 url 的情况下滚动到顶部呢?

还有问题我如何让“滚动到顶部”仅在搜索栏未显示时显示?

我正在考虑使用 $anchorScroll 并在 li 上使用 id'ed 数字,如果它高于“element-3”然后显示按钮,但不确定这是否可行。

更新: 我正在考虑遵循this 示例,即使用#search 和#results 导航栏,并使#search href 在#results 活动和#results 上可见。

【问题讨论】:

  • 只使用# 应该会让你回到顶部,而不是链接中的#search-bar
  • 你确定吗?我添加了另一个指向“#”的链接,如果我使用第一个“#search-bar”链接,向下滚动并使用“#”然后它会向上,但第一次使用时不会。

标签: javascript html css angularjs twitter-bootstrap


【解决方案1】:

你也可以不使用 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"
                    });
                });
            }
        }
    }

});

【讨论】:

    【解决方案2】:

    您可以这样做。创建一个按钮:

    <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;
        });
    
    });
    

    你可以找到一个演示here。提供的来源取自here

    【讨论】:

    • 对不起,我对 Javascript 答案不感兴趣
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多