我点击照片后遇到问题,然后photoswipe
正常活动
但是当我想在视频播放器外点击关闭时。它不起作用。它适用于图像幻灯片,但是当视频不再有效时。
请帮助我真的不知道如何解决这个问题。
这是我的Photoswipe JS
代码
var parseThumbnailElements = function(el) {
var items = [];
var item;
el.each(function(){
if ($(this).attr('href').match(/youtube/)) {
item = {
html: '<div class="d-flex justify-content-center w-100 h-100">' +
'<iframe width="80%" height="100%" src="'+ $(this).attr('href') +'" frameborder="0" allowfullscreen></iframe>' +
'</div>'
};
} else {
item = {
src: $(this).attr('href'),
w: $(this).data('w'),
h: $(this).data('h'),
msrc: $(this).find('img').attr('src')
};
}
item.el = $(this);
items.push(item);
});
return items;
};
var openPhotoSwipe = function(el) {
var pswpElement = document.querySelectorAll('.pswp')[0];
var index = el.eq();
// build items array
var items = parseThumbnailElements(el);
// define options (if needed)
var options = {
index: index,
getThumbBoundsFn: function(index) {
// See Options -> getThumbBoundsFn section of documentation for more info
var thumbnail = items[index].el.find('img').get(0), // find thumbnail
pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
rect = thumbnail.getBoundingClientRect();
return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
}
};
var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();
};
(function($){
$(document).on('click', '.photoswipe', function(e){
e.preventDefault();
// if image object do this
if ($(this).hasClass('image-gallery__img--show')) {
openPhotoSwipe($(this).closest('.image-gallery__img').find('.photoswipe'));
} else {
// if video then to this
openPhotoSwipe($(this));
}
});
})(jQuery);
&#13;
答案 0 :(得分:1)
我只是从
更改
item = {
html:
'<div class="d-flex justify-content-center w-100 h-100">' +
'<iframe width="80%" height="100%" src="'+ $(this).attr('href') +'" frameborder="0" allowfullscreen></iframe>' +
'</div>'
};
以强>
item = {
html:
'<div class="video-wrapper">' +
'<div class="video-wrapper-inside">' +
'<iframe width="960" height="640" src="'+ $(this).attr('href') +'" frameborder="0" allowfullscreen></iframe>' +
'</div>' +
'</div>'
};
.video-wrapper {
line-height: 0;
width: 100%;
max-width: 900px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
vertical-align: middle;
margin: 0 auto;
text-align: left;
z-index: 1045;
}
.video-wrapper-inside {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
padding-top: 25px;
height: 0;
width: 100%;
iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
}
只需在gallery.init();
var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();
gallery.listen('close', function() {
$('iframe').each(function() {
$(this).attr('src', $(this).attr('src'));
});
});