CSS -- 透明 "glass" 模态,其他一切变暗
CSS -- transparent "glass" modal, everything else darkened
----- JSBin Example ----
答案是使用background-attachment
ORIGINAL QUESTION
我正在做一个项目,我们希望在背景中显示 "sees through" 模态框,但模态框面板之外的所有地方都被轻微遮盖了。
我已经成功地将 border: 10000px rgba(0,0,0,0.3)
与 border-radius: 10010px
结合使用,但这是一个 hack,我无法用 box-shadow
勾勒出模态框
有没有标准的方法可以做到这一点?如果您能想出一种将透明滤镜渐变应用于图像的方法,可加分。
我认为你在这里得到的最佳选择是有一个 3 行,其中中间一行包含 3 列,顶部底部行和左右列(中间行)的背景变暗:
$(function() {
$('button').click(function() {
tpl = '<div class="modal-reverse-container"><div class="r1"></div><div class="r2"><div class="c1"></div><div class="c2"></div><div class="c3"></div></div><div class="r3"></div></div>'
$('body').append($(tpl));
$('.modal-reverse-container').width($(document).width());
$('.modal-reverse-container').height($(document).height());
$('.modal-reverse-container r1, .modal-reverse-container r2').height($(document).height());
});
$(document).on('click', '.modal-reverse-container', function() {
$(this).remove();
});
});
td {
text-align: center;
background: red;
}
.modal-reverse-container {
position: absolute;
top: 0;
left: 0;
}
.modal-reverse-container .r1, .modal-reverse-container .r2, .modal-reverse-container .r3 {
height: 33%;
}
.modal-reverse-container .r1, .modal-reverse-container .r3 {
background: rgba(0, 0, 0, 0.7);
}
.modal-reverse-container .r2 .c1, .modal-reverse-container .r2 .c2, .modal-reverse-container .r2 .c3 {
width: 33%;
height: 100%;
float: left;
}
.modal-reverse-container .r2 .c3 {
float: right;
}
.modal-reverse-container .r2 .c1, .modal-reverse-container .r2 .c3 {
background: rgba(0, 0, 0, 0.7);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width="100%">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
<img src="https://dummyimage.com/600x400/d950d9/fff" />
<div>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
</div>
<button>Set reverse-modal</button>
---- JSBin Example ----
答案是使用background-attachment
background-attachment: fixed;
background-size: cover;
background-position: center center;
background-image: linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.4)), url(http://imgur.com/oVaQJ8F.png);
.modal-backdrop {
background: url(myurl.png) center center / cover no-repeat fixed
}
.modal-panel {
background: url(myurl.png) center center / cover no-repeat fixed
}
最好的值是 fixed
(并非所有设备都支持 fixed)这样你的背景和你的模式可以共享相同的视口 X
和 Y
(0, 0)
默认
然后您可以按 background-size
百分比或 cover
进行缩放
当使用 background:
shorthand 时,请确保使用 /
将 background-position
与 background-scale
金额分开
我 运行 在旧设备上发现了一个错误,所以我使用 local
然后手动计算 leftX
和 topY
来排列背景和模态面板 background-position
在 (0, 0)
在我的视口上。
然后我以相同的百分比缩放两个图像,以覆盖屏幕
我也用了渐变,功劳--How to darken a background Image
----- JSBin Example ----
答案是使用background-attachment
ORIGINAL QUESTION
我正在做一个项目,我们希望在背景中显示 "sees through" 模态框,但模态框面板之外的所有地方都被轻微遮盖了。
我已经成功地将 border: 10000px rgba(0,0,0,0.3)
与 border-radius: 10010px
结合使用,但这是一个 hack,我无法用 box-shadow
有没有标准的方法可以做到这一点?如果您能想出一种将透明滤镜渐变应用于图像的方法,可加分。
我认为你在这里得到的最佳选择是有一个 3 行,其中中间一行包含 3 列,顶部底部行和左右列(中间行)的背景变暗:
$(function() {
$('button').click(function() {
tpl = '<div class="modal-reverse-container"><div class="r1"></div><div class="r2"><div class="c1"></div><div class="c2"></div><div class="c3"></div></div><div class="r3"></div></div>'
$('body').append($(tpl));
$('.modal-reverse-container').width($(document).width());
$('.modal-reverse-container').height($(document).height());
$('.modal-reverse-container r1, .modal-reverse-container r2').height($(document).height());
});
$(document).on('click', '.modal-reverse-container', function() {
$(this).remove();
});
});
td {
text-align: center;
background: red;
}
.modal-reverse-container {
position: absolute;
top: 0;
left: 0;
}
.modal-reverse-container .r1, .modal-reverse-container .r2, .modal-reverse-container .r3 {
height: 33%;
}
.modal-reverse-container .r1, .modal-reverse-container .r3 {
background: rgba(0, 0, 0, 0.7);
}
.modal-reverse-container .r2 .c1, .modal-reverse-container .r2 .c2, .modal-reverse-container .r2 .c3 {
width: 33%;
height: 100%;
float: left;
}
.modal-reverse-container .r2 .c3 {
float: right;
}
.modal-reverse-container .r2 .c1, .modal-reverse-container .r2 .c3 {
background: rgba(0, 0, 0, 0.7);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width="100%">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
<img src="https://dummyimage.com/600x400/d950d9/fff" />
<div>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
</div>
<button>Set reverse-modal</button>
---- JSBin Example ----
答案是使用background-attachment
background-attachment: fixed;
background-size: cover;
background-position: center center;
background-image: linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.4)), url(http://imgur.com/oVaQJ8F.png);
.modal-backdrop {
background: url(myurl.png) center center / cover no-repeat fixed
}
.modal-panel {
background: url(myurl.png) center center / cover no-repeat fixed
}
最好的值是 fixed
(并非所有设备都支持 fixed)这样你的背景和你的模式可以共享相同的视口 X
和 Y
(0, 0)
默认
然后您可以按 background-size
百分比或 cover
当使用 background:
shorthand 时,请确保使用 /
将 background-position
与 background-scale
金额分开
我 运行 在旧设备上发现了一个错误,所以我使用 local
然后手动计算 leftX
和 topY
来排列背景和模态面板 background-position
在 (0, 0)
在我的视口上。
然后我以相同的百分比缩放两个图像,以覆盖屏幕
我也用了渐变,功劳--How to darken a background Image