我建议您使用所需的功能扩展 ScrollSpy,例如 https://stackoverflow.com/a/13460392/1407478
带有可排除 ID 的 ScrollSpy 扩展:
// save the original function object
var _superScrollSpy = $.fn.scrollspy;
// add a array of id's that need to be excluded
$.extend( _superScrollSpy.defaults, {
excluded_ids : []
});
// create a new constructor
var ScrollSpy = function(element, options) {
_superScrollSpy.Constructor.apply( this, arguments )
}
// extend prototypes and add a super function
ScrollSpy.prototype = $.extend({}, _superScrollSpy.Constructor.prototype, {
constructor: ScrollSpy
, _super: function() {
var args = $.makeArray(arguments)
// call bootstrap core
_superScrollSpy.Constructor.prototype[args.shift()].apply(this, args)
}
, activate: function (target) {
//if target is on our exclusion list, prevent the scrollspy to activate
if ($.inArray(target, this.options.excluded_ids)>-1) {
return
}
this._super('activate', target)
}
});
// override the old initialization with the new constructor
$.fn.scrollspy = $.extend(function(option) {
var args = $.makeArray(arguments),
option = args.shift()
//this runs everytime element.scrollspy() is called
return this.each(function() {
var $this = $(this)
var data = $this.data('scrollspy'),
options = $.extend({}, _superScrollSpy.defaults, $this.data(), typeof option == 'object' && option)
if (!data) {
$this.data('scrollspy', (data = new ScrollSpy(this, options)))
}
if (typeof option == 'string') {
data[option].apply( data, args )
}
});
}, $.fn.scrollspy);
对于http://twitter.github.com/bootstrap/javascript.html#scrollspy 的示例,如果您希望 ScrollSpy 阻止显示 #mdo,则应该像这样初始化它
$(".scrollspy-example").scrollspy({
excluded_ids : ['#mdo']
});
你可以尝试将代码放在一个单独的文件中,scrollspy-addon.js,包含它并初始化你的scrollspy
$("#your-scrollspy-id").scrollspy({
excluded_ids : ['#login']
});