[英]having issue to calculate each div top and left position in masonry grid
I Have creating grid gallery where each div has different width and height. 我有创建网格画廊,其中每个div具有不同的宽度和高度。 I want to make this gallery like masonry. 我想让这个画廊像砖石一样。
Here is my Jsfiddle demo 这是我的Jsfiddle演示
here is snippet of java script code 这是Java脚本代码的片段
function posit(){
$(".sli").css({"position":"relative"});
var k=0;
total = $(".sli1").length;
if(k<total){
$(".sli1").each(function(){
// alert(k++);
// var selectpos = selectedItems.position();
$(this).css({"position":"absolute"});
var c = $(this).position();
console.log((k) + ':' + c.left);
var d = $(this).outerWidth();
$(this).css({"left": (c.left)});
$(this).css({"top": (c.top)});
});
}k++;
};
$(document).ready(function(){
posit();
});
Can some one help to calculate each div right top and left position so i can create masonry? 有人可以帮忙计算每个div的右上角和左上角的位置,以便我可以创建砌体吗? Please note:- I don't want to use any plugin . 请注意:-我不想使用任何插件 。 Thankx in advance. 提前感谢。
Updated Here is screenshot How I want my Output 更新,这里是屏幕截图我想要我的输出
You can use this code. 您可以使用此代码。 I used getBoundingClientRect method to get top,right,bottom,left values 我使用getBoundingClientRect方法获取顶部,右侧,底部,左侧的值
I modified your code a bit. 我修改了您的代码。 Please check if this works for you. 请检查是否适合您。
$(".sli1").each(function(){
var i = $(this);
i.css({"position":"absolute"});
var c = i.get(0).getBoundingClientRect();
console.log(c.top,c.left,c.bottom,c.right);
});
To get the position of each div, please see below: 要获取每个div的位置,请参见以下内容:
if(k<total){
$(".sli1").each(function(){
var c = $(this)[0];
// now you can get the position you want for each of the div as follows:
console.log("Top: " + c.offsetTop);
console.log("Left: " + c.offsetLeft);
//the rest of the code
});
}k++;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.