简体   繁体   English

当它们都缩放后,如何正确地计算图像并将其放置在另一个图像之上-跨浏览器?

[英]How properly calculate and place an image on top of another image when they're both scaled - cross browser?

I have a base image with items on it. 我有一个基本图片,上面有物品。 If the user disables one of the items, it should then show as disabled. 如果用户禁用其中一项,则应显示为已禁用。 To handle this, I use a second image of the item disabled (a png) and place that in the correct location above the background image. 为了解决这个问题,我使用了禁用项目的第二张图像(png),并将其放置在背景图像上方的正确位置。

When there's no scaling, this works perfectly across all browsers. 如果没有缩放比例,则可以在所有浏览器上完美运行。

When there's scaling (for example, in a mobile browser when the image is 40% of the original size) this only works in Firefox . 当缩放时(例如,在移动浏览器中,图像为原始大小的40%时) 仅在Firefox中有效 Firefox always places the image in the correct spot, covering the item underneath. Firefox始终将图像放置在正确的位置,覆盖下面的项目。

The other browsers (Chrome on Android, Safari iOS), are always off by 1-2 pixels depending on the scaling level. 其他浏览器(Android上的Chrome,Safari iOS)始终保持1-2像素关闭,具体取决于缩放级别。 So you can see a little bit of the item below. 因此,您可以在下面看到一些项目。

How can I do this so it works cross-browser? 我该怎么做才能跨浏览器工作?

var heightRatio = bgOffsetHeight / originalHeight;
var widthRatio = bgOffsetWidth / originalWidth;

//Place the disabled items
for ( var i = 0; i < disableds.length; i++ )
{
    var disabled = disableds[ i ];

    /**
     * I also tried with "parseInt", but it still doesn't work.
     * 
     * originalLeft/Top/Width/Height are the values saved in 
     * JavaScript when the page is first loaded before everything
     * is scaled.
     */
    disabled.style.left = ( widthRatio * disabled.originalLeft ) + "px";
    disabled.style.top = ( heightRatio * disabled.originalTop ) + "px";
    disabled.style.width = ( widthRatio * disabled.originalWidth ) + "px";
    disabled.style.height = ( heightRatio * disabled.originalHeight ) + "px";

    //Make visible
    disabled.style.visibility = "visible";
}

Does your background use white or another solid color? 您的背景使用白色还是其他纯色? Then you could try this: place an empty div over the bg portion and then apply background-color: rgba(255, 255, 255, .5) for example. 然后,您可以尝试以下操作:在bg部分上放置一个空的div,然后应用background-color: rgba(255, 255, 255, .5) This should give you the same effect, plus it would save a whole lot of additional images. 这应该给您相同的效果,此外,它还可以节省大量其他图像。 But only works if "the bg of your bg" is a solid color. 但是仅当“ bg中的bg”是纯色时才有效。

The issue arises in WebKit only, where separately calculating the width and height ratio gives the browser an inability to properly size and place images. 仅在WebKit中会出现此问题,在WebKit中,单独计算宽度和高度比率会使浏览器无法正确调整大小和放置图像。 If I change my code to only use the width ratio, then everything places correctly across all browsers: 如果我更改代码以仅使用宽度比例,则所有内容都将正确放置在所有浏览器中:

//We no longer use height ratio for the height/top calculations
//var heightRatio = bgOffsetHeight / originalHeight;
var widthRatio = bgOffsetWidth / originalWidth;

//Place the disabled items
for ( var i = 0; i < disableds.length; i++ )
{
    var disabled = disableds[ i ];

    /**
     * I also tried with "parseInt", but it still doesn't work.
     * 
     * originalLeft/Top/Width/Height are the values saved in 
     * JavaScript when the page is first loaded before everything
     * is scaled.
     */
    disabled.style.left = ( widthRatio * disabled.originalLeft ) + "px";
    disabled.style.top = ( widthRatio * disabled.originalTop ) + "px";
    disabled.style.width = ( widthRatio * disabled.originalWidth ) + "px";
    disabled.style.height = ( widthRatio * disabled.originalHeight ) + "px";

    //Make visible
    disabled.style.visibility = "visible";
}

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

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