我看了一眼,并询问周围的问题解决方案,到目前为止没有运气。我希望有人可以帮助我。
我一直在研究基于[this] [1]教程的视差效果,我发现了一段时间。它做了应有的事情,因为所有元素都会左右移动,具体取决于移动鼠标的位置。我想调整它,如果可能的话,它可以根据鼠标的位置向左或向右平滑滚动。就像现在一样,它只在鼠标移动时移动。我宁愿更改它,以便它以设定的速度连续滚动给定的左右空间宽度,具体取决于鼠标相对于中心的位置。
这是我到目前为止的代码,它的工作方式与上面链接的教程的第一部分相似:
var headerDiv = document.getElementById("header");
var image1Div = document.getElementById("image1");
var image2Div = document.getElementById("image2");
var image3Div = document.getElementById("image3");
var image4Div = document.getElementById("image4");
var headerDivHeight;
var canStartParallax = false;
var canPositionDivsVertically = true;
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false
// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)
// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;
// Temporary variables to hold mouse x-y pos.s
var tempX = 0;
var tempY = 0;
var objectArray = new Array();
window.onload = function()
{
showAllOfTheContent();
headerDivHeight = headerDiv.offsetHeight;
fillObjectArray();
setimage1ToTransparent();
positionDivs();
turnOffPreloaderDotAnimation();
objectAnimation();
}
function showAllOfTheContent()
{
document.getElementById("content").setAttribute("class", "");
}
function fillObjectArray()
{
var image1X = { getX: function() {return 0.5 * windowWidth + 50} };
var image1Y = 10;
var image1Factor = 0.0;
var image1Array = new Array();
image1Array.push(image1Div, image1X, image1Y, image1Factor);
objectArray.push(image1Array);
var image2X = { getX: function() {return 0.5 * windowWidth - 202} }; //position div from half width of the page
var image2Y = 0;
var image2Factor = 0.20; //parallax shift factor, the bigger the value, the more it shift for parallax movement
var image2Array = new Array();
image2Array.push(image2Div, image2X, image2Y, image2Factor);
objectArray.push(image2Array);
var image3X = { getX: function() {return 0.5 * windowWidth - -160} };
var image3Y = 23;
var image3Factor = 0.60;
var image3Array = new Array();
image3Array.push(image3Div, image3X, image3Y, image3Factor);
objectArray.push(image3Array);
var image4X = { getX: function() {return 0.5 * windowWidth + 50} };
var image4Y = 60;
var image4Factor = 1.0;
var image4Array = new Array();
image4Array.push(image4Div, image4X, image4Y, image4Factor);
objectArray.push(image4Array);
var image5Div = document.getElementById("image5");
var image5X = { getX: function() {return 0.5 * windowWidth + 500} };
var image5Y = 400;
var image5Factor = 0.85;
var image5Array = new Array();
image5Array.push(image5Div, image5X, image5Y, image5Factor);
objectArray.push(image5Array);
}
// Main function to retrieve mouse x-y pos.s
function getMouseXY(e)
{
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft
tempY = event.clientY + document.body.scrollTop
} else { // grab the x-y pos.s if browser is NS
tempX = e.pageX
tempY = e.pageY
}
// catch possible negative values in NS4
if (tempX < 0){tempX = 0}
if (tempY < 0){tempY = 0}
moveDiv(tempX, tempY);
return true
}
function moveDiv(tempXsent, tempYsent)
{
if (canStartParallax == true)
{
var tempXreceived = tempXsent;
var tempYreceived = tempYsent;
if (tempYreceived <= headerDivHeight) //limit the mouse over area
{
for (var i=0;i<objectArray.length;i++)
{
var yourDivPositionX = objectArray[i][3] * (0.5 * windowWidth - tempXreceived) + objectArray[i][1].getX();
objectArray[i][0].style.left = yourDivPositionX + 'px';
}
}
}
}
function positionDivs()
{
var verticalParallaxFactorMultiplyNumber = 2; //the bigger this number, the vertical gap between header object animation will be bigger too
for (var i=0;i<objectArray.length;i++)
{
objectArray[i][0].style.left = objectArray[i][1].getX() + "px";
if (canPositionDivsVertically == true)
{
if ((objectArray[i][0] == image2Div) || (objectArray[i][0] == image3Div) || (objectArray[i][0] == image4Div))
{
objectArray[i][0].style.top = objectArray[i][2] - ((1 + (verticalParallaxFactorMultiplyNumber * objectArray[i][3])) * windowHeight) + "px";
}
else if ((objectArray[i][0] == image1Div))
{
objectArray[i][0].style.top = objectArray[i][2] + "px";
}
else
{
objectArray[i][0].style.top = objectArray[i][2] + ((1 + (verticalParallaxFactorMultiplyNumber * objectArray[i][3])) * windowHeight) + "px";
}
}
}
}
function setimage1ToTransparent()
{
image1Div.style.opacity = 0;
image1Div.style.filter = "alpha(opacity=" + 0 + ")";
}
function objectAnimation()
{
var objectAnimationDuration = 2000;
//animate preloader
$(preloaderAndStringContainerDiv).stop().animate({top: (-1 * windowHeight) + "px"}, objectAnimationDuration, function() {hidePreloader()});
for (var i=0;i<objectArray.length;i++)
{
if ((objectArray[i][0] == image1Div))
{
$(objectArray[i][0]).stop().fadeTo(objectAnimationDuration, 1);
}
else
{
if ((navigator.userAgent.match(/iPad/i) != null) || (navigator.userAgent.match(/iPhone/i) != null) || (navigator.userAgent.match(/iPod/i) != null)) //if using safari mobile device, never turn on parallax
{
$(objectArray[i][0]).stop().animate({top: objectArray[i][2] + "px"}, objectAnimationDuration, function() {canPositionDivsVertically = false});
}
else
{
$(objectArray[i][0]).stop().animate({top: objectArray[i][2] + "px"}, objectAnimationDuration, function() {canStartParallax = true; canPositionDivsVertically = false});
}
}
}
}
function resizeHeader()
{
positionDivs();
}
有人可以告诉我吗?我真的很感激。感谢。
答案 0 :(得分:1)
目前,moveDiv例程由mouseXY函数调用,该函数被设置为mousemove事件的处理程序。所以在你移动鼠标之前,没有任何东西在触发。
相反,你想使用mousemove改变一些速度向量,并使用一些触发器进入一个连续循环(我建议以setInterval开头),它使用这个速度向量来调用moveDiv。
由于您不希望在悬停移动时发生这种情况(否则当鼠标悬停在盒子上时它将首先疯狂旋转),请考虑在mousedown上进入循环,并在mouseup和mouseout上退出循环。
在这里做动画时,请看一下使用requestAnimationFrame,这是新浏览器中支持平滑动画渲染的一项功能,您可以为其提供重复执行的功能:
http://paulirish.com/2011/requestanimationframe-for-smart-animating/
使用这些方法中的任何一种,我建议你保持速度的向量(由mousemove事件调整)和每帧更新的时间戳变量。您将无法预测调用代码之间的延迟,因此为了保持一致,您应该使用现在和最后一帧时间戳之间的差值来计算移动的距离。