


JavaScript implementation to support mobile device gallery_javascript tips
This is a gallery effect that does not rely on any js framework and is implemented in pure javascript. It supports mobile device gesture operations, such as gesture touch sliding, zooming in and closing pictures, and it also supports keyboard operations on PC. In short, it is an indispensable picture gallery plug-in for WEB developers. It is called photoswipe.
The display renderings are as follows:
View demo Download source code
HTML
First load the required CSS and js files.
<link rel="stylesheet" href="css/photoswipe.css"> <link rel="stylesheet" href="css/default-skin/default-skin.css"> <script src="js/photoswipe.min.js"></script> <script src="js/photoswipe-ui-default.min.js"></script>
Don’t worry about the above files, Moonlight has been packaged, you can just download and use them.
Next, prepare the HTML part in the body. We prepare a picture thumbnail on the page. When this thumbnail is clicked, the corresponding large album will pop up. We are ready
HTML structure is as follows:
<div id="photos"> <img src="images/s1_m.jpg" alt="Image description" /> <p>图集</p> </div>
Now, the important gallery display part will provide the structure for the large image display. Note that the elements in the following code: .pswp__bg, .pswp__scroll-wrap, .pswp__container and .pswp__item cannot be changed.
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true"> <div class="pswp__bg"></div> <div class="pswp__scroll-wrap"> <div class="pswp__container"> <div class="pswp__item"></div> <div class="pswp__item"></div> <div class="pswp__item"></div> </div> <div class="pswp__ui pswp__ui--hidden"> <div class="pswp__top-bar"> <div class="pswp__counter"></div> <button class="pswp__button pswp__button--close" title="Close (Esc)"></button> <button class="pswp__button pswp__button--share" title="Share"></button> <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button> <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button> <div class="pswp__preloader"> <div class="pswp__preloader__icn"> <div class="pswp__preloader__cut"> <div class="pswp__preloader__donut"></div> </div> </div> </div> </div> <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap"> <div class="pswp__share-tooltip"></div> </div> <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)"> </button> <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)"> </button> <div class="pswp__caption"> <div class="pswp__caption__center"></div> </div> </div> </div> </div>
The above HTML structure defines the content, tools, direction buttons, title description and other elements displayed in the gallery.
Javascript
We define the album picture collection in js (of course, we can also define the picture collection in the html part like demo2), set various options, and then call the photoSwipe plug-in by using new PhotoSwipe().
var openPhotoSwipe = function() { var pswpElement = document.querySelectorAll('.pswp')[0]; //定义图片集合 var items = [ { src: 'images/s1.jpg', w: 800, h: 1142 }, { src: 'images/s2.jpg', w: 800, h: 1142 } ]; var options = { history: false, focus: false, showAnimationDuration: 0, hideAnimationDuration: 0 }; var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options); gallery.init(); }; //点击图集元素时触发调用openPhotoSwipe document.getElementById('photos').onclick = openPhotoSwipe;
You can apply this plug-in to mobile projects. For more option settings, please refer to the PhotoSwipe project address: <https://github.com/dimsemenov/photoswipe.
The above content is all the content of javascript implementation to support mobile device gallery. I hope you like it.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
