Я использую fancyBox 2.1.5, и у меня есть 3 селектора, которые подходят для видео и изображений по-разному. Они немного отличаются друг от друга, поэтому есть много дублированного кода.Как предотвратить дублирование кода в моей инициализации fancyBox?
Вот как это выглядит:
// Fit video content to display area, ignoring title text.
$('.fitVideo').fancybox({
helpers: {
// Enable the media helper to better handle video.
media: true,
// Put comments within the white border.
title: {
type: 'inside'
}
},
// Do not use this because it tries to fit title text as well.
fitToView: false,
// Prevent the introduction of black bars when resized for mobile.
aspectRatio: true,
// Restrict content to the display dimensions.
maxWidth: "100%",
maxHeight: "100%",
// Change the title keyword to 'caption' to avoid title text in tooltips.
beforeLoad: function() {
this.title = $(this.element).attr('caption');
},
// Override the default iframe dimensions with manually set dimensions.
afterLoad: function() {
this.width = $(this.element).data("width");
this.height = $(this.element).data("height");
}
});
// Fit image content to display area, ignoring title text.
$('.fitImage').fancybox({
helpers: {
// Put comments within the white border.
title: {
type: 'inside'
}
},
// Do not use this because it tries to fit title text as well.
fitToView: false,
// Restrict content to the display dimensions.
maxWidth: "100%",
maxHeight: "100%",
// Change the title keyword to 'caption' to avoid title text in tooltips.
beforeLoad: function() {
this.title = $(this.element).attr('caption');
}
});
// Only fit image content to display width; content extends beyond the bottom of the page.
$('.fitImageWidth').fancybox({
helpers: {
// Put comments within the white border.
title: {
type: 'inside'
}
},
// Do not use this because it tries to fit title text as well.
fitToView: false,
// Only restrict content to the display width.
maxWidth: "100%",
// Change the title keyword to 'caption' to avoid title text in tooltips.
beforeLoad: function() {
this.title = $(this.element).attr('caption');
}
});
Все они имеют одни и те же значения для title
, type
, fitToView
, maxWidth
и beforeLoad
, так что я надеялся, что я мог бы, возможно, поместить эти свойства в $('.fancybox')
догоняющий все, а затем имеют небольшие селектора с уникальными свойствами, которые «стекают» поверх этих значений по умолчанию.
Это может быть неправильный подход, хотя ... Итак, что лучший способ сделать это?
Спасибо PSL, ваше предложение действительно работает, но оказалось, что весь мой подход на самом деле сломал элементы группы fancyBox. Я не уверен, что этикета StackOverflow диктует в этой ситуации, но с моим вопросом, связанным конкретно с fancyBox, я склонен опубликовать свой окончательный рабочий подход в качестве ответа. Исправьте меня, если это неправильно! – calbar
@calbar Конечно, вы должны опубликовать сообщение, если считаете, что лучше подходит к вашему вопросу ... И вы можете принять его также (я думаю, через несколько часов или около того). – PSL