javascript - object-fit: cover; stops working on dynamically added (Safari bug?)
问题描述
I have a weird one... Just spent hours trying to tackle a Safari bug where object-fit: cover; stops working on a dynamically added img elements.
Maybe there is a workaround? Or maybe someone has any idea what is going on? I am unable to wrap my head around this one.
I have isolated the bug in this small test site: https://safaribug.000webhostapp.com/
Just press Next page on Safari (it loads the same page just with a new next page URL).
解决方案
If anyone else stumbles open this bug, the solution that worked for me was to:
- Detect if the browser is Safari
- If it is, then once the HTML was put to the dom, loop through each image and force it to re-render. In my case I just removed srcset attribute and put it back again like so:
document.querySelectorAll('picture.image > img')
.forEach($img => {
const srcset = $img.getAttribute('srcset');
$img.setAttribute('srcset', '');
$img.setAttribute('srcset', srcset);
});
Not a solution I was looking for, but it works.
推荐阅读
- r - 在 R 管道中应用自定义函数
- excel - 如何修改vba代码中的excel公式?
- r - 反转特征缩放
- javascript - Excel 加载项:表格 OnChange 事件有时不会在单元格值更改后触发
- vb.net - WebAPI HttpContext.Current.Request.Cookies NULL 尽管检查
- node.js - 如何使用 d3 节点向条形图添加负值?
- react-native - 如何将 useLinkProps 与操作一起使用以仅使用路径设置导航行为
- java - 对http get请求进行多次调用的最佳方法?
- java - GET 方法返回 HTML 和 POST 到 JSON
- javascript - 我为我的 Angular 11 项目安装了 Bootstrap 4,但它不起作用