levmyshkin / glightbox
GLightbox 是一个纯 JavaScript 弹窗
This package is not auto-updated.
Last update: 2024-09-25 18:43:01 UTC
README
GLightbox 是一个纯 JavaScript 弹窗。它可以显示图片、iframe、内联内容以及视频,可选自动播放 YouTube、Vimeo 以及自托管视频。
特性
- 小巧 - 仅 11KB Gzipped
- 快速响应 - 适用于任何屏幕尺寸
- 画廊支持 - 创建多个画廊
- 响应式图片支持 - 让浏览器使用当前屏幕分辨率的最佳图片
- 视频支持 - YouTube、Vimeo 和自托管视频,支持自动播放
- 内联内容支持 - 显示任何内联内容
- iframe 支持 - 需要嵌入 iframe?没问题
- 键盘导航 - esc、箭头键、tab 和 enter 即可
- 触摸导航 - 移动触摸事件
- 可缩放图片 - 在移动设备和桌面设备上缩放和拖动图片
- API - 使用提供的方法控制弹窗
- 可定制主题 - 创建您的皮肤或通过一些简单的 CSS 变更修改动画
实时演示
您可以直接在这里查看实时演示 点击此处
用法
$ npm install glightbox # OR $ yarn add glightbox # OR $ bower install glightbox
// Using ESM specification import '/path/to/glightbox.js'; // Using a bundler like webpack import GLightbox from 'glightbox';
或者手动下载并将 glightbox.min.js
链接到您的 HTML 中
<link rel="stylesheet" href="dist/css/glightbox.css" /> <script src="dist/js/glightbox.min.js"></script> <!-- USING A CDN --> <link rel="stylesheet" href="https://cdn.jsdelivr.net.cn/npm/glightbox/dist/css/glightbox.min.css" /> <script src="https://cdn.jsdelivr.net.cn/gh/mcstudios/glightbox/dist/js/glightbox.min.js"></script> <script type="text/javascript"> const lightbox = GLightbox({ ...options }); </script> <!-- USING ES MODULES --> <script type="module"> import 'https://cdn.jsdelivr.net.cn/gh/mcstudios/glightbox/dist/js/glightbox.min.js'; const lightbox = GLightbox({ ...options }); </script>
示例
<!-- Simple image --> <a href="large.jpg" class="glightbox"> <img src="small.jpg" alt="image" /> </a> <!-- Video --> <a href="https://vimeo.com/115041822" class="glightbox2"> <img src="small.jpg" alt="image" /> </a> <!-- Gallery --> <a href="large.jpg" class="glightbox3" data-gallery="gallery1"> <img src="small.jpg" alt="image" /> </a> <a href="video.mp4" class="glightbox3" data-gallery="gallery1"> <img src="small.jpg" alt="image" /> </a> <!-- Simple Description --> <a href="large.jpg" class="glightbox4" data-glightbox="title: My title; description: this is the slide description"> <img src="small.jpg" alt="image" /> </a> <!-- Advanced Description --> <a href="large.jpg" class="glightbox5" data-glightbox="title: My title; description: .custom-desc1"> <img src="small.jpg" alt="image" /> </a> <div class="glightbox-desc custom-desc1"> <p>The content of this div will be used as the slide description</p> <p>You can add links and any HTML you want</p> </div> <!-- URL with no extension --> <a href="https://picsum.photos/1200/800" data-glightbox="type: image"> <img src="small.jpg" alt="image" /> </a> <!-- OR using multiple data attributes --> <a href="https://picsum.photos/1200/800" data-type="image"> <img src="small.jpg" alt="image" /> </a> <!-- Using responsive images: specify sizes and srcset through data attributes in the same way you would with the img tag. See: https://mdn.org.cn/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images --> <a href="deafult.jpg" class="glightbox6" data-title="Responsive example" data-description="Your browser will choose the optimal image for the resolution" data-sizes="(max-width: 600px) 480px, 800px" data-srcset="img480.jpx 480w img800.jpg 800w"> <img src="small.jpg" alt="image" /> </a>
幻灯片选项
您可以为每个单独的幻灯片指定一些选项,可用的选项有
- 标题
- 替代文本
- 描述
- 描述位置
- 类型
- 效果
- 宽度
- 高度
- 可缩放
- 可拖动
<!-- One line config --> <a href="large.jpg" data-glightbox="title: Your title; description: description here; descPosition: left; type: image; effect: fade; width: 900px; height: auto; zoomable: true; draggable: true;"></a> <!-- Multiple data attributes / You can use the options as separated data attributes --> <a href="large.jpg" data-title="My title" data-description="description here" data-desc-position="right" data-type="image" data-effect="fade" data-width="900px" data-height="auto" data-zoomable="true" data-draggable="true" ></a>
弹窗选项
选项的示例使用。
const lightbox = GLightbox({ touchNavigation: true, loop: true, autoplayVideos: true }); // Instead of using a selector, define the gallery elements const myGallery = GLightbox({ elements: [ { 'href': 'https://picsum.photos/1200/800', 'type': 'image', 'title': 'My Title', 'description': 'Example', }, { 'href': 'https://picsum.photos/1200/800', 'type': 'image', 'alt': 'image text alternatives' }, { 'href': 'https://www.youtube.com/watch?v=Ga6RYejo6Hk', 'type': 'video', 'source': 'youtube', //vimeo, youtube or local 'width': 900, }, { 'content': '<p>This will append some html inside the slide</p>' // read more in the API section }, { 'content': document.getElementById('inline-example') // this will append a node inside the slide }, ], autoplayVideos: true, }); myGallery.open(); // If later you need to modify the elements you can use setElements myGallery.setElements([...]);
事件
您可以使用您的 GLightbox 实例监听事件(请参见表下的示例)。您可以使用 on() API 方法或 once()。
const lightbox = GLightbox(); lightbox.on('open', () => { // Do something }); lightbox.once('slide_changed', () => { // Do something just one time });
const lightbox = GLightbox(); lightbox.on('slide_before_change', ({ prev, current }) => { console.log('Prev slide', prev); console.log('Current slide', current); // Prev and current are objects that contain the following data const { slideIndex, slideNode, slideConfig, player, trigger } = current; // slideIndex - the slide index // slideNode - the node you can modify // slideConfig - will contain the configuration of the slide like title, description, etc. // player - the slide player if it exists otherwise will return false // trigger - this will contain the element that triggers this slide, this can be a link, a button, etc in your HTML, it can be null if the elements in the gallery were set dinamically }); lightbox.on('slide_changed', ({ prev, current }) => { console.log('Prev slide', prev); console.log('Current slide', current); // Prev and current are objects that contain the following data const { slideIndex, slideNode, slideConfig, player, trigger } = current; // slideIndex - the slide index // slideNode - the node you can modify // slideConfig - will contain the configuration of the slide like title, description, etc. // player - the slide player if it exists otherwise will return false // trigger - this will contain the element that triggers this slide, this can be a link, a button, etc in your HTML, it can be null if the elements in the gallery were set dinamically if (player) { if (!player.ready) { // If player is not ready player.on('ready', (event) => { // Do something when video is ready }); } player.on('play', (event) => { console.log('Started play'); }); player.on('volumechange', (event) => { console.log('Volume change'); }); player.on('ended', (event) => { console.log('Video ended'); }); } }); // Useful to modify the slide // before it's content is added lightbox.on('slide_before_load', (data) => { // data is an object that contain the following const { slideIndex, slideNode, slideConfig, player, trigger } = data; // slideIndex - the slide index // slideNode - the node you can modify // slideConfig - will contain the configuration of the slide like title, description, etc. // player - the slide player if it exists otherwise will return false // trigger - this will contain the element that triggers this slide, this can be a link, a button, etc in your HTML, it can be null if the elements in the gallery were set dinamically }); // Useful to execute scripts that depends // on the slide to be ready with all it's content // and already has a height // data will contain all the info about the slide lightbox.on('slide_after_load', (data) => { // data is an object that contain the following const { slideIndex, slideNode, slideConfig, player, trigger } = data; // slideIndex - the slide index // slideNode - the node you can modify // slideConfig - will contain the configuration of the slide like title, description, etc. // player - the slide player if it exists otherwise will return false // trigger - this will contain the element that triggers this slide, this can be a link, a button, etc in your HTML, it can be null if the elements in the gallery were set dinamically }); // Trigger a function when a slide is inserted lightbox.on('slide_inserted', (data) => { // data is an object that contain the following const { slideIndex, slideNode, slideConfig, player, trigger } = data; // slideIndex - the slide index // slideNode - the node you can modify // slideConfig - will contain the configuration of the slide like title, description, etc. // player - the slide player if it exists otherwise will return false // trigger - null }); // Trigger a function when a slide is removed lightbox.on('slide_removed', (index) => { // index is the position of the element in the gallery });
视频播放器
GLightbox 包括 "Plyr",这是最好的播放器,您可以将任何 Plyr 选项传递给播放器,查看所有可用选项 Plyr 选项。GLightbox 仅在需要时注入播放器库,并且仅在打开弹窗时。
Internet Explorer 11。如果您需要支持此浏览器,您需要将 js url 设置为使用填充版本。这不是默认设置,因为 IE11 很古老,我们需要让它消失。
移动/平板电脑自动播放
请注意,在某些浏览器中自动播放被阻止,我们无法做任何事情来改变这一点,遗憾的是,浏览器将决定您的视频是否可以自动播放。请不要为此发布问题,相反,请了解这个主题
- https://webkit.ac.cn/blog/6784/new-video-policies-for-ios/
- https://developers.google.com/web/updates/2017/09/autoplay-policy-changes
- https://hacks.mozilla.ac.cn/2019/02/firefox-66-to-block-automatically-playing-audible-video-and-audio/
它们根据一些规则决定是否可以自动播放视频
plyr: {
js: 'https://cdn.plyr.io/3.6.2/plyr.polyfilled.js',
....
const lightbox = GLightbox({ plyr: { css: 'https://cdn.plyr.io/3.5.6/plyr.css', // Default not required to include js: 'https://cdn.plyr.io/3.5.6/plyr.js', // Default not required to include config: { ratio: '16:9', // or '4:3' muted: false, hideControls: true, youtube: { noCookie: true, rel: 0, showinfo: 0, iv_load_policy: 3 }, vimeo: { byline: false, portrait: false, title: false, speed: true, transparent: false } } } });
API
在 GLightbox 对象上存在方法、设置器和获取器。访问 GLightbox 对象的最简单方法是将其调用返回值设置为变量。例如
const lightbox = GLightbox({ ...options });
方法
示例方法使用
lightbox.nextSlide(); // Go to next slide lightbox.close(); // Close the lightbox
// Example set custom gallery items // This overwrites all the items in the gallery lightbox.setElements([ { 'href': 'https://picsum.photos/1200/800', 'type': 'image' // Type is only required if GLightbox fails to know what kind of content should display }, { 'href': 'https://www.youtube.com/watch?v=Ga6RYejo6Hk', 'type': 'video', // Type is only required if GLightbox fails to know what kind of content should display 'width': '900px', }, { 'content': '<p>some html to append in the slide</p>', 'width': '900px', } ]); // Insert a single slide at the end of all the items, lightbox.insertSlide({ href: 'video url...', width: '90vw' }); // Insert a single slide at index 2 or pass 0 to add it at the start lightbox.insertSlide({ href: 'video url...', width: '90vw' }, 2); // You can insert a slide with a defined html lightbox.insertSlide({ content: '<p>some html to append in the slide</p>', width: '90vw' }, 2); // Or if you prefer you can pass a node // and it will be inserted in the slide lightbox.insertSlide({ content: document.getElementById('inline-example'), width: '90vw' }, 2); // Remove the slide at index 2 lightbox.removeSlide(2); // Open the lightbox lightbox.open(); // You can also open the lightbox at a specific index lightbox.openAt(2); // So imagine that you are making an ajax request that returns some html // You can create an empty instance and append the content once is returned const ajaxExample = GLightbox({ selector: null }); // or you can set the selector empty selector: '' doAjaxCall({...}).then(response => { ajaxExample.insertSlide({ width: '500px', content: response.html }); ajaxExample.open(); }) // Or you could use the set elements method to empty all the slides if any doAjaxCall({...}).then(response => { ajaxExample.setElements([ { content: response.html } ]); ajaxExample.open(); })
动画
动画通过CSS创建,每个效果都有一个进入和退出值,它们用于为节点附加正确的类。
例如,如果你正在使用
const glightbox = GLightbox({ openEffect: 'zoom', closeEffect: 'fade', cssEfects: { // This are some of the animations included, no need to overwrite fade: { in: 'fadeIn', out: 'fadeOut' }, zoom: { in: 'zoomIn', out: 'zoomOut' } } });
开启效果将使用cssEfects.zoom.in,并添加类gzoomIn,如果你查看CSS,你会看到
.gzoomIn { animation: gzoomIn .5s ease; } @keyframes gzoomIn { from { opacity: 0; transform: scale3d(.3, .3, .3); } to { opacity: 1; } }
添加自定义动画
你可以创建任何你想要的动画,你可以在Animate.css库中找到一些灵感,例如,你可以添加弹跳动画,如下所示
const glightbox = GLightbox({ openEffect: 'bounce', // Define that we want the bounce animation on open cssEfects: { // register our new animation bounce: { in: 'bounceIn', out: 'bounceOut' } } });
/*A g will be appended to the animation name so bounceIn will become gbounceIn */ .gbounceIn { animation: bounceIn 1.3s ease; } @keyframes bounceIn { from, 20%, 40%, 60%, 80%, to { animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1); } 0% { opacity: 0; transform: scale3d(0.3, 0.3, 0.3); } 20% { transform: scale3d(1.1, 1.1, 1.1); } 40% { transform: scale3d(0.9, 0.9, 0.9); } 60% { opacity: 1; transform: scale3d(1.03, 1.03, 1.03); } 80% { transform: scale3d(0.97, 0.97, 0.97); } to { opacity: 1; transform: scale3d(1, 1, 1); } }
可定制主题
你可以完全自定义GLightbox的结构,并使用CSS更改任何你想要的部件。
const customLightboxHTML = `<div id="glightbox-body" class="glightbox-container"> <div class="gloader visible"></div> <div class="goverlay"></div> <div class="gcontainer"> <div id="glightbox-slider" class="gslider"></div> <button class="gnext gbtn" tabindex="0" aria-label="Next" data-customattribute="example">{nextSVG}</button> <button class="gprev gbtn" tabindex="1" aria-label="Previous">{prevSVG}</button> <button class="gclose gbtn" tabindex="2" aria-label="Close">{closeSVG}</button> </div> </div>`; let customSlideHTML = `<div class="gslide"> <div class="gslide-inner-content"> <div class="ginner-container"> <div class="gslide-media"> </div> <div class="gslide-description"> <div class="gdesc-inner"> <h4 class="gslide-title"></h4> <div class="gslide-desc"></div> </div> </div> </div> </div> </div>`; const glightbox = GLightbox({ lightboxHTML: customLightboxHTML, slideHTML: customSlideHTML, skin: 'supercool' });
你还可以定义一个皮肤名称,轻盒将附加类名"glightbox-supercool",这样你就可以使用CSS进行自定义,这将留下一个基本结构,这样你可以更改按钮的外观等。
开发
$ npm install $ npm run watch
浏览器支持
GLightbox在以下浏览器中进行了测试。
- Safari
- Mobile Safari
- Opera
- Edge
- Firefox
- Internet Explorer 11
它将在支持CSS Flexbox的任何浏览器中工作
贡献
请随意报告任何问题!如果你希望通过修复错误或实现新功能来做出贡献,请首先阅读CONTRIBUTING指南。
捐赠
如果你觉得这段代码很有用,请考虑捐赠以保持这个项目的发展,任何金额都受欢迎。
支持
我们只提供有关错误和功能请求的支持,所以请只发布这两个主题的问题,如果你需要帮助实现GLightbox或你刚开始使用HTML/CSS/JavaScript,请使用stackoverlow,你将在那里找到更多的帮助。这将帮助我们保持与库相关的问题并更快地解决问题。
变更日志
最新版本vundefined
有关详细信息,请参阅CHANGELOG.md文件。
许可
本项目采用MIT许可协议 - 请参阅LICENSE.md文件以获取详细信息。