这是非 CSS3 解决方案。该解决方案使用透明度为 10% 的黑色背景 png,聚光灯在下方添加为图像。
HTML
<div class="spotlightContainer">
<div class="imageContainer">
<img src="/images/tuinderlusten.jpg" alt="de tuin der lusten"/>
</div>
<div class="darkCover">
<div class="dark darktop"> </div>
<div class="darkmiddle">
<div class="dark darkleft"> </div>
<div class="spotlight"> </div>
<div class="dark darkright"> </div>
</div>
<div class="dark darkbottom"> </div>
</div>
</div>
JavaScript
var darkRight, darkLeft, darkBottom, darkTop, darkMiddle, containerHeight, containerWidth;
$(function(){
containerWidth = $(".spotlightContainer").width();
containerHeight = $(".spotlightContainer").height();
darkTop = $(".darktop");
darkBottom = $(".darkbottom");
darkLeft = $(".darkleft");
darkRight = $(".darkright");
darkMiddle = $(".darkmiddle");
darkTop.height(100-50);
darkBottom.height(containerHeight-100-50);
darkLeft.width(100-50);
darkRight.width(containerWidth-100-50);
setSpotlight(100, 100);
$(".spotlightContainer").on("mousemove", function(e){
setSpotlight(e.pageX, e.pageY);
});
});
var setSpotlight = function(pageX, pageY){
var radius = 100;
darkMiddle.height(radius*2);
if(pageX < radius){
pageX = radius;
} else if (pageX > containerWidth -radius){
pageX = containerWidth -radius;
}
darkTop.height(pageY-radius);
darkBottom.height(containerHeight-pageY-radius);
darkLeft.width(pageX-radius);
darkRight.width(containerWidth-pageX-radius);
}
CSS
html, body{
width: 100%;
height: 100%;
margin: 0 0 0 0;
padding: 0 0 0 0;
}
div{
margin: 0 0 0 0;
padding: 0 0 0 0;
border: 0;
}
body{
overflow: hidden;
}
.dark{
background: url("/images/darkcover.png");
}
.darktop{
width: 100%;
height: 100%;
}
.darkbottom{
width: 100%;
height: 0px;
}
.darkmiddle{
height:0px;
width: 100%;
}
.darkmiddle div{
float: left;
height: 100%;
}
.darkleft{
width: 200px;
}
.darkright{
width: 200px;
}
.spotlight{
width: 200px;
background: url("/images/spotlight.png");
background-size: cover;
}
.spotlightContainer{
width: 100%; height: 100%; z-index: 500; position: fixed;
}
.spotlightContainer .imageContainer, .spotlightContainer .darkCover{
width: 100%; height: 100%; position: absolute; top: 0; left: 0;
}
.spotlightContainer .imageContainer{
max-height: 100%; max-width: 100%; width: 100%;
}
聚光灯图片
我对此进行了测试,它适用于所有现代和 IE7+ 桌面浏览器。