【发布时间】:2015-10-26 01:20:45
【问题描述】:
我想知道是否可以在网页上使用光标创建镜像效果(也许使用 Javascript?)。以下面的代码为例,页面黑色和白色部分之间的线将是“反射线”,因此如果鼠标在顶部黑色部分移动,则会出现光标的反转图像并且在底部白色部分发生相反的动作,反之亦然。
任何关于从哪里开始的帮助和指示(没有双关语)将不胜感激,谢谢!
编辑:我尝试按照人们的建议使用 .mousemove()、event.pageX、event.pageY(如下所示)。我对 Javascript 还很陌生,所以我在使它工作时遇到了问题。
cursor.html
<doctype! html>
<head>
<link rel="stylesheet" href="cursor.css">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="cursor.js"></script>
<title> Cursor Mirror </title>
</head>
<body>
<div class="top-half-black"></div>
<div class="bottom-half-white">
<img id="mirror-image" src="upside-down.png">
</div>
<script>
$(".top-half-black").mousemove(function(event){
var xPosition = event.pageX;
var yPosition = event.pageY;
})
function placeCursor() {
var d = document.getElementById('mirror-image');
d.style.position = "absolute";
d.style.left = xPosition+'px';
d.style.top = -yPosition+'px';
}
$( ".top-half-black").mouseover(function( event ){
placeCursor () ;
})
</script>
</body>
</html>
cursor.css
body{
margin:0px 0px 0px 0px;
}
.top-half-black{
background-color:black;
width:100%;
height:50%;
}
我想要的样子:
【问题讨论】:
-
大声思考。您可以在鼠标移动事件中获取顶部 div 中的光标位置。您可以在底部 div 中有一个画布元素,并根据顶部 div 中的光标位置计算光标位置。您需要更新顶部 div 中鼠标移动事件的画布。
-
您可以使用鼠标光标的绝对定位图像和 jquery 的
mousemove()事件以及event.pageX和event.pageY属性轻松实现这一点。 -
是的,不要使用画布,工作量会超出必要。光标图像元素的绝对定位将处理它。
-
@leemo 谢谢,我会试试的!
标签: javascript jquery html css