首先我们需要了解相册集的一些基本概念和实现原理。
什么是相册集?
相册集是一种展示图片集合的页面效果,它一般包含缩略图列表、图片预览、图片标题和描述等内容。相册集可以通过点击缩略图来切换显示不同的图片,并支持左右滑动切换图片。相册集通常使用轮播插件或者自定义实现来制作。
使用jquery制作相册集具体步骤:
1.准备工作
1.1 引入jquery库
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
1.2 编写HTML结构
首先需要编写相册集的HTML结构,包括缩略图列表、图片预览区域、图片标题和描述等内容。
<div class="album">
<div class="thumbs">
<ul>
<li><img src="images/1.jpg" alt=""></li>
<li><img src="images/2.jpg" alt=""></li>
<li><img src="images/3.jpg" alt=""></li>
<li><img src="images/4.jpg" alt=""></li>
</ul>
</div>
<div class="preview">
<div class="slide"><img src="images/1.jpg" alt=""></div>
<div class="slide"><img src="images/2.jpg" alt=""></div>
<div class="slide"><img src="images/3.jpg" alt=""></div>
<div class="slide"><img src="images/4.jpg" alt=""></div>
<a class="prev"></a>
<a class="next"></a>
</div>
<div class="info"></div>
</div>
2.实现功能
2.1 点击缩略图切换图片
$(".thumbs ul li").click(function() {
var index = $(this).index();
$(this).addClass("active").siblings().removeClass("active");
$(".preview .slide").eq(index).show().siblings().hide();
})
2.2 左右滑动切换图片
var curIndex = 0;
var total = $('ul', '.thumbs').find("img").length;
$(".prev").click(function() {
curIndex--;
if (curIndex < 0) {
curIndex = total - 1;
}
$('ul', '.thumbs li').eq(curIndex).trigger('click');
})
$(".next").click(function() {
curIndex++;
if (curIndex > total - 1) {
curIndex = 0;
}
$('ul', '.thumbs li').eq(curIndex).trigger('click');
})
2.3 显示图片标题和描述
$(".thumbs ul li").click(function() {
var index = $(this).index();
$(this).addClass("active").siblings().removeClass("active");
$(".preview .slide").eq(index).show().siblings().hide();
var title = $(this).find("img").attr("alt");
var desc = $(this).find("img").attr("data-desc");
$(".info").html("<h3>" + title + "</h3><p>" + desc + "</p>");
})
3.美化样式
最后对CSS样式进行美化。
.album {
margin: 0 auto;
width: 80%;
}
.thumbs {
float: left;
width: 20%;
height: 500px;
overflow-y: scroll;
}
.thumbs ul {
list-style: none;
margin: 0;
padding: 0;
}
.thumbs li {
margin-bottom: 10px;
cursor: pointer;
}
.thumbs li img {
width: 100%;
height: auto;
}
.preview {
float: left;
width: 60%;
height: 500px;
position: relative;
}
.slide {
position: absolute;
top: 0;
left: 0;
z-index: 1;
width: 100%;
height: 100%;
}
.preview img {
width: 100%;
height: 100%;
object-fit: contain;
}
.prev,
.next {
position: absolute;
top: 50%;
z-index: 2;
width: 30px;
height: 30px;
margin-top: -15px;
background-color: rgba(0, 0, 0, .5);
}
.prev {
left: 0;
border-radius: 4px 0 0 4px;
}
.next {
right: 0;
border-radius: 0 4px 4px 0;
}
.info {
float: left;
width: 20%;
height: 500px;
padding: 20px;
font-size: 14px;
line-height: 1.5;
color: #333;
}
.info h3 {
font-size: 18px;
font-weight: 400;
margin-bottom: 10px;
}
.info p {
margin-bottom: 10px;
}
示例1:非常漂亮的相册集
演示效果:https://codepen.io/ziweihuang/pen/WNrRVwO
代码解析:这是一个非常漂亮的相册集,它使用了CSS3动画和渐变效果,以及一些jQuery代码实现了图片的切换、滑动和标题描述的显示效果。
示例2:使用插件制作相册集
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>相册集</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/photoswipe/4.1.3/photoswipe.css">
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/photoswipe/4.1.3/default-skin/default-skin.css">
<style>
.gallery-item {
width: 25%;
float: left;
margin-bottom: 10px;
padding: 0 5px;
}
.gallery-item img {
width: 100%;
height: auto;
cursor: pointer;
}
</style>
</head>
<body>
<!-- 相册集列表 -->
<div class="gallery">
<div class="gallery-item">
<img src="images/1.jpg" alt="">
</div>
<div class="gallery-item">
<img src="images/2.jpg" alt="">
</div>
<div class="gallery-item">
<img src="images/3.jpg" alt="">
</div>
<div class="gallery-item">
<img src="images/4.jpg" alt="">
</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/photoswipe/4.1.3/photoswipe.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>
<script>
$(document).ready(function() {
var pswpElement = document.querySelectorAll('.pswp')[0];
var items = [
{
src: 'images/1.jpg',
w: 400,
h: 300
},
{
src: 'images/2.jpg',
w: 800,
h: 600
},
{
src: 'images/3.jpg',
w: 1600,
h: 1200
}
];
$(".gallery-item").click(function() {
var index = $(this).index();
var options = {
index: index,
bgOpacity: 0.7,
showHideOpacity: true
};
var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();
})
})
</script>
<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>
</body>
</html>
代码解析:该示例使用的是PhotoSwipe插件,实现了图片的缩放、旋转、滑动和标题描述的显示效果。
结语
无论是自定义制作相册集还是使用插件进行制作,相册集都是一种很好的图片展示效果,可以为网站提供更为丰富的视觉体验。本攻略提供了相册集的制作流程和代码示例,希望对大家有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:非常漂亮的相册集 使用jquery制作相册集 - Python技术站