gpt4 book ai didi

jquery - 响应式方形图像重复以平均填充窗口空间

转载 作者:行者123 更新时间:2023-11-28 12:59:29 25 4
gpt4 key购买 nike

我会尽量说清楚。我的目标是用固定数量的方 block 均匀地填充一个窗口空间,这些方 block 也将响应窗口大小的变化。然后这些方 block 会随机消失,直到只有它们中的树作为按钮留在中间。

我找到了一段用于照片库的代码,并试图根据我的目的对其进行调整,但我卡在了响应部分。

这是我的索引:

    <html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> SPLIT</title>

<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script src ="jquery.sliced.js"></script>

<link rel="stylesheet" href="jquery.sliced.css"/>


</head>
<body>

<div class="square"></div>

<div id ="wrapper">
<div id="back"></div>
</div>
</body>


</html>

这是我的 CSS:

html, body {
width:100%;
}

#wrapper {
width:100%;
height:100%;
margin-left:-5px;
padding-top:-10px;
position:fixed;
overflow:hidden;
}


.tile {
float:left;
background-color:red;
-webkit-transition: all .1s linear;
-moz-transition: all .1s linear;
-ms-transition: all .1s linear;
-o-transition: all .1s linear;
}


.tile:hover {
opacity:0;
}

还有我的 JS:

$(window).load(function(){
;(function( $, window ) {

var _defaults = {
x : 20, // number of tiles in x axis
y :20, // number of tiles in y axis
gap:2,
random : true, // animate tiles in random order
speed : 2000 // time to clear all tiles
};

$.fn.splitInTiles = function( options ) {

var o = $.extend( {}, _defaults, options );

return this.each(function() {

var $container = $('#wrapper');
var width = $container.width(),
height = $container.height(),
$back = $("#back"),
n_tiles = o.x * o.y,
tiles = [], $tiles;

for ( var i = 0; i < n_tiles; i++ ) {
tiles.push('<div class="tile"/>');
}

$tiles = $( tiles.join('') );

// Hide original image and insert tiles in DOM
$back.hide().after( $tiles );

// Set background
$tiles.css({
width: width / o.x,
height: height / o.y,
marginBottom: o.gap +'px',
marginRight: o.gap +'px',
});

// Adjust position
$tiles.each(function() {
var pos = $(this).position();
$(this).css( 'backgroundPosition', -pos.left +'px '+ -pos.top +'px' );
});


});

};

}( jQuery, window ));

$('#back').splitInTiles();
});//]]>

这是带有结果的 jsfiddle:

http://jsfiddle.net/Z5WyQ/13/

我似乎想不出办法让方 block 均匀地填满窗口空间。

最佳答案

更改后它确实有效:

$tiles.css({
width: Math.floor((width / o.x) + o.gap),
height: Math.floor((height / o.y) + o.gap),
marginBottom: o.gap + 'px',
marginRight: o.gap + 'px'
});

关于jquery - 响应式方形图像重复以平均填充窗口空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16263279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com