我正在为 this tutorial 的手电筒效果建模.
我遇到的问题是当窗口宽度大于我的实际容器时。发生这种情况时,手电筒不再以光标为中心。
看看这个fiddle为了更好地理解。 Fiddle 上的结果链接
$(document).ready(function(){
$('.content-wrap').mousemove(function(e) {
$('#flashlight').css({
'left': e.clientX-960,
'top': e.clientY-523
});
});
});
.content-wrap {
max-width:960px;
width:100%;
min-height: 592px;
margin:0 auto;
position: relative;
overflow: hidden;
background: blue;
}
#flashlight {
background: url(https://s3.amazonaws.com/ip-devs/AllState/keep-your-sanity/img/flashlight.png) center center no-repeat;
position:absolute;
z-index:999;
height:1046px;
width:1920px;
pointer-events:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="content-wrap">
<div id="flashlight"></div>
</div>
最佳答案
这里的技巧是设置偏移量的值。
.content-wrap {
max-width:960px;
width:100%;
min-height: 592px;
和
'left': e.clientX-960,
'top': e.clientY-523
这些值不应该被硬编码。
关于javascript - 在包含的 div 中创建手电筒效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37995298/