我试图创造一个轻微的视差效应(我不确定它是否真的构成视差,但这是一个类似的想法)。这里有四层,当鼠标移动时,它们都以稍微不同的速度移动。
我发现了一个很好的例子,它提供了一些与我想要的类似的东西:
当鼠标移动时,它通过移动#landing-content div上的背景位置来实现这一点。
$(document).ready(function(){
$('#landing-content').mousemove(function(e){
var x = -(e.pageX + this.offsetLeft) / 20;
var y = -(e.pageY + this.offsetTop) / 20;
$(this).css('background-position', x + 'px ' + y + 'px');
});
});然而,我无法找到一种方法,使这一工作不是在background-position的轮班,而是在div的职位转换。例如,position:relative;和top:x,这样div本身就会移动一点。
我的推理是,div包含CSS动画元素,所以它需要是一个包含内容的div,而不是背景图像。
使用上面的代码来解决这个问题吗?
发布于 2015-06-10 19:39:25
不如用jQuery.offSet()代替?你可能想调整数学/数值,但我认为它应该让你朝着正确的方向发展。
$(document).ready(function () {
$('#layer-one').mousemove(function (e) {
parallax(e, this, 1);
parallax(e, document.getElementById('layer-two'), 2);
parallax(e, document.getElementById('layer-three'), 3);
});
});
function parallax(e, target, layer) {
var layer_coeff = 10 / layer;
var x = ($(window).width() - target.offsetWidth) / 2 - (e.pageX - ($(window).width() / 2)) / layer_coeff;
var y = ($(window).height() - target.offsetHeight) / 2 - (e.pageY - ($(window).height() / 2)) / layer_coeff;
$(target).offset({ top: y ,left : x });
};JSFiddle:http://jsfiddle.net/X7UwG/854/
https://stackoverflow.com/questions/30761409
复制相似问题