简体   繁体   English

jQuery单击时滚动到元素,但如果存在页面加载

[英]jQuery scroll to element on click but also page load if present

so my on click event to scroll down to the hashtag element class worked fine but i noticed if i requested from another page it did not scroll so i added window load which works lovely but now with both bits of code together the on load scrolls if a hashtag url is present but click no longer works... does anyone have a better solution for these to both work? 所以我的on click事件向下滚动到hashtag元素类工作正常,但是我注意到如果我从另一个页面请求它不会滚动,所以我添加了窗口加载,该加载效果很好,但现在同时包含两个代码位,如果标签网址已存在,但单击不再起作用...是否有人有更好的解决方案来使它们同时起作用?

JS JS

// Scroll To # Links
    $('a[href^="#"]').on('click', function(e) {
        if (!$(this).hasClass("menu-button")) {

        e.preventDefault();

        var target = this.hash;
        target = target.replace('#', '');
        var $target = $('.' + target);
            $('html, body').stop().animate({
                    'scrollTop': $target.offset().top
                }, 700, 'swing', function() {

                window.location.hash = '#' + target;
            });
        }

    });

    // Scroll on load if #hashtag set in URL
    $(window).load(function(e){
        // Remove the # from the hash, as different browsers may or may not include it
        var loadTarget = location.hash.replace('#','');

        if(loadTarget != ''){

            e.preventDefault();
            // Clear the hash in the URL
            // location.hash = '';   // delete front "//" if you want to change the address bar
            var $loadTarget = $('.' + loadTarget);
            $('html, body').stop().animate({
                    'scrollTop': $loadTarget.offset().top
                }, 700, 'swing', function() {

                window.location.hash = '#' + loadTarget;
            });

        }
    });

HTML: HTML:

<a href="#test">test link</a>

<div class="test" style="margin-top: 1000px;">
  testing content
</div>

I managed to get the code working for myself so posting just in case someone else every needs such a similar solution: 我设法使代码适合自己,因此发布以防万一其他人都需要这样的解决方案:

JS: JS:

// Scroll To # Links
    $('a[href^="#"], a[href^="/#"]').on('click', function(e) {
        if (!$(this).hasClass("menu-button")) {

        e.preventDefault();

        var target = this.hash;
            target = target.replace('#', '');
        var $target = $('.' + target);
            $('html, body').animate({
                    'scrollTop': $target.offset().top
                }, 700, 'swing', function() {

                window.location.hash = '#' + target;
            });
        }

    });

    $(window).load(function(e){
        // scrollTo if #_ found
        var target = window.location.hash,
            target = target.replace('#', '');
        var $target = $('.' + target);
        console.log(target);
        if (target) {
            $('html, body').animate({
                    'scrollTop': $target.offset().top
                }, 700, 'swing', function() {

                window.location.hash = '#' + target;
            });
        }
    });

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

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