我使用Highslide jQuery Gallery在单个页面上加载相册及其缩略图。用户可以单击缩略图,然后加载每个相册。
如果我每页有10张以上的相册,网站运行速度会很慢(由于加载了拇指和图片)我对数据库中的数据使用base64编码。
如何只加载缩略图而不加载整个相册?只有在用户单击缩略图后才会加载相册。。
<?php
//vars
$albumsQuery = mysql_query("select * from albums");
$album_count = 0;
// start loop
while ($album = mysql_fetch_array($albumsQuery)) {
$album_count++;
$unserializePhotos = unserialize(base64_decode($album['photos']));
$unserializeDescriptions = unserialize(base64_decode($album['descriptions']));
$firstPhoto = '';
$first_photo_count = 0;
foreach ($unserializePhotos as $k => $v) {
if ($first_photo_count == 0) {
$firstPhoto = $v['name'];
}
$first_photo_count++;
}
$first_desc_count = 0;
foreach ($unserializeDescriptions as $k => $v) {
$unserializeDescriptions[$k]=htmlspecialchars($v);
if ($first_desc_count == 0) {
$firstDesc = htmlspecialchars($v);
}
$first_desc_count++;
}
?>
<div class="highslide-gallery">
<a class='highslide' id="thumb<?php echo $album_count; ?>" href='/albums/<?php echo $firstPhoto; ?>' onclick="return hs.expand(this, {slideshowGroup: <?php echo $album_count; ?>})">
<img src='/albums/<?php echo $firstPhoto; ?>' height="100px" width="100px" />
</a>
<div class="hidden-container">
<?php
$photoDescIndex = 0;
foreach ($unserializePhotos as $k => $v) {
if ($v['name'] != '' && $v['name'] != $firstPhoto){
?>
<a class='highslide' href='/albums/<?php echo $v['name']; ?>' onclick="return hs.expand(this, {slideshowGroup: <?php echo $album_count; ?>})">
<img src='/albums/<?php echo $v['name']; ?>' />
</a>
<?php
}
$photoDescIndex++;
}
?>
</div>
-
您正在将完整图像加载到缩略图中:
<img src='/albums/<?php echo $firstPhoto; ?>' height="100px" width="100px" />
。浏览器需要更多时间加载大照片,甚至需要更多时间调整大小。您应该在服务器端准备100x100的小缩略图。 -
不应显式加载所有相册
<div class="hidden-container">...</div>
。通过AJAX按需加载选定相册的内容(当用户单击缩略图时)。此外,您可以在页面加载后开始在后台预加载相册。 -
不要将事件处理程序直接分配给每个
onclick="..."
-使用事件委派,例如$("body").on("click", ".highslide", function() { var album_count = this.id.slice(5); // a id="thumb<?php echo $album_count; ?>" var target = $(this).next("div.hidden-container"); // TODO: load album album_count into target via AJAX // TODO: after load: hs.expand(this, {slideshowGroup: album_count}); });