gpt4 book ai didi

javascript - 如何使用 Bootstrap 和 Drupal 覆盖 JS 文件?

转载 作者:行者123 更新时间:2023-12-03 02:40:09 24 4
gpt4 key购买 nike

我有一个包含“Drupal 8”和“Bootstrap 3”的网站。我通过将“Bootstrap”主题的collapse.js 文件复制到我的子主题中来自定义“Collapse”菜单的行为。

我的问题:

我复制整个文件,有什么方法可以通过仅复制自定义代码段来覆盖 JS 文件吗?

这是文件 bootstrap_subtheme_front_office.libraries.yml 的内容:

global-styling:
css:
theme:
fonts/font-awesome/css/font-awesome.css: {}
# bootstrap/dist/css/bootstrap.css: {}
# css/bootstrap-cosmo.css: {}
css/style.css: {}
# css/style-noel.css: {}
# css/style-nouvel-an.css: {}

bootstrap-scripts:
js:
bootstrap/js/affix.js: {}
bootstrap/js/alert.js: {}
bootstrap/js/button.js: {}
bootstrap/js/carousel.js: {}
bootstrap/js/collapse.js: {}
# bootstrap/js/dropdown.js: {}
bootstrap/js/modal.js: {}
bootstrap/js/tooltip.js: {}
bootstrap/js/popover.js: {}
bootstrap/js/scrollspy.js: {}
bootstrap/js/tab.js: {}
bootstrap/js/transition.js: {}
js/tour.js: {}
js/collapse.js: {}

这是文件 crash.js 的内容:

/* ========================================================================
* Bootstrap: collapse.js v3.3.7
* http://getbootstrap.com/javascript/#collapse
* ========================================================================
* Copyright 2011-2016 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */

/* jshint latedef: false */

+function ($) {
'use strict';

// COLLAPSE PUBLIC CLASS DEFINITION
// ================================

var Collapse = function (element, options) {
this.$element = $(element)
this.options = $.extend({}, Collapse.DEFAULTS, options)
this.$trigger = $('[data-toggle="collapse"][href="#' + element.id + '"],' +
'[data-toggle="collapse"][data-target="#' + element.id + '"]')
this.transitioning = null

if (this.options.parent) {
this.$parent = this.getParent()
} else {
this.addAriaAndCollapsedClass(this.$element, this.$trigger)
}

if (this.options.toggle) this.toggle()
}

Collapse.VERSION = '3.3.7'

Collapse.TRANSITION_DURATION = 350

Collapse.DEFAULTS = {
toggle: true
}

Collapse.prototype.dimension = function () {
var hasWidth = this.$element.hasClass('width')
return hasWidth ? 'width' : 'height'
}

Collapse.prototype.show = function () {
if (this.transitioning || this.$element.hasClass('in')) return

var activesData
var actives = this.$parent && this.$parent.children('.panel').children('.in, .collapsing')

if (actives && actives.length) {
activesData = actives.data('bs.collapse')
if (activesData && activesData.transitioning) return
}

var startEvent = $.Event('show.bs.collapse')
this.$element.trigger(startEvent)
if (startEvent.isDefaultPrevented()) return

if (actives && actives.length) {
Plugin.call(actives, 'hide')
activesData || actives.data('bs.collapse', null)
}

var dimension = this.dimension()

this.$element
.removeClass('collapse')
.addClass('collapsing')[dimension](0)
.attr('aria-expanded', true)

this.$trigger
.removeClass('collapsed')
.attr('aria-expanded', true)

this.transitioning = 1

var complete = function () {
this.$element
.removeClass('collapsing')
.addClass('collapse in')[dimension]('')
this.transitioning = 0
this.$element
.trigger('shown.bs.collapse')
}

if (!$.support.transition) return complete.call(this)

var scrollSize = $.camelCase(['scroll', dimension].join('-'))

this.$element
.one('bsTransitionEnd', $.proxy(complete, this))
.emulateTransitionEnd(Collapse.TRANSITION_DURATION)[dimension](this.$element[0][scrollSize])
}

Collapse.prototype.hide = function () {
if (this.transitioning || !this.$element.hasClass('in')) return

var startEvent = $.Event('hide.bs.collapse')
this.$element.trigger(startEvent)
if (startEvent.isDefaultPrevented()) return

var dimension = this.dimension()

this.$element[dimension](this.$element[dimension]())[0].offsetHeight

this.$element
.addClass('collapsing')
.removeClass('collapse in')
.attr('aria-expanded', false)

this.$trigger
.addClass('collapsed')
.attr('aria-expanded', false)

this.transitioning = 1

var complete = function () {
this.transitioning = 0
this.$element
.removeClass('collapsing')
.addClass('collapse')
.trigger('hidden.bs.collapse')
}

if (!$.support.transition) return complete.call(this)

this.$element
[dimension](0)
.one('bsTransitionEnd', $.proxy(complete, this))
.emulateTransitionEnd(Collapse.TRANSITION_DURATION)
}

Collapse.prototype.toggle = function () {
this[this.$element.hasClass('in') ? 'hide' : 'show']()
}

Collapse.prototype.getParent = function () {
return $(this.options.parent)
.find('[data-toggle="collapse"][data-parent="' + this.options.parent + '"]')
.each($.proxy(function (i, element) {
var $element = $(element)
this.addAriaAndCollapsedClass(getTargetFromTrigger($element), $element)
}, this))
.end()
}

Collapse.prototype.addAriaAndCollapsedClass = function ($element, $trigger) {
var isOpen = $element.hasClass('in')

$element.attr('aria-expanded', isOpen)
$trigger
.toggleClass('collapsed', !isOpen)
.attr('aria-expanded', isOpen)
}

function getTargetFromTrigger($trigger) {
var href
var target = $trigger.attr('data-target')
|| (href = $trigger.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') // strip for ie7

return $(target)
}


// COLLAPSE PLUGIN DEFINITION
// ==========================

function Plugin(option) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.collapse')
var options = $.extend({}, Collapse.DEFAULTS, $this.data(), typeof option == 'object' && option)

if (!data && options.toggle && /show|hide/.test(option)) options.toggle = false
if (!data) $this.data('bs.collapse', (data = new Collapse(this, options)))
if (typeof option == 'string') data[option]()
})
}

var old = $.fn.collapse

$.fn.collapse = Plugin
$.fn.collapse.Constructor = Collapse


// COLLAPSE NO CONFLICT
// ====================

$.fn.collapse.noConflict = function () {
$.fn.collapse = old
return this
}


// COLLAPSE DATA-API
// =================

$(document).on('click.bs.collapse.data-api', '[data-toggle="collapse"]', function (e) {
var $this = $(this)

if (!$this.attr('data-target')) e.preventDefault()

var $target = getTargetFromTrigger($this)
var data = $target.data('bs.collapse')
var option = data ? 'toggle' : $this.data()

Plugin.call($target, option)
})

}(jQuery);

我在末尾添加了以下代码(我的自定义代码):

$('#navbar-collapse-first').on('show.bs.collapse', function () {
$('#navbar-collapse-second').collapse('hide');
})

$('#navbar-collapse-second').on('show.bs.collapse', function () {
$('#navbar-collapse-first').collapse('hide');
})

$('#navbar-collapse-first').on('show.bs.collapse', function () {
$('body').addClass('overlay-is-navbar-collapse');
});

$('#navbar-collapse-first').on('hide.bs.collapse', function () {
$('body').removeClass('overlay-is-navbar-collapse');
});

$('#navbar-collapse-second').on('show.bs.collapse', function () {
$('body').addClass('overlay-is-navbar-collapse');
});

$('#navbar-collapse-second').on('hide.bs.collapse', function () {
$('body').removeClass('overlay-is-navbar-collapse');
});

UPDATE : Here is the correct code

(function ($) {
var $document = $(document);
var $body = $(document.body);

// Wrap everything in a DOM ready handler.
$document.ready(function () {

// Save the navbar collapse selectors so making updates/tracking easier.
var navbarCollapseFirst = '#navbar-collapse-first';
var navbarCollapseSecond = '#navbar-collapse-second';
var navbarCollapseBoth = navbarCollapseFirst + ',' + navbarCollapseSecond;

// Save the jQuery instances (for performance).
var $navbarCollapseFirst = $(navbarCollapseFirst);
var $navbarCollapseSecond = $(navbarCollapseSecond);

// Variable for saving which navbar collapse is currently open.
var $open = $();

// For performance reasons, bind evens directly on the document. jQuery
// allows you to pass a targeting selector between the event and handler
// so it will only call said handler when the event matches said selector.
$document
// Bind "show" event for first navbar collapse.
.on('show.bs.collapse', navbarCollapseBoth, function (e) {
// Indicate that the first is open.
$open = $(e.target);

// Collapse the first if it's not the one that just opened.
if (!$navbarCollapseFirst.is($open)) {
$navbarCollapseFirst.collapse('hide');
}
// Collapse the second if it's not the one that just opened.
else if (!$navbarCollapseSecond.is($open)) {
$navbarCollapseSecond.collapse('hide');
}

// Add the body class.
$body.addClass('overlay-is-navbar-collapse');
})
// Bind "hide" event for first navbar collapse.
.on('hide.bs.collapse', navbarCollapseFirst, function (e) {
// Indicate that the first is open.
var $hide = $(e.target);

// Remove the first as the opened navbar collapse.
if ($navbarCollapseFirst.is($hide) && $navbarCollapseFirst.is($open)) {
$open = $();
}
// Remove the second as the opened navbar collapse.
else if ($navbarCollapseSecond.is($hide) && $navbarCollapseSecond.is($open)) {
$open = $();
}

// Remove the body class if there is no open navbar collapse.
if (!$open[0]) {
$body.removeClass('overlay-is-navbar-collapse');
}
});
});

})(window.jQuery);

最佳答案

我认为你必须将代码包装在 document.ready 函数中,在你自己的 js 文件中,如下所示:

$(function(){
$('#navbar-collapse-first').on('show.bs.collapse', function () {
$('#navbar-collapse-second').collapse('hide');
})

$('#navbar-collapse-second').on('show.bs.collapse', function () {
$('#navbar-collapse-first').collapse('hide');
})

$('#navbar-collapse-first').on('show.bs.collapse', function () {
$('body').addClass('overlay-is-navbar-collapse');
});

$('#navbar-collapse-first').on('hide.bs.collapse', function () {
$('body').removeClass('overlay-is-navbar-collapse');
});

$('#navbar-collapse-second').on('show.bs.collapse', function () {
$('body').addClass('overlay-is-navbar-collapse');
});

$('#navbar-collapse-second').on('hide.bs.collapse', function () {
$('body').removeClass('overlay-is-navbar-collapse');
});
});

关于javascript - 如何使用 Bootstrap 和 Drupal 覆盖 JS 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48362501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com