jQuery代码如下:

$(function() { var $lightbox = $('.lightbox'); var $imageList = $('.image-list'); var $images = $imageList.find('img'); var currentIndex = 0; var imageCount = $images.length;

function updateLightbox() { $lightbox.find('img').attr('src', $images.eq(currentIndex).attr('src').replace('-thumb', '')); $lightbox.find('.info').text($images.eq(currentIndex).attr('alt') + ' (' + (currentIndex + 1) + ' of ' + imageCount + ')'); }

function showLightbox() { $lightbox.fadeIn(); $('body').css('overflow', 'hidden'); updateLightbox(); }

function hideLightbox() { $lightbox.fadeOut(); $('body').css('overflow', 'auto'); }

function showPrevImage() { currentIndex = (currentIndex == 0) ? imageCount - 1 : currentIndex - 1; updateLightbox(); }

function showNextImage() { currentIndex = (currentIndex == imageCount - 1) ? 0 : currentIndex + 1; updateLightbox(); }

function handleKeydown(e) { if (e.keyCode == 27) { // ESC key hideLightbox(); } else if (e.keyCode == 37) { // left arrow key showPrevImage(); } else if (e.keyCode == 39) { // right arrow key showNextImage(); } }

$imageList.on('click', 'a', function(e) { e.preventDefault(); currentIndex = $(this).index(); showLightbox(); });

$lightbox.find('.close').on('click', hideLightbox);

$lightbox.on('click', function(e) { if (e.target == this) { hideLightbox(); } });

$lightbox.find('.prev').on('click', showPrevImage);

$lightbox.find('.next').on('click', showNextImage);

$lightbox.on('swipeleft', showNextImage);

$lightbox.on('swiperight', showPrevImage);

$(document).on('keydown', handleKeydown); });

需要使用jQuery插件来实现手势滑动的效果,可以使用如hammer.js、touchSwipe等插件。

div class=image-list a href=cc1jpgimg src=cc1-thumbjpg alt=image1a a href=cc2jpgimg src=cc2-thumbjpg alt=image2a a href=cc3jpgimg src=cc3-thumbjpg alt=image3adivdiv class=lightbox img src= alt=box

原文地址: https://www.cveoy.top/t/topic/bEmp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录