Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-19-2009, 03:00 AM   PM User | #1
nschultz
New to the CF scene

 
Join Date: Jun 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
nschultz is an unknown quantity at this point
Where do I put setInverval?

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

Code:
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";
}
nschultz is offline   Reply With Quote
Old 06-19-2009, 04:45 AM   PM User | #2
ninnypants
Regular Coder

 
ninnypants's Avatar
 
Join Date: Apr 2008
Location: Utah
Posts: 504
Thanks: 10
Thanked 47 Times in 47 Posts
ninnypants is an unknown quantity at this point
you should be able to just replace this:
Code:
document.onmousemove = CurPos;
With:
Code:
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
ninnypants is offline   Reply With Quote
Old 06-22-2009, 03:59 PM   PM User | #3
nschultz
New to the CF scene

 
Join Date: Jun 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
nschultz is an unknown quantity at this point
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.

Code:
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";
}
nschultz is offline   Reply With Quote
Old 06-22-2009, 05:14 PM   PM User | #4
randomuser773
Banned

 
Join Date: Nov 2008
Location: not found
Posts: 284
Thanks: 0
Thanked 53 Times in 51 Posts
randomuser773 can only hope to improve
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
randomuser773 is offline   Reply With Quote
Old 06-22-2009, 06:58 PM   PM User | #5
nschultz
New to the CF scene

 
Join Date: Jun 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
nschultz is an unknown quantity at this point
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.
nschultz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:50 PM.


Advertisement
Log in to turn off these ads.