我試圖讓這個背景圖像在mousemove上移動。現在圖像不會移動,只是保持不變。另外,我認爲腳本存在錯誤,但我無法弄清楚哪個部分是錯誤的。在mousemove上移動背景圖像
/* background movement by mouse*/
$(document).ready(function() {
var movementStrength = 25;
var height = movementStrength/$(window).height();
var width = movementStrength/$(window).width();
$("#top-image").mousemove(function(e) {
var pageX = e.pageX - ($(window).width()/2);
var pageY = e.pageY - ($(window).height()/2);
var newvalueX = width * pageX * -1 - 25;
var newvalueY = height * pageY * -1 - 50;
$('#top-image').css("background-position", newvalueX + "px " + newvalueY + "px");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<img id="top-image" src="https://static.pexels.com/photos/4164/landscape-mountains-nature-mountain.jpeg">
</div>
你有你的HTML添加圖片標籤,你試着用背景位置,這不是任何意義動畫。你能確定一下你真正想做什麼以及你現在嘗試做什麼嗎? – mlbd