简体   繁体   English

图像在悬停时移动 - 铬不透明度问题

[英]image moves on hover - chrome opacity issue

There seems to be an issue with my page here: http://www.lonewulf.eu 我的页面似乎存在问题: http//www.lonewulf.eu

When hovering over the thumbnails the image moves a bit on the right, and it only happens on Chrome. 将鼠标悬停在缩略图上时,图像会在右侧稍微移动,而只会在Chrome上进行。

My css: 我的css:

.img{
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
    filter:alpha(opacity=50);
    -moz-opacity: 0.5; 
    opacity: 0.5;
    -khtml-opacity: 0.5;
    display:block;
    border:1px solid #121212;
}
.img:hover{
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
    filter:alpha(opacity=100);
    -moz-opacity: 1; 
    opacity: 1;
    -khtml-opacity: 1;  
    display:block;
}

Another solution would be to use 另一种解决方案是使用

-webkit-backface-visibility: hidden;

on the hover element that has the opacity. 在具有不透明度的悬停元素上。 Backface-visibility relates to transform , so @Beskow 's has got something to do with it. 背面可见性与transform ,因此@Beskow与它有关。 Since it is a webkit specific problem you only need to specify the backface-visibility for webkit. 由于它是webkit特定问题,因此您只需指定webkit的背面可见性。 There are other vendor prefixes . 还有其他供应商前缀

See http://css-tricks.com/almanac/properties/b/backface-visibility/ for more info. 有关详细信息,请参阅http://css-tricks.com/almanac/properties/b/backface-visibility/

For some reason Chrome interprets the position of elements without an opacity of 1 in a different way. 出于某种原因,Chrome以不同的方式解释不具有不透明度1的元素的位置。 If you apply the CSS attribute position:relative to your a.img elements there will be no more left/right movement as their opacity varies. 如果你应用CSS属性position:relative对于你的a.img元素,不再有左/右移动,因为它们的不透明度会有所不同。

I had the same problem, I fixed it with css transform rotate. 我有同样的问题,我用css变换旋转修复它。 Like this: 像这样:

-webkit-transform: rotate(0);
-moz-transform: rotate(0);
transform: rotate(0);

It works fine in major browsers. 它在主流浏览器中运行良好。

Another solution that fixed this issue for me was to add the rule: 解决这个问题的另一个解决方案是添加规则:

will-change: opacity;

In my particular case this avoided a similar pixel-jumping issue that translateZ(0) introduced in Internet Explorer, despite fixing things in Chrome. 在我的特殊情况下,这避免了类似的像素跳跃问题,即在Internet Explorer中引入的translateZ(0) ,尽管在Chrome中进行了修复。

Most of the other solutions suggested here that involve transforms (eg. translateZ(0) , rotate(0) , translate3d(0px,0px,0px) , etc) work by handing painting of the element over to the GPU, allowing more efficient rendering. 这里提出的涉及变换的大多数其他解决方案(例如, translateZ(0)rotate(0)translate3d(0px,0px,0px)等)通过将元素的绘制交给GPU来工作,从而允许更有效的渲染。 will-change provides a hint to the browser that has presumably a similar effect (allowing the browser to render the transition more efficiently), but feels less hacky (since it's explicitly addressing the problem rather than just nudging the browser to use the GPU). will-change为浏览器提供了一个类似效果的提示(允许浏览器更有效地呈现转换),但感觉不那么hacky(因为它明确地解决了问题,而不仅仅是推动浏览器使用GPU)。

Having said that, it's worth bearing in mind that browser support is not as good for will-change (though if the issue is with Chrome only then this might be a good thing!), and in some situations it can introduce problems of its own , but still, it's another possible solution to this issue. 话虽如此,值得注意的是, 浏览器支持不会will-change (尽管问题只适用于Chrome,这可能是一件好事!),在某些情况下,它可能会引入自己的问题 ,但仍然是这个问题的另一种可能的解决方案。

I was need apply both backface-visibility and transform rules to prevent this glitch. 我需要应用backface-visibilitytransform规则来防止这种故障。 Like this: 像这样:

a     {-webkit-transform: rotate(0);}
a img {-webkit-backface-visibility: hidden;}

I had a similar issue with (non-opacity) filters on hover. 我在悬停时遇到类似的问题(非不透明度)过滤器。 Fixed by adding a rule to the base class with: 修复了通过向基类添加规则:

filter: brightness(1.01);

backface-visibility (or -webkit-backface-visibility) only fixed the issue in Chrome for me. backface-visibility(或-webkit-backface-visibility)只为我解决了Chrome中的问题。 To fix in both Firefox and Chrome I used the following combination of above answers. 要修复Firefox和Chrome,我使用了以下上述答案的组合。

//fixes image jiggle issue, please do not remove
img {
  -webkit-backface-visibility: hidden; //Webkit fix
  transform: translate3d(0px,0px,0px); //Firefox fix
}

I encountered a similar issue in Safari 8.0.2, where images would jitter as their opacity transitioned. 我在Safari 8.0.2中遇到了类似的问题,当不透明度转换时,图像会抖动。 None of the fixes posted here worked, however the following did: 此处发布的修复程序均无效,但以下情况如下:

-webkit-transform: translateZ(0);

All credit to this answer via this subsequent answer 所有信贷这个答案通过此后续的答案

I ran into this issue with images in a grid each image was nested in an that had display: inline-block declared. 我遇到了这个问题,网格中的图像每个图像都嵌套在一个显示的内容中:声明了内联块。 The solution that Jamland posted above worked to correct the issue when the display: inline-block; Jamland上面发布的解决方案在显示时纠正了问题:inline-block; was declare on the parent element. 是在父元素上声明的。

I had another grid where the images were in an unordered list and I was able to just declared display: block; 我有另一个网格,图像在一个无序列表中,我只能声明display:block; and a width on the parent list item and declared backface-visibility: hidden; 和父列表项上的宽度并声明backface-visibility:hidden; on the image element and there doesn't seem to be any position shift on hover. 在图像元素上,悬停时似乎没有任何位置偏移。

li { width: 33.33333333%; display: block; }
li img { backface-visibility: hidden; }

The solution alpipego was served me in this problem. 解决方案alpipego在这个问题上为我服务。 Use -webkit-backface-visibility: hidden; 使用-webkit-backface-visibility: hidden; With this the image no move in hover opacity transition 这样,图像在悬停不透明度转换中不会移动

/* fix error hover image opacity*/
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-ms-backface-visibility: hidden;
backface-visibility: hidden;

I had trouble with all the other solutions here, as they require changes to the CSS that may break other things -- position:relative requires me to completely rethink how I'm positioning my elements, transform:rotate(0) can interfere with existing transforms and gives wonky little transition effects when I have a transition-duration set, and backface-visibility won't work if I ever actually need a backface (and requires a whole lot of prefixing.) 我在这里遇到了所有其他解决方案的问题,因为它们需要对CSS进行更改,这可能会破坏其他事情 - 位置:相对要求我完全重新思考我如何定位我的元素,转换:rotate(0)会干扰现有的当我有一个转换持续时间设置时,转换并给出了很小的过渡效果,如果我真的需要一个背面(并且需要大量的前缀),背面可见性将不起作用。

The laziest solution I found is to just set an opacity on my element which is very close to, but not quite, 1. This is only a problem if opacity's 1, so it's not going to break or interfere with any of my other styles: 我找到的最懒的解决方案是在我的元素上设置一个非常接近但不完全是1的不透明度。如果不透明度为1,这只是一个问题,所以它不会破坏或干扰我的任何其他样式:

opacity:0.99999999;

Having marked Rick Giner's answer as correct I then found out it did not fix the issue. 将Rick Giner的答案标记为正确后,我发现它没有解决问题。

In my case I have responsive width images contained within a max-width div. 在我的情况下,我有响应宽度图像包含在最大宽度div。 Once the site width crosses that max width the images move on hover (using css transform). 一旦站点宽度超过最大宽度,图像就会在悬停时移动(使用css变换)。

The fix in my case was to simply amend the max width to a multiple of three, three columns in this case, and it fixed it perfectly. 在我的情况下修复是在这种情况下简单地将最大宽度修改为三个,三列的倍数,并且它完美地修复了它。

I noticed you had opacity included in your CSS. 我注意到你的CSS中包含了不透明度。 I had the same exact issue with Chrome (the image moving on hover) .. all I did was disable the opacity and it was solved, no more images moving. 我与Chrome有同样的问题(图像在悬停时移动)..我所做的就是禁用不透明度并且它已经解决,没有更多的图像移动。

.yourclass:hover {
  /* opacity: 0.6; */
}

有同样的问题,我的修复是将类放在img选项卡中的src之前。

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

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