我试图在图像上实现this 鼠标移动效果。
图像看起来像是跟着我的鼠标,我'我试图遵循它,到目前为止this。我试过这个例子https://jsfiddle.net/lesson8/SYwba/。但是我坚持把它与我现在的小提琴结合起来。
这是我想要的输出。就像图像跟随鼠标一样。 Output
$('.animated').hover(
function() {
$('.animated').animate({
marginTop:20
});
},
function() {
$('.animated').animate({
marginTop:10
});
}
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<img class="animated" src="http://via.placeholder.com/350x150"/>
&#13;
还试过这个:
$(document).mousemove(function(e) {
$('.logo').offset({
left: e.pageX,
top: e.pageY + 20
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<img class="logo" src="//ssl.gstatic.com/images/logos/google_logo_41.png" alt="Google">
&#13;
答案 0 :(得分:3)
逻辑实际上非常简单:您要做的是根据光标在文档/视口中的相对位置,使图像偏离其原始位置。我们需要在文档上的mousemove
事件中执行所有这些计算。
$(document).on('mousemove', function(e) {...});
此外,这意味着您需要以下信息:
假设我们想将运动限制在±30px。我们可以将它们定义为:
// Maximum offset for image
var maxDeltaX = 30,
maxDeltaY = 30;
document.documentElement.clientWidth/clientHeight
可以访问视口尺寸:
// Get viewport dimensions
var viewportWidth = document.documentElement.clientWidth,
viewportHeight = document.documentElement.clientHeight;
此处的关键是计算光标与视口的相对位置。首先,我们将鼠标/光标坐标的一部分得到视口,这将给出一系列[0,1]。但是,我们需要将其转换为[-1,1],以便我们可以计算左/上移动(使用负值)和下/右移动(使用正值)。从[0,1]到[-1,1]的算术变换只是乘以2(所以得到[0,2])和减1(然后得到[-1,1]):
// Get relative mouse positions to viewport
var mouseX = e.pageX / viewportWidth * 2 - 1,
mouseY = e.pageY / viewportHeight * 2 - 1;
transform
定位图像这是最直接的部分。要翻译的金额只是mouseX
×maxDeltaX
,沿y轴也是如此。我们将这些值传递给transform: translate(<x>px, <y>px)
:
// Calculate how much to transform the image
var translateX = mouseX * maxDeltaX,
translateY = mouseY * maxDeltaY;
$('.animated').css('transform', 'translate('+translateX+'px, '+translateY+'px)');
见下面的概念验证:
// Settings
// Maximum offset for image
var maxDeltaX = 30,
maxDeltaY = 30;
// Bind mousemove event to document
$(document).on('mousemove', function(e) {
// Get viewport dimensions
var viewportWidth = document.documentElement.clientWidth,
viewportHeight = document.documentElement.clientHeight;
// Get relative mouse positions to viewport
// Original range: [0, 1]
// Should be in the range of -1 to 1, since we want to move left/right
// Transform by multipling by 2 and minus 1
// Output range: [-1, 1]
var mouseX = e.pageX / viewportWidth * 2 - 1,
mouseY = e.pageY / viewportHeight * 2 - 1;
// Calculate how much to transform the image
var translateX = mouseX * maxDeltaX,
translateY = mouseY * maxDeltaY;
$('.animated').css('transform', 'translate('+translateX+'px, '+translateY+'px)');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<img class="animated" src="http://via.placeholder.com/350x150"/>
答案 1 :(得分:0)
尝试将悬停事件绑定到div或某个容器,而不是图像本身。
$('.wrapper-div').hover();
答案 2 :(得分:0)
我只是给你一个提示
$('.animated').mousemove(
function(ev) {
document.getElementById("MouseCoord").innerHTML = ev.clientX + ':' + ev.clientY;
}
);
window.addEventListener("load", function() {
var rect = document.getElementById("container").getBoundingClientRect();
// .toFixed(0) only remove decimal part for a nice display
document.getElementById("BoxSize").innerHTML =
"Box is at (" + rect.left.toFixed(0) + ";" + rect.top.toFixed(0) + ")" +
" with size " + rect.width.toFixed(0) + "x" + rect.height.toFixed(0);
});
#container {
width: 400px;
height: 200px;
border: .1px solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<p id="MouseCoord">-</p>
<p id="BoxSize">-</p>
<div id="container">
<img class="animated" src="http://via.placeholder.com/350x150" />
</div>