如何在jquery脚本中隐藏url中的#href

时间:2018-04-20 12:17:39

标签: javascript jquery

我使用此脚本(与bootstrap结合使用)制作动画滚动到单个页面网站的各个部分。 我想在地址栏中隐藏href的外观。

HTML NAV

        <nav class="collapse navbar-collapse navbar-right" role="navigation" data-toggle="collapse" data-target=".navbar-collapse">
            <ul id="nav" class="nav navbar-nav">
                <li class="current"><a href="#body">Home</a></li>
                <li><a href="#1">1</a></li>
                <li><a href="#2">2</a></li>
                <li><a href="#3">3</a></li>
            </ul>
        </nav>

的jQuery

if (typeof Object.create !== "function") {
    Object.create = function(e) {
        function t() {}
        t.prototype = e;
        return new t
    }
}(function(e, t, n, r) {
    "use strict";
    var i = {
        init: function(n, r) {
            this.options = e.extend({}, e.fn.singlePageNav.defaults, n);
            this.container = r;
            this.$container = e(r);
            this.$links = this.$container.find("a");
            if (this.options.filter !== "") {
                this.$links = this.$links.filter(this.options.filter)
            }
            this.$window = e(t);
            this.$htmlbody = e("html, body");
            this.$links.on("click.singlePageNav", e.proxy(this.handleClick, this));
            this.didScroll = false;
            this.checkPosition();
            this.setTimer()
        },
        handleClick: function(t) {
            var n = this,
                r = t.currentTarget,
                i = e(r.hash);
            t.preventDefault();
            if (i.length) {
                n.clearTimer();
                if (typeof n.options.beforeStart === "function") {
                    n.options.beforeStart()
                }
                n.setActiveLink(r.hash);
                n.scrollTo(i, function() {
                    if (n.options.updateHash && history.pushState) {
                        history.pushState(null, null, r.hash)
                    }
                    n.setTimer();
                    if (typeof n.options.onComplete === "function") {
                        n.options.onComplete()
                    }
                })
            }
        },
        scrollTo: function(e, t) {
            var n = this;
            var r = n.getCoords(e).top;
            var i = false;
            n.$htmlbody.stop().animate({
                scrollTop: r
            }, {
                duration: n.options.speed,
                easing: n.options.easing,
                complete: function() {
                    if (typeof t === "function" && !i) {
                        t()
                    }
                    i = true
                }
            })
        },
        setTimer: function() {
            var e = this;
            e.$window.on("scroll.singlePageNav", function() {
                e.didScroll = true
            });
            e.timer = setInterval(function() {
                if (e.didScroll) {
                    e.didScroll = false;
                    e.checkPosition()
                }
            }, 250)
        },
        clearTimer: function() {
            clearInterval(this.timer);
            this.$window.off("scroll.singlePageNav");
            this.didScroll = false
        },
        checkPosition: function() {
            var e = this.$window.scrollTop();
            var t = this.getCurrentSection(e);
            this.setActiveLink(t)
        },
        getCoords: function(e) {
            return {
                top: Math.round(e.offset().top) - this.options.offset
            }
        },
        setActiveLink: function(e) {
            var t = this.$container.find("a[href$='" + e + "']");
            if (!t.hasClass(this.options.currentClass)) {
                this.$links.removeClass(this.options.currentClass);
                t.addClass(this.options.currentClass)
            }
        },
        getCurrentSection: function(t) {
            var n, r, i, s;
            for (n = 0; n < this.$links.length; n++) {
                r = this.$links[n].hash;
                if (e(r).length) {
                    i = this.getCoords(e(r));
                    if (t >= i.top - this.options.threshold) {
                        s = r
                    }
                }
            }
            return s || this.$links[0].hash
        }
    };
    e.fn.singlePageNav = function(e) {
        return this.each(function() {
            var t = Object.create(i);
            t.init(e, this)
        })
    };
    e.fn.singlePageNav.defaults = {
        offset: 0,
        threshold: 120,
        speed: 400,
        currentClass: "current",
        easing: "swing",
        updateHash: false,
        filter: "",
        onComplete: false,
        beforeStart: false
    }
})(jQuery, window, document)

我想获得与此脚本相同的结果(但使用动画):

 $('a').each(function(){  
 $(this).attr('onclick','window.location.href="'+$(this).attr('href')+'"');
 $(this).attr('href','#');
 });

有可能得到我想要的东西吗?

0 个答案:

没有答案