html + css + jquery 动态照片墙实现
需求:
-
将指定的照片放到照片墙
-
点击选中照片墙中的照片,照片墙显示选中照片的放大图片
-
点击照片墙的放大照片,照片墙还原成 多个小图
照片墙小图模式
照片墙大图模式
实现代码
<!DOCTYPE html>
<html>
<head>
<script src="./js/jquery3.5.1.js"></script>
<style>
*{
margin:0;
padding:0;
}
html{
background: #000000;
}
#warp{
width:90%;
height:90vh;
position:absolute;
top:5%;
left:5%;
border: #D95AE2 solid 5px;
border-radius: 10px;
}
#warp>div{
position:absolute;
transition: 1s;
}
</style>
</head>
<body>
<div id = "warp"></div>
<script>
$(function(){
const big_width = 700
const big_height = 500
const small_width = big_width/3
const small_height = big_height/3
let isOpen;
create_div()
open();
function create_div(){
for(let i = 0; i < 9; i++){
const div = $('<div></div>').css({
'width': small_width + 'px',
'height': small_height + 'px'
})
div.appendTo($('#warp'))
}
}
function open(){
for (let i = 0; i < $('#warp>div').length; i++) {
const row = parseInt(i / 3);
const column = i % 3;
const row_span = ($('#warp').height() - big_height)/4;
const column_span = ($('#warp').width() - big_width)/4;
const left = (column+1) * column_span + column * small_width;
const top = (row + 1) * row_span + row * small_height;
const r = Math.random()*20 - 10;
$('#warp>div').eq(i).css({
'background': 'url(./images/' + (i+1) + '.jpg)',
'background-size': small_width + 'px ' + small_height + 'px',
'left': left + 'px',
'top': top + 'px',
'transform': 'scale(0.8) rotate(' + r + 'deg)',
'box-shadow': '1px 1px 20px #ff0ffb',
'border-radius': '5px'
});
isOpen = true;
}
}
function close(index){
for(let i = 0; i < $('#warp>div').length; i++) {
const row = parseInt(i / 3);
const column = i % 3;
const left_span = ($('#warp').width() - big_width) / 2;
const top_span = ($('#warp').height() - big_height) / 2;
const top = top_span + row * small_height;
const left = left_span + column * small_width;
$('#warp>div').eq(i).css({
'background':'url(./images/' + (index + 1) + '.jpg)',
'transform':'scale(1) rotate(0deg)',
'box-shadow':'0 0 0 0',
'border-radius': '0',
'background-position': '-' + (column * small_width) + 'px -' + (row * small_height) + 'px',
'top': top + 'px',
'left': left + 'px'
});
isOpen = false;
}
}
$('#warp>div').click(function(){
const index = $(this).index();
isOpen ? close(index) : open();
})
})
</script>
</body>
</html>