使用JavaScript调用时,我该如何更改弹出式颜色
我正在尝试使用JavaScript调用弹出窗口,但是当弹出窗口显示时,我需要更改背景颜色,但是每次尝试它时都会更改整个页面颜色。有什么方法可以解决此问题吗?
这是我的代码:弹出背景颜色需要在HTML中脱颖而出,并具有透明颜色的覆盖层。
setTimeout(() => {
(function (proxied) {
window.alert = function () {
iosAlert(arguments[0], arguments[1]);
};
})(window.alert);
function iosAlert() {
try {
var $alert = document.querySelector('.alert');
$alert.parentElement.removeChild($alert);
} catch ($error) {}
var $alert = document.createElement('span');
if (arguments[1] == null) {
arguments[1] =
window.location.protocol + '//' + window.location.hostname;
}
$alert.innerHTML =
'<div class="alert"><div class="inner"><div class="title">' +
arguments[1] +
'</div><div class="text">' +
arguments[0] +
'</div></div><div class="button">OK</div></div>';
document.querySelector('body').appendChild($alert);
setTimeout(function () {
document
.querySelector('.alert .button:last-child')
.addEventListener('click', function () {
$alert.parentElement.removeChild($alert);
});
});
return false;
}
iosAlert('Hello World');
}, 1000);
body {
background: #333 url('https://pixabay.com/get/e837b20a2ff5093ecd1f4604e4484093ea6ae3d11eb2124293f7c179/fire-1235718.jpg') no-repeat center center;
overflow: hidden;
}
.alert * {
-webkit-tap-highlight-color: transparent;
-webkit-touch-callout: none;
-ms-touch-action: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.alert *:focus {
outline: 0
}
.alert {
position: fixed;
top: 50%;
left: 50%;
margin-left: -135px;
margin-top: -50px;
width: 270px;
text-align: center;
font-family: -apple-system, SF UI Text, Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.4;
border-radius: 13px;
overflow: hidden;
z-index: 9999999999999999999999998;
background-color: rgba(255, 255, 255, 0.8);
}
.alert .inner {
padding: 15px;
}
.alert .title {
font-weight: 500;
font-size: 18px;
}
.alert .text {
margin-top: 5px;
}
.alert .button {
position: relative;
height: 44px;
line-height: 44px;
font-size: 17px;
color: #007aff;
border-radius: 0 0 13px 13px;
overflow: hidden;
cursor: pointer;
}
.alert .button:after {
content: '';
position: absolute;
left: 0;
top: 0;
height: 1px;
width: 100%;
display: block;
background-color: #c4c4c4;
z-index: 9999999999999999999999999;
}
I'm trying to call a pop-up with JavaScript, but I need to change the background color when the pop-up shows, but every time I try it changes the whole page color. Is there a way I can fix this?
This is my code: pop up background color needs to stand out of the html and have a overlay with transparent color.
setTimeout(() => {
(function (proxied) {
window.alert = function () {
iosAlert(arguments[0], arguments[1]);
};
})(window.alert);
function iosAlert() {
try {
var $alert = document.querySelector('.alert');
$alert.parentElement.removeChild($alert);
} catch ($error) {}
var $alert = document.createElement('span');
if (arguments[1] == null) {
arguments[1] =
window.location.protocol + '//' + window.location.hostname;
}
$alert.innerHTML =
'<div class="alert"><div class="inner"><div class="title">' +
arguments[1] +
'</div><div class="text">' +
arguments[0] +
'</div></div><div class="button">OK</div></div>';
document.querySelector('body').appendChild($alert);
setTimeout(function () {
document
.querySelector('.alert .button:last-child')
.addEventListener('click', function () {
$alert.parentElement.removeChild($alert);
});
});
return false;
}
iosAlert('Hello World');
}, 1000);
body {
background: #333 url('https://pixabay.com/get/e837b20a2ff5093ecd1f4604e4484093ea6ae3d11eb2124293f7c179/fire-1235718.jpg') no-repeat center center;
overflow: hidden;
}
.alert * {
-webkit-tap-highlight-color: transparent;
-webkit-touch-callout: none;
-ms-touch-action: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.alert *:focus {
outline: 0
}
.alert {
position: fixed;
top: 50%;
left: 50%;
margin-left: -135px;
margin-top: -50px;
width: 270px;
text-align: center;
font-family: -apple-system, SF UI Text, Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.4;
border-radius: 13px;
overflow: hidden;
z-index: 9999999999999999999999998;
background-color: rgba(255, 255, 255, 0.8);
}
.alert .inner {
padding: 15px;
}
.alert .title {
font-weight: 500;
font-size: 18px;
}
.alert .text {
margin-top: 5px;
}
.alert .button {
position: relative;
height: 44px;
line-height: 44px;
font-size: 17px;
color: #007aff;
border-radius: 0 0 13px 13px;
overflow: hidden;
cursor: pointer;
}
.alert .button:after {
content: '';
position: absolute;
left: 0;
top: 0;
height: 1px;
width: 100%;
display: block;
background-color: #c4c4c4;
z-index: 9999999999999999999999999;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码有效。
您应该为
.Alert
背景
alpha
part(Sorbga(255,2555,255,/*较小的数字*/)
)。身体的背景颜色为
#333
。That code works.
You should use a smaller number for the
.alert
background
alpha
part (sorbga(255,255,255,/*smaller number*/)
).And the body's background color is
#333
.