css 背景=包含 URL 更改无法跨浏览器工作

标签 css background switch-statement contain

所以我有一个由 DIV 组成的画廊,根据您单击的预览图像/文本更改背景图像。

由于图片尺寸不同,我选择使用“背景尺寸包含”..我的切换功能在 Safar 中运行完美,但在 Firefox 和 Chrome 中效果不佳。

我做错了什么(为什么它只在 Safari 中正确显示)我需要如何更改我的代码? .... 或者有更好的解决方法吗?

http://jsfiddle.net/8m7VB/2/

<div id="bilder" style="position: absolute; bottom: 0px; left: 0px; width: 100%;
height: -moz-calc(100% - 170px); height: -webkit-calc(100% - 170px); height: -o-calc(100% - 170px);

background: url(https://www.google.de/logos/doodles/2013/holiday-series-2013-2-5173241802915840-hp.png) center bottom no-repeat; background-size: contain; z-index: 1;"></div>
<a onclick="document.getElementById('bilder').style.background='url(http://cdn3.spiegel.de/images/image-550112-thumb-fjcw.jpg) center bottom no-repeat'">google<a>
<a onclick="document.getElementById('bilder').style.background='url(http://cdn3.spiegel.de/images/image-549692-thumb-gsor.jpg) center bottom no-repeat'">bild<a>

感谢一百万!

最佳答案

执行 document.getElementById('bilder').style.background= 时,您不仅要覆盖 css 属性 background,还要覆盖子属性,即 背景大小。您没有重新定义 background-size,因此这将设置回默认值 (auto)。

我会改用 document.getElementById('bilder').style.backgroundImage,这样您就不会覆盖其他子属性。 http://jsfiddle.net/8m7VB/3/

关于css 背景=包含 URL 更改无法跨浏览器工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20873714/

相关文章:

html - 横幅图片涵盖了电子商务故事中的侧推车。如何防止这种情况?

CSS布局问题

css - 显示错误 - 在 SASS 中扩展类时无法扩展

php - Wordpress 中的多个背景包装器

html - CSS Sidebar - 内容区域需要边距,但宽度超出了带有滚动条的框架

JavaScript 'case' 与 "case"

java - 我添加范围 1 - 9999 条件的代码无法使用 switch 语句运行

javascript - 如何使用switch比较值之间的值

php - 如何使用 jquery 在自动刷新 div 之前显示消息

javascript - 在 CSS 中创建缩略图叠加