PDA

View Full Version : How to stop object movement-please help



fenix_ds
08-18-2010, 04:51 PM
1) Script Title: Floating images script

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex4/flyimage.htm

3) Describe problem: Hello, I like this script but I'm looking for a way to stop the image movement after, let's say, 10 seconds, to avoid the image to be an annoyance to the visitor. The script is in use in a demo page here: http://nutsaboutflorida.com/floatingobject.html

Thank you

Daniel

jscheuer1
08-19-2010, 06:35 AM
At the end of the moveobj.js script, change this:


document.getElementById(chip.named).style.left=chip.xx+"px";
document.getElementById(chip.named).style.top=chip.yy+"px";


chip.timer1=setTimeout("movechip('"+chip.named+"')",100);
}
}

to:


document.getElementById(chip.named).style.left=chip.xx+"px";
document.getElementById(chip.named).style.top=chip.yy+"px";

if(!chip.stop)
chip.timer1=setTimeout("movechip('"+chip.named+"')",100);
else
document.getElementById(chip.named).style.display = 'none';
}
}

Then in the on page portion of the script, add the highlighted:


<script type="text/javascript">

/***********************************************
* Floating image script- By Virtual_Max (http://www.geocities.com/siliconvalley/lakes/8620)
* Modified by Dynamic Drive for various improvements
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

//Step 1: Define unique variable names depending on number of flying images (ie:3):
var flyimage1

function pagestart(){
//Step 2: Using the same variable names as 1), add or delete more of the below lines (60=width, height=80 of image):
flyimage1=new Chip("flyimage1",84,149);

//Step 3: Using the same variable names as 1), add or delete more of the below lines:
movechip("flyimage1");

setTimeout(function(){flyimage1.stop = true;}, 3000);
}

if (window.addEventListener)
window.addEventListener("load", pagestart, false)
else if (window.attachEvent)
window.attachEvent("onload", pagestart)
else if (document.getElementById)
window.onload=pagestart

</script>

Notice the red 3000. That's the number of milliseconds until the thing will stop and disappear. 3000 = 3 seconds, probably way too short a time for this, and just an example. Increase that number as desired.

fenix_ds
08-19-2010, 02:42 PM
Thank you! It works as desired, here's the link:
http://www.nutsaboutflorida.com/floatingobject.html

As a suggestion, you might publish the update on DD's script page so anyone who uses the script is able to stop the transition. I like the script and believe it has a lot of applications.

One more question: how can I modify the starting position of the image? I tried messing around with the red color values, with uncertain results. The x initial position changed, but not the y starting position.

function Chip(chipname,width,height){
this.named=chipname;
this.vx=vmin+vmax*Math.random();
this.vy=vmin+vmax*Math.random();
this.w=width+20;
this.h=height;
this.xx=0;
this.yy=0;
this.timer1=null;


I also played around with this, at the bottom of the moveobj.js document:

document.getElementById(chip.named).style.right=chip.xx+"px";document.getElementById(chip.named).style.top=chip.yy+"px";

When I changed it to .style.right the starting position moved to the center of the screen, not to the right.

However, changing .style.top to .style.bottom made it start from the bottom of the page.

jscheuer1
08-19-2010, 03:10 PM
The starting position is meant to be random:


function Chip(chipname,width,height){
this.named=chipname;
this.vx=vmin+vmax*Math.random();
this.vy=vmin+vmax*Math.random();
this.w=width+20;
this.h=height;
this.xx=0;
this.yy=0;
this.timer1=null;

You can override that and set a specific starting point by doing:


function Chip(chipname,width,height){
this.named=chipname;
this.vx=200; // initial horizontal offset
this.vy=150; // initial vertical offset
this.w=width+20;
this.h=height;
this.xx=0;
this.yy=0;
this.timer1=null;

The green parts are just comments.

But there appears to be more to it than that, because although it will start in that position, it rapidly moves to another (albeit consistent position) and then proceeds leisurely from there. When I have more time I'll look into it further. But with just the above, and a bit of trial and error, you may be able to get the effect you want.

fenix_ds
08-19-2010, 03:24 PM
Awesome! and thanks again.

We've been using DD scripts since 2003, and most of the scripts are reliable and worthy. We've used scripts from other sources too, but the ones from Dynamic Drive are the ones who lasted through browser updates and other changes.

Keep up the good work.

Daniel

jscheuer1
08-19-2010, 07:09 PM
I did look at this some more. I was running off before for a number of pre-scheduled things.

Anyways, it looks as though your original idea works:


function Chip(chipname,width,height){
this.named=chipname;
this.vx=vmin+vmax*Math.random();
this.vy=vmin+vmax*Math.random();
this.w=width+20;
this.h=height;
this.xx=300; // initial horizontal offset
this.yy=150; // initial vertical offset
this.timer1=null;

I'm not sure why it didn't when you tried it.