...

View Full Version : Where do I put setInverval?



nschultz
06-19-2009, 03:00 AM
I have an overloaded div on my page that is scrolled by the position of the mouse in the window. It uses the onmousemove event to tell the position of the mouse. I want to use setInterval, or sonething, so that it will continually update the position of the mouse. It currently only registers the position when the mouse is moving, so it stops scrolling when the mouse stops moving.
The page is at http://www.nicnok.net/HTML/gallery/gallery.html
I'll assume you know how to get the html and so I won't take up space with it here. Thanks in advance for your help. The .js code is




document.onmousemove = CurPos;

function CurPos(evt) {
if (!evt) {
evt = window.event;
}
moveGal(evt.clientX);
}

function moveGal(xPos) {

var stageWidth = document.width;
var speed = 15;
var galWidth = document.getElementById("whole").offsetWidth;
var galX = document.getElementById("whole").offsetLeft;
var galView = document.getElementById("container").offsetWidth;

var xDist = xPos-(stageWidth/2);

galX = galX+((0-xDist)/speed);

if (galX>=0) {
galX = 0;
}
if (galX<=galView-galWidth) {
galX = galView-galWidth;
}

document.getElementById("whole").style.left=galX+"px";
}

ninnypants
06-19-2009, 04:45 AM
you should be able to just replace this:

document.onmousemove = CurPos;
With:

setInterval("CurPos()", 5000);//5000 = 5 seconds

I'm not sure how this will work with event's though, but you should be able to get the mouses x position without an event

nschultz
06-22-2009, 03:59 PM
I tried just swapping in your code, but nothing. This is what I was trying to do before I just settled for the mouse event; but it wouldn't work either.



setInterval("moveGal()", 500);

function moveGal() {

if (!evt) {
evt = window.event;
}

var xPos=evt.clientX;

var stageWidth = document.width;
var speed = 15;
var galWidth = document.getElementById("whole").offsetWidth;
var galX = document.getElementById("whole").offsetLeft;
var galView = document.getElementById("container").offsetWidth;

var xDist = xPos-(stageWidth/2);

galX = galX+((0-xDist)/speed);

if (galX>=0) {
galX = 0;
}
if (galX<=galView-galWidth) {
galX = galView-galWidth;
}

document.getElementById("whole").style.left=galX+"px";
}

randomuser773
06-22-2009, 05:14 PM
Your actual URL seems to be http://www.nicnok.net/html/gallery/gallery.html

The only information you need is the last reported position of the mouse, which you poll periodically. http://scripterlative.com?cursordivscroll

nschultz
06-22-2009, 06:58 PM
Sorry about the url, I changed it after my initial post. As you may or may not be able to tell, I am very new to javascript. I copied and translated my code from Actionscript, and I appreciate the link, but I cannot see where it's solution lies. I can't figure out why Firefox will only recognize the event's property inside a onmousemove handler and ie won't recognize the .clientX at all. I appreciate what help I've been given so far tho, and only wish I had more to offer in return. Thanks guys.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum