简体   繁体   English

背景图像不透明与小JS脚本无法在iOS上工作

[英]Background image opacity with small JS script not working on iOS

I have some simple markup and CSS that generates a slightly transparent color screen over a background image. 我有一些简单的标记和CSS,可以在背景图像上生成略微透明的彩色屏幕。 A small script has been added which allows the background image to fully load before adding a new class and transitioning from a solid to transparent. 添加了一个小脚本,允许在添加新类并从实体转换为透明之前完全加载背景图像。

Looks good on desktop browsers, but the issue is that the opacity doesn't seem to be working on my iOS devices (and possibly other devices too). 在桌面浏览器上看起来不错,但问题是不透明度似乎不适用于我的iOS设备(也可能是其他设备)。 It's just a solid color with no background image visible. 它只是一种纯色,没有可见的背景图像。 I'm not very good at JS so I assume the problem is there, but it could be the CSS. 我不是很擅长JS所以我认为问题存在,但它可能是CSS。

Here is a JSFiddle and the code is also embedded below. 这是一个JSFiddle ,代码也嵌入在下面。 Thanks in advance; 提前致谢; any help is greatly appreciated. 任何帮助是极大的赞赏。

 (function(el) { el.forEach(function(e) { var style = e.currentStyle || window.getComputedStyle(e, false), bi = style.backgroundImage.slice(5, -2); var img = new Image(); img.onload = function() { e.classList.add('loaded'); } img.src = bi; }); })(document.querySelectorAll('.title-area')); 
 .title-area * { position: relative; color: #fff; padding: 5px; } .title-area { background-color: #d9554c; background-size: cover; background-position: 50%; position: relative; background-image: url(https://unsplash.it/1000/500?image=206); } .title-area::before { content: ''; position: absolute; width: 100%; height: 100%; background: #d9554c; opacity: 1; transition: opacity 0.5s; } .title-area.loaded::before { opacity: 0.5; transform: translateZ(0); } 
 <div id="content" class="title-area title-area-3"> <div class="title-area-wrapper"> <div class="title-area-content constrain"> <h2 class="sub-header portfolio-header">Content header</h2> </div> </div> </div> 

EDIT: Apparently the javascript had some imperfections. 编辑:显然javascript有一些不完美之处。 When used liked this there is no issue: 使用时喜欢这个没有问题:

 (function(el){ el.forEach(function(e) { var style = e.currentStyle || window.getComputedStyle(e, false), bi = style.backgroundImage.slice(4, -1).replace(/["|']/g, ""); var img = new Image(); img.onload = function() { e.classList.add('loaded'); } img.src = bi; }); })(document.querySelectorAll('.title-area')); 
 .title-area * { position: relative; color: #fff; padding: 5px; } .title-area { background-color: #d9554c; background-size: cover; background-position: 50%; position: relative; background-image: url(https://unsplash.it/1000/500?image=206); } .title-area::before { content: ''; position: absolute; width: 100%; height: 100%; background: #d9554c; opacity: 1; transition: opacity 0.5s; } .title-area.loaded::before { opacity: 0.5; transform: translateZ(0); } 
 <div id="content" class="title-area title-area-3"> <div class="title-area-wrapper"> <div class="title-area-content constrain"> <h2 class="sub-header portfolio-header">Content header</h2> </div> </div> </div> 

You need to use the following code rather then background . 您需要使用以下代码而不是background

background-color: rgb(255,255,255); // White background for old browsers
background-color: rgba(255,255,255,0.5); // White background, 50% transparency

Here I mentation JSFiddle . 在这里我心理JSFiddle

 (function(el){ el.forEach(function(e) { var style = e.currentStyle || window.getComputedStyle(e, false), bi = style.backgroundImage.slice(5, -2); var img = new Image(); img.onload = function() { e.classList.add('loaded'); } img.src = bi; }); })(document.querySelectorAll('.title-area')); 
 .title-area * {position: relative; color: #fff; padding: 5px;} .title-area {background-color: rgba(255, 27, 66, 0.5); background-size: cover; background-position: 50%; position: relative; background-image: url(https://unsplash.it/1000/500?image=206);} .title-area::before {content: ''; position: absolute; width: 100%; height: 100%; background-color: rgba(255, 27, 66, 0.5); opacity: 1; transition: opacity 0.5s;} .title-area.loaded::before {opacity: 0.5; transform: translateZ(0);} 
 <div id="content" class="title-area title-area-3"> <div class="title-area-wrapper"> <div class="title-area-content constrain"> <h2 class="sub-header portfolio-header">Content header</h2> </div> </div> </div> 

Apparently the JS had a couple issues with it. 显然,JS有几个问题。 Using this Javascript fixes the problem. 使用此Javascript修复了该问题。

 (function(el){ el.forEach(function(e) { var style = e.currentStyle || window.getComputedStyle(e, false), bi = style.backgroundImage.slice(4, -1).replace(/["|']/g, ""); var img = new Image(); img.onload = function() { e.classList.add('loaded'); } img.src = bi; }); })(document.querySelectorAll('.title-area')); 
 .title-area * { position: relative; color: #fff; padding: 5px; } .title-area { background-color: #d9554c; background-size: cover; background-position: 50%; position: relative; background-image: url(https://unsplash.it/1000/500?image=206); } .title-area::before { content: ''; position: absolute; width: 100%; height: 100%; background: #d9554c; opacity: 1; transition: opacity 0.5s; } .title-area.loaded::before { opacity: 0.5; transform: translateZ(0); } 
 <div id="content" class="title-area title-area-3"> <div class="title-area-wrapper"> <div class="title-area-content constrain"> <h2 class="sub-header portfolio-header">Content header</h2> </div> </div> </div> 

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

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