我正在创建一个网站,其中我有一系列具有不同尺寸的图像,这些图像被放置在灯箱库中。
对不起提前阅读....代码如下:
http://jsfiddle.net/djtiii/Sas94/1/
HTML
<div id="wrapper">
<h1>simple lightbox w/ slideshow (images only)</h1>
<p>those image links i was talking about:</p>
<ul id="paintings">
<li> <a title="The Hills" href="http://www.artmajeur.com/files/oksana-veber/images/artworks/100x100/6683281_the-hils-100-100-cm-acrilic-on-canvas-1998-thumb.jpg" class="lightboxTrigger">
on
</a>
</li>
<li> <a title="Pixelisa" href="http://piq.codeus.net/static/media/userpics/piq_2313_400x400.png" class="lightboxTrigger">
two
</a>
</li>
<li> <a title="Vase" href="http://andyinoman.files.wordpress.com/2013/01/story-no-5-the-loosing-battle.jpg?w=500" class="lightboxTrigger">
three
</a>
</li>
</ul>
</div>
CSS
/* page styles */
body {
margin: 0;
padding: 0;
background: #efefef;
}
#wrapper {
width: 600px;
margin: 0 auto;
border-radius: 0 0 5px 5px;
-moz-border-radius: 0 0 5px 5px;
-webkit-border-radius: 0 0 5px 5px;
background: #fff;
border: 1px solid #ccc;
padding: 25px;
border-top: none;
box-shadow: 0 0 5px #ccc;
-moz-box-shadow: 0 0 5px #ccc;
-webkit-box-shadow: 0 0 5px #ccc;
text-align: left;
}
/* lightbox styles */
#lightbox {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#lightbox p {
text-align: right;
color: #fff;
margin-right: 20px;
font-size: 12px;
}
#lightbox img {
max-width: 940px;
}
/* slideshow styles */
#slideshow {
position: relative;
z-index: 100;
width: 600px;
height: 350px;
margin: 0 auto;
padding: 10px;
background-color: #fff;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}
#slideshow ul > li {
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
list-style: none;
}
.nav {
display: none;
}
.prev, .next {
position: absolute;
top: 50%;
background: rgba(100, 100, 100, .5);
padding: .25em .5em;
color: #fff;
text-decoration: none;
}
.next {
right: 10px;
}
.prev {
left: 10px;
}
的JavaScript
jQuery(document).ready(function ($) {
var current, size;
$('.lightboxTrigger').click(function (e) {
e.preventDefault();
var slideNum = $('.lightboxTrigger').index(this);
if ($('#lightbox').length > 0) {
$('#lightbox').fadeIn(300);
$('#content').html('<img src="' + href + '" />');
} else {
var hgt = $(this).attr('height');
var wid = $(this).attr('width');
var lightbox =
'<div id="lightbox">' +
'<p>X</p>' +
'<div id="slideshow">' +
'<ul></ul>' +
'<div class="nav">' +
'<a href="#prev" class="prev slide-nav"><</a>' +
'<a href="#next" class="next slide-nav">></a>' +
'</div>' +
'</div>' +
'</div>';
$('body').append(lightbox);
$('#paintings').find('.lightboxTrigger').each(function () {
var href = $(this).attr('href');
var title = $(this).attr('title');
$('#slideshow ul').append(
'<li>' +
'<img src="' + href + '">' +
'<h5>' + title + '</h5>' +
'</li>');
});
$('#lightbox').hide().fadeIn(300);
}
size = $('#slideshow ul > li').length;
$('#slideshow ul > li').hide();
$('#slideshow ul > li:eq(' + slideNum + ')').show();
current = slideNum;
});
$('body').on('click', '#lightbox', function () {
$('#lightbox').fadeOut(300);
});
$('body').on({
mouseenter: function () {
$('.nav').fadeIn(300);
},
mouseleave: function () {
$('.nav').fadeOut(300);
}
}, '#slideshow');
$('body').on('click', '.slide-nav', function (e) {
e.preventDefault();
e.stopPropagation();
var $this = $(this);
var dest;
if ($this.hasClass('prev')) {
dest = current - 1;
if (dest < 0) {
dest = size - 1;
}
} else {
dest = current + 1;
if (dest > size - 1) {
dest = 0;
}
}
$('#slideshow ul > li:eq(' + current + ')').fadeOut(750);
$('#slideshow ul > li:eq(' + dest + ')').fadeIn(750);
current = dest;
});
});
此代码的问题是div id=slideshow
的尺寸对于图像的尺寸不是动态的。我很确定我需要某种Javascript来存储每个图像片段的尺寸,然后将div的尺寸调整到这些尺寸。但是,我在Javascript中没有足够的经验来实现这一目标。
感谢所有对此采取行动的人!
答案 0 :(得分:1)
要调整灯箱的尺寸,您可以使用附加功能查找正在显示的图像的高度,宽度,并将其应用于灯箱窗口。 我为此创造了一个小提琴,请看这里: 计算和应用维度的函数:
function adjustDimensions(current){
var H = $('img','#slideshow ul > li:eq(' + current + ')').height();
var W = $('img','#slideshow ul > li:eq(' + current + ')').width();
if($('h5','#slideshow ul > li:eq(' + current + ')').length>0){
H = H+20;
}
$('#slideshow').animate({width:W+'px',height:H+'PX'},1000);
}