3.5.5.1. 在 Drupal Views 中集成 jQuery OwlCarousel
在上一篇文章中,我们使用 Views Slideshow 创建了一个幻灯片,但分页器(paginator)仍然没有样式化,并且缩略图显示较小。
在本文中,我们将学习如何添加 OwlCarousel 并通过附加库引入 CSS 样式。
本文中,我们使用一个自定义模块来加载 OwlCarousel 库;在下一篇文章中,我们将演示如何在主题文件中美化幻灯片。
https://drupalbook.org/sites/default/files/inline-images/drupalbook_owlcarousel.zip
关于如何创建幻灯片的内容,请参考前一篇文章:
3.5.5. Views slideshow - 通过 Views 输出 jQuery 幻灯片与轮播。
添加自定义模块后的最终效果如下:
创建自定义模块
首先在所有模块的目录中创建我们的模块文件夹:
/modules/custom
然后创建模块信息文件:
/modules/custom/drupalbook_owlcarousel/drupalbook_owlcarousel.info.yml
适用于 Drupal 8.8 及以下版本:
name: Drupalbook OwlCarousel
description: Adds styles to Views pager
type: module
core: 8.x
package: Drupalbook
适用于 Drupal 8.8+
name: Drupalbook OwlCarousel
description: Adds styles to Views pager
type: module
core_version_requirement: ^8 || ^9
package: Drupalbook
我们稍后会更详细地讨论模块开发,现在我们先了解如何在自定义模块中添加库。
- name: 模块名称(任意)
- description: 模块描述
- type: 指定类型(module / theme)
- core: 指定适用的 Drupal 核心版本
- package: 模块所属分组
添加 OwlCarousel 库
现在我们可以启用模块,但在此之前,需要添加 OwlCarousel 库:
https://owlcarousel2.github.io/OwlCarousel2/
我将 OwlCarousel 库放在此路径下:
\modules\custom\drupalbook_owlcarousel\js\owlcarousel
同时添加一个自定义 JavaScript 文件,用于初始化 OwlCarousel:
\modules\custom\drupalbook_owlcarousel\js\drupalbook_owlcarousel.js
(function ($) {
Drupal.behaviors.drupalBookOwlCarousel = {
attach: function (context, settings) {
$('.view-slideshow #widget_pager_bottom_slideshow-block_1').owlCarousel({
items: 5,
margin: 5,
nav: true,
responsiveClass: true,
responsive: {
0: { items: 3 },
600: { items: 4 },
1000: { items: 5 }
}
});
$('.view-slideshow #widget_pager_bottom_slideshow-block_1').addClass('owl-carousel');
}
};
})(jQuery);
OwlCarousel 会作用于包裹幻灯片图片的公共容器上。我们在该容器上添加了 owl-carousel
类,以便启用 OwlCarousel 的默认样式。
此外,在 responsive
选项中,我们可以为不同的屏幕宽度设置每页显示的项目数量:
- 0–600px:3 个项目
- 600–1000px:4 个项目
- 1000px 及以上:5 个项目
添加自定义 CSS 样式
创建一个额外的 CSS 文件来美化幻灯片:
\modules\custom\drupalbook_owlcarousel\css\drupalbook_owlcarousel.css
.view-slideshow #widget_pager_bottom_slideshow-block_1 {
max-width: 480px;
position: relative;
}
.view-slideshow #widget_pager_bottom_slideshow-block_1 .owl-prev {
position: absolute;
left: 5px;
top: 30px;
opacity: 0.7;
background: url(../images/little-left.png) center center no-repeat;
width: 32px;
padding-top: 32px;
height: 0;
overflow: hidden;
display: block;
border: 0;
}
.view-slideshow #widget_pager_bottom_slideshow-block_1 .owl-prev:hover {
opacity: 1;
}
.view-slideshow #widget_pager_bottom_slideshow-block_1 .owl-next {
position: absolute;
right: 5px;
top: 30px;
opacity: 0.7;
background: url(../images/little-right.png) center center no-repeat;
width: 32px;
padding-top: 32px;
height: 0;
overflow: hidden;
display: block;
border: 0;
}
.view-slideshow #widget_pager_bottom_slideshow-block_1 .owl-next:hover {
opacity: 1;
}
.view-slideshow .views-slideshow-controls-bottom {
max-width: 480px;
}
.views_slideshow_pager_field_item.active img {
border: 1px solid #0000ff;
}
.views_slideshow_controls_text_pause {
display: none;
}
.view-slideshow .view-content {
position: relative;
max-width: 480px;
}
#views_slideshow_controls_text_previous_slideshow-block_1 {
position: absolute;
top: 155px;
left: 10px;
z-index: 99;
}
#views_slideshow_controls_text_previous_slideshow-block_1 a {
background: url(../images/left.png) center center no-repeat;
width: 42px;
padding-top: 42px;
height: 0;
overflow: hidden;
display: block;
border: 0;
}
#views_slideshow_controls_text_next_slideshow-block_1 {
position: absolute;
top: 155px;
right: 10px;
z-index: 99;
}
#views_slideshow_controls_text_next_slideshow-block_1 a {
background: url(../images/right.png) center center no-repeat;
width: 42px;
padding-top: 42px;
height: 0;
overflow: hidden;
display: block;
border: 0;
}
在 Drupal 中注册库文件
创建库文件以注册 CSS 和 JS 资源:
\modules\custom\drupalbook_owlcarousel\drupalbook_owlcarousel.libraries.yml
drupalbook_owlcarousel:
css:
theme:
js/owlcarousel/assets/owl.carousel.css: {}
js/owlcarousel/assets/owl.theme.default.min.css: {}
css/drupalbook_owlcarousel.css: {}
js:
js/owlcarousel/owl.carousel.min.js: {}
js/drupalbook_owlcarousel.js: {}
dependencies:
- core/jquery
在 dependencies 中指定 core/jquery
,以确保 jQuery 可用。我们加载 OwlCarousel 的 CSS/JS 文件(owl.carousel.css
、owl.theme.default.min.css
、owl.carousel.min.js
)后,再加载自定义的 drupalbook_owlcarousel.css
与 drupalbook_owlcarousel.js
,这样既能使用库功能,又能覆盖样式。
在模块中加载库
创建模块文件:
\modules\custom\drupalbook_owlcarousel\drupalbook_owlcarousel.module
/**
* Implements hook_preprocess_views_view().
*/
function drupalbook_owlcarousel_preprocess_views_view(&$variables) {
if ($variables['view']->storage->id() == 'slideshow') {
$variables['#attached']['library'][] = 'drupalbook_owlcarousel/drupalbook_owlcarousel';
}
}
在 if
条件中指定视图的机器名称(例如 “slideshow”)。库名称格式为:模块名/库名
。这里模块名为 drupalbook_owlcarousel
,库名与模块名相同(可自定义)。
清除缓存后,我们的样式和 JS 就会生效。
如果选择器名称不同,请修改相应的 CSS 或 JS 文件。
若仍有问题,可以留言讨论。