简体   繁体   English

通过 javascript 重新定位的元素之间的空白

[英]White space between repositioned elements via javascript

I'm creating an image puzzle with javascript.我正在用 javascript 创建一个图像拼图。 I creating 14 div elements in 2 row and then I add backrgound image to them, setting their positions and widths ect... It is working fine.我在 2 行中创建 14 个 div 元素,然后向它们添加背景图像,设置它们的位置和宽度等......它工作正常。 But then when I repositioning them (shuffle) unwanted white space appears.但是当我重新定位它们(随机播放)时,会出现不需要的空白。 Not always, not every image and not the same place.并非总是如此,不是每个图像,也不是同一个地方。 I change only two element at the same time.我同时只更改两个元素。

Here is my change function:这是我的更改 function:


let original_pos = [];

// I choose two element randomly, and pushing the first element positions into the original_pos array
// and I pass the second element to the shuffle_elem() function
function shuffle() {
    let random_elem = document.querySelectorAll(".bg-elem");
    for(let i = 0; i <= 50; i++) {
    let random_num = Math.floor(Math.random() * random_elem.length);
    let random_num2 = Math.floor(Math.random() * random_elem.length);
    original_pos.push(random_elem[random_num].offsetTop);
    original_pos.push(random_elem[random_num].offsetLeft);
    cserel_elem(random_elem[random_num],random_elem[random_num2]);
}

// Here are the positions change
function shuffle_elem(elem1, elem2) {
    elem1.style.left = elem2.offsetLeft+'px';
    elem1.style.top = elem2.offsetTop+'px';
    elem2.style.top = original_pos[0]+'px';
    elem2.style.left  = original_pos[1]+'px';
    original_pos = [];
}

Everything is working fine, so I can change the two elements, but there will be a little white space.一切正常,所以我可以更改这两个元素,但会有一点空白。 In this Picture you can see it in two elements next to each other, but sometimes there is no white space at all or just one element has or almost every element... Totally random.在这张图片中,您可以在两个相邻的元素中看到它,但有时根本没有空白,或者只有一个元素有或几乎每个元素......完全随机。

在此处输入图像描述

Only 1 pixel but it is there and very frustrating.只有 1 个像素,但它在那里,非常令人沮丧。 Please help me find out where is this one pixel hiding.请帮我找出这个像素隐藏在哪里。 No jQuery please没有 jQuery 请

Some additional information (CSS):一些附加信息(CSS):

// the main holder div where my elements are
.PlayGround {
    position: relative;
    overflow: hidden;
}
// one element
.bg-elem {
    position: absolute;
    margin: 0;
    padding: 0;
}

With your link provided I was able reproduce the issue, inspect your code and found a fix or two.使用您提供的链接,我能够重现该问题,检查您的代码并找到一两个修复程序。

Yes, you can see the original elements positioned by javascript, then I wait 1.5s then shuffle them.是的,您可以看到 javascript 定位的原始元素,然后我等待 1.5 秒然后将它们随机播放。 Here!这里!
– Bálint Gácsfalvy – BálintGácsfalvy
Comment quote for documentation purposes.用于文档目的的评论报价。

The issue is fairly simple:问题很简单:
You're images might be all the same size, but they are not rounded to full pixels, as shown by the code:您的图像可能大小相同,但它们没有四舍五入到完整像素,如代码所示:

// ratio is already already not an integers
let new_width = imgwidth * ratio;
let new_height = imgheight * ratio;
// and then some lines later you do divide by 7 and 2.
element[i].style.width = new_width/7+'px';
element[i].style.height = new_height/2+'px';

In addition to that you also set left and top not to integer pixels.除此之外,您还设置lefttop不为 integer 像素。
All of this is causing these strange lines of the background flashing between the images.所有这些都导致这些奇怪的背景线条在图像之间闪烁。 Because the rendering engine of the browser can only display images for "boxes" that are defined by whole number pixel position and size, the browser fits the image by rounding to the nearest pixel.因为浏览器的渲染引擎只能显示由整数像素 position 和大小定义的“框”的图像,所以浏览器通过四舍五入到最接近的像素来拟合图像。
If there's an rounding error, it can happen, that the images next to each other are separated by exactly one pixel.如果存在舍入误差,则可能会发生彼此相邻的图像恰好被一个像素分开。 (They can also overlap by one pixel, but that usually goes unnoticed.) (它们也可以重叠一个像素,但这通常不会被注意到。)

jsfiddle截图:背景闪烁 See video of this issue happening on resize.请参阅调整大小时发生的此问题的视频。

Now you know why, let's fix this:现在你知道为什么了,让我们解决这个问题:
There are many ways to fix this, the best would be to do the rounding to full pixels by yourself when setting size and offset ( left and top ) and not rely on the rounding of the rendering engine.有很多方法可以解决这个问题,最好的办法是在设置大小和偏移量( lefttop )时自己进行四舍五入,而不是依赖渲染引擎的四舍五入。 But that's something that is more time expensive and I'm sure you'll figure it out yourself if you want to do it this way.但这是更耗时的事情,我相信如果你想这样做,你会自己弄清楚。

The quick and dirty way I have chosen to show you here is to make the new_width and new_height values dividable to integers by the number of pieces before you use them.我选择在这里向您展示的快速而肮脏的方法是在使用之前将new_widthnew_height值除以整数。 This is super easy, just do this:这非常简单,只需执行以下操作:

let new_width = Math.floor(imgwidth * ratio / 7) * 7;
let new_height = Math.floor(imgheight * ratio / 2) * 2;

instead of this:而不是这个:

let new_width = imgwidth * ratio;
let new_height = imgheight * ratio;

Downside of this fix: You loose up to 6 pixels horizontal and 1 vertical.此修复的缺点:您最多会丢失 6 个水平像素和 1 个垂直像素。 (I know. Terrible.) (我知道。太可怕了。)

See how the fixed code works here !看看固定代码是如何工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM