Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 滚动至顶部按钮,当不在页面顶部时显示_Javascript_Html_Css_Angularjs_Twitter Bootstrap - Fatal编程技术网

Javascript 滚动至顶部按钮,当不在页面顶部时显示

Javascript 滚动至顶部按钮,当不在页面顶部时显示,javascript,html,css,angularjs,twitter-bootstrap,Javascript,Html,Css,Angularjs,Twitter 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 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更改为…#搜索栏后第一次工作,当您再次单击它时,不会发生任何事情。那么,如何在不更改url的情况下滚动到顶部呢

    还有一个问题,我如何使“滚动到顶部”仅在搜索栏未显示时显示

    我在考虑使用$anchorScroll,在li上使用id号,如果它高于“element-3”,则显示按钮,但不确定这是否有效

    更新:
    我在考虑以下示例,即使用导航栏,即“搜索”和“结果”,并使“搜索href”在“结果”上可见,在“结果”上隐藏。创建一个按钮:

    <a href="#" class="scrollToTop">Scroll To Top</a>
    
    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;
        });
    
    });
    

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

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

    只使用
    #
    可以让您返回顶部,而不是链接中的
    #搜索栏
    。您确定吗?我添加了另一个指向“#”的链接,如果我先使用“#搜索栏”链接,向下滚动并使用“#”,然后它会向上滚动,但在第一次使用时不会。很抱歉,我对Javascript答案不感兴趣
    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"
                        });
                    });
                }
            }
        }
    
    });