adding a fade-away to an imageswapper - I dont know how to usesetTimeout

W

windandwaves

It might be a good idea to post lines 42 and 86 (and others that
relate to them.)

The whole page is shown below. I have added some comments to LINE 42
and 86.

Cheers

Nicolaas

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/
html4/strict.dtd">
<html>
<head>
<title>Sunny Side Up Webdesign - ImageChange</title>
<script type="text/javascript">
/* <![CDATA[ */
var exOwner;
var exOptionNumber;
var minNumber = 1;
var maxNumber = 5;
var currentNumber = 1;
function showMe(up) {
//hide old one...
var oldElName = "option" + currentNumber;
if(up == 1) {
currentNumber++;
}
else {
currentNumber--;
}
var newElName = "option" + currentNumber;
if(currentNumber > maxNumber) {
currentNumber = maxNumber;
}
if(currentNumber < minNumber) {
currentNumber = minNumber;
}
if(currentNumber == minNumber) {
//hide previous arrow
document.getElementById("previousArrow").style.display = "none";
}
else {
document.getElementById("previousArrow").style.display = "block";
}
if(currentNumber == maxNumber) {
//hide up arrow
document.getElementById("nextArrow").style.display = "none";
}
else {
document.getElementById("nextArrow").style.display = "block";
}
//startFade(oldElName, 99, 1, 30, 1000); // THIS IS LINE 42 THAT
DOES NOT WORK!
document.getElementById(oldElName).style.display = "none";
document.getElementById(newElName).style.display = "block";
startFade(newElName, 1, 99, 30, 1000);
return true;
}

//you can set these variables
var totalTime = 1000;//in milliseconds
var startOpacity = 0.00; // starting point for fade
var stopOpacity = 99.9990; //end point for fade
var numberOfSteps = 30; //number of steps

//other variables
var speed; //in milliseconds
var opacity; //starting point
var step; //interval for each one
var div;
var startOpacity;

function startFade(elName, startOpacity, stopOpacity, numberOfSteps,
totalTime){
opacity = startOpacity;
step = (stopOpacity - startOpacity) / numberOfSteps;
speed = totalTime / numberOfSteps;
div = document.getElementById(elName);
repeatFade();
return true;
}


function repeatFade() {
opacity += step;
opacity = Math.round(opacity * 1000) / 1000;
var last = false;
if(Math.abs(stopOpacity - opacity) < Math.abs(step)) {
opacity = stopOpacity;
last = true;
}
if(opacity >= 0 && opacity <= 100) {
div.style.filter = "alpha(opacity="+Math.round(opacity)+")";// IE/
Winfilter: alpha(opacity=50)
div.style.KHTMLOpacity = opacity/100;// Safari<1.2, Konqueror
div.style.MozOpacity = opacity/100;// Older Mozilla and Firefox
div.style.opacity = opacity/100;// Safari 1.2, newer Firefox and
Mozilla, CSS3
if(last == false) {
window.setTimeout("repeatFade()", speed); // HERE IS LINE 86 -
WHICH I THINK ALLOWS THE JAVASCRIPT THAT ORIGINALLY CALLED IT TO
CONTINUE ITS COURSE WITHOUT WAITING FOR THE FADE OUT TO BE EXECUTED
(AS CALLED IN LINE 42)
}
}
}


/* ]]> */
</script>
<style type="text/css" media="all">
body {margin: auto; padding: 100px; width: 370px;}
.hideForNow { display: none; }
#arrows a {display: block; width: 70px; float: left;}
</style>
</head>
<body onload="">
<div id="imagePlaceholder">
<div id="option1" class="hideForNow" style="display: block;"><img
src="../../i/004.jpg" alt="image one"><br />caption for image one</
div>

<div id="option2" class="hideForNow"><img src="../../i/021.jpg"
alt="image two"><br />caption for image two</div>
<div id="option3" class="hideForNow"><img src="../../i/022.jpg"
alt="image three"><br />caption for image three</div>
<div id="option4" class="hideForNow"><img src="../../i/023.jpg"
alt="image four"><br />caption for image four</div>
<div id="option5" class="hideForNow"><img src="../../i/025.jpg"
alt="image five"><br />caption for image five</div>
</div>
<div id="arrows">
<a href="#" onclick="return !showMe(0);"
id="previousArrow">previous</a>

<a href="#" onclick="return !showMe(1);" id="nextArrow">next</a>
</div>

</body>
</html>
 
D

David Mark

It might be a good idea to post lines 42 and 86 (and others that
relate to them.)

The whole page is shown below.  I have added some comments to LINE 42
and 86.

Cheers

Nicolaas

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/
html4/strict.dtd">
<html>
<head>
 <title>Sunny Side Up Webdesign - ImageChange</title>
 <script type="text/javascript">
  /* <![CDATA[ */

Lose that.
 var exOwner;
 var exOptionNumber;
 var minNumber = 1;
 var maxNumber = 5;
 var currentNumber = 1;
 function showMe(up) {
  //hide old one...
  var oldElName = "option" + currentNumber;
  if(up == 1) {
   currentNumber++;
  }
  else {
   currentNumber--;
  }
  var newElName = "option" + currentNumber;
  if(currentNumber > maxNumber) {
   currentNumber = maxNumber;
  }
  if(currentNumber < minNumber) {
   currentNumber = minNumber;
  }
  if(currentNumber == minNumber) {
   //hide previous arrow
   document.getElementById("previousArrow").style.display = "none";
  }
  else {
   document.getElementById("previousArrow").style.display = "block";

var el = document.getElementById("previousArrow");
if (el) { el.style.display = (currentNumber ==
minNumber)?'none':'block'; }
  }
  if(currentNumber == maxNumber) {
   //hide up arrow
   document.getElementById("nextArrow").style.display = "none";
  }
  else {
   document.getElementById("nextArrow").style.display = "block";
  }

Same here.
  //startFade(oldElName, 99, 1, 30, 1000);  // THIS IS LINE 42 THAT
DOES NOT WORK!

DOES NOT WORK in what way?
  document.getElementById(oldElName).style.display = "none";
  document.getElementById(newElName).style.display = "block";
  startFade(newElName, 1, 99, 30, 1000);
  return true;
 }

 //you can set these variables
 var totalTime = 1000;//in milliseconds
 var startOpacity = 0.00; // starting point for fade
 var stopOpacity = 99.9990; //end point for fade
 var numberOfSteps = 30; //number of steps

Did you download this script?
 //other variables
 var speed; //in milliseconds
 var opacity; //starting point
 var step; //interval for each one

Effects should progress based on time, not steps.
 var div;
 var startOpacity;

 function startFade(elName, startOpacity, stopOpacity, numberOfSteps,
totalTime){
  opacity = startOpacity;
  step = (stopOpacity - startOpacity) / numberOfSteps;
  speed = totalTime / numberOfSteps;
  div = document.getElementById(elName);
  repeatFade();
  return true;
 }

 function repeatFade() {
  opacity += step;
  opacity = Math.round(opacity * 1000) / 1000;
Why?

  var last = false;
  if(Math.abs(stopOpacity - opacity) < Math.abs(step)) {
   opacity = stopOpacity;
   last = true;
  }
  if(opacity >= 0 && opacity <= 100) {

This doesn't make sense.
   div.style.filter = "alpha(opacity="+Math.round(opacity)+")";// IE/
Winfilter: alpha(opacity=50)

Why all the rounding? Opacity does not have to be an integer.
   div.style.KHTMLOpacity = opacity/100;// Safari<1.2, Konqueror
   div.style.MozOpacity = opacity/100;// Older Mozilla and Firefox
   div.style.opacity = opacity/100;// Safari 1.2, newer Firefox and
Mozilla, CSS3

This is awful. See the setOpacity function in the CWR project.
   if(last == false) {
    window.setTimeout("repeatFade()", speed); // HERE IS LINE 86 -
WHICH I THINK ALLOWS THE JAVASCRIPT THAT ORIGINALLY CALLED IT TO
CONTINUE ITS COURSE WITHOUT WAITING FOR THE FADE OUT TO BE EXECUTED
(AS CALLED IN LINE 42)

I realize that browser scripting can be frustrating, but there is no
need to shout.
   }
  }
 }

 /* ]]> */

Lose that.
 </script>
 <style type="text/css" media="all">
  body {margin: auto; padding: 100px; width: 370px;}
  .hideForNow { display: none;  }

Bad idea. You may hide it forever. You need to test that the agent
supports scripted updates to the display style. A typical example:

var el = getAnElement(); // See CWR project
if (el && el.style && typeof el.style.display == 'string') {
document.write('<style type="text/css" media="all">.hideForNow
  #arrows a {display: block; width: 70px; float: left;}
Why?

 </style>
</head>
<body onload="">

What is the onload attribute for?
 <div id="imagePlaceholder">
  <div id="option1" class="hideForNow" style="display: block;"><img

You can lose the style attribute.
src="../../i/004.jpg" alt="image one"><br />caption for image
one</

div>

  <div id="option2" class="hideForNow"><img src="../../i/021.jpg"
alt="image two"><br />caption for image two</div>
  <div id="option3" class="hideForNow"><img src="../../i/022.jpg"
alt="image three"><br />caption for image three</div>
  <div id="option4" class="hideForNow"><img src="../../i/023.jpg"
alt="image four"><br />caption for image four</div>
  <div id="option5" class="hideForNow"><img src="../../i/025.jpg"
alt="image five"><br />caption for image five</div>
 </div>
 <div id="arrows">
  <a href="#" onclick="return !showMe(0);"
id="previousArrow">previous</a>

  <a href="#" onclick="return !showMe(1);" id="nextArrow">next</a>

These shouldn't be in the markup. They won't do anything when
scripting is disabled. Also, you shouldn't create them unless the
previously mentioned display style test passes.
 </div>

</body>
</html>

I looked at the page. It sort of works in FF (has at least one fade
effect per swap), but has no fade effect in IE7. The IE7 issue is
easily fixed (set div.style.zoom = 1.) Again, use the setOpacity
function in the CWR project as it handles this (and other) issues for
you. AFAIK, it is the only competent opacity-related function on the
Web (the rest use browser sniffing and/or do far too much work on each
call.)

What else "doesn't work" as expected? You will have to be more
specific.
 
W

windandwaves

Hi,

Thank you for all your comments. I have changed the page to the one
below. I have also loaded it. I have not implemented all your
comments, even though I agreed with all of them.

Also see:
http://www.sunnysideup.co.nz/j/imageSwapper/index.html

Thank you again

Nicolaas


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/
html4/strict.dtd">
<html>
<head>
<title>Sunny Side Up Webdesign - ImageChange</title>
<script type="text/javascript">

var exOptionNumber;
var minNumber = 1;
var maxNumber = 5;
var currentNumber = 1;
var loaded = false;

function loader() {
document.getElementById("nextArrow").style.display = "block";
document.getElementById("previousArrow").style.display =
"block";
document.getElementById("option1").style.display ="block";
loaded = true;
}

function showMe(up) {
//hide old one...
if(!loaded) {
loader();
}
var oldElName = "option" + currentNumber;
if(up == 1) { currentNumber++;}
else {currentNumber--;}
var newElName = "option" + currentNumber;
if(currentNumber > maxNumber) {currentNumber = maxNumber;}
if(currentNumber < minNumber) {currentNumber = minNumber;}
if(currentNumber == minNumber)
{document.getElementById("previousArrow").style.display = "none";}
else {document.getElementById("previousArrow").style.display =
"block";}
if(currentNumber == maxNumber)
{document.getElementById("nextArrow").style.display = "none";}
else {document.getElementById("nextArrow").style.display =
"block";}
//startFade(oldElName, 99, 1, 30, 1000);
document.getElementById(oldElName).style.display = "none";
document.getElementById(newElName).style.display = "block";
startFade(newElName, 0.1, 0.9, 30, 1000);
return true;
}



function startFade(elName, startOpacity, stopOpacity,
numberOfSteps, totalTime){
opacity = startOpacity;
step = (stopOpacity - startOpacity) / numberOfSteps;
speed = Math.round(totalTime / numberOfSteps);
div = document.getElementById(elName);
repeatFade(opacity, step, speed, div, stopOpacity);
return true;
}


function repeatFade(opacity, step, speed, div, stopOpacity) {
opacity += step;
var last = false;
if(Math.abs((stopOpacity - opacity) * 10000) < Math.abs(step *
10000)) {
opacity = stopOpacity;
last = true;
}
setOpacity(div, opacity);
if(last == false) {
window.setTimeout(
function () {
repeatFade(opacity, step, speed, div, stopOpacity);
//alert("opacity " + opacity);alert("stop Opacity "+
stopOpacity);alert("step " + step);alert("speed " + speed);
}
, speed);
}
}

var isRealObjectProperty = function(o, p) {
return !!(typeof(o[p]) == 'object' && o[p]);
};
if (typeof isRealObjectProperty == 'function' &&
isRealObjectProperty(document, 'documentElement')) {
var getAnElement = function(d) {
return (d || document).documentElement;
};
}
if (typeof getAnElement == 'function' && typeof
isRealObjectProperty == 'function') {
var setOpacity = (function() {
var html = getAnElement(),
props = ['KhtmlOpacity', 'MozOpacity', 'opacity'];
if (html && isRealObjectProperty(html, 'style')) {
if (typeof html.style.filter == 'string' &&
String.prototype.replace) {
return function(el, o) {
if (!el.currentStyle || !el.currentStyle.hasLayout) {
el.style.zoom = 1;
}
var s = el.style;
s.filter = s.filter.replace(/alpha\([^\)]*\)/gi,'') + ((o <
1) ? 'alpha(opacity='+o*100+')' : '');
};
}
for (var i=props.length; i--; ) {
var p = props;
if (typeof(html.style[p]) == 'string') {
return function(el, o) {
el.style[p] = (o < .00001) ? 0 : o;
};
}
}
}
})
();
}

function test() {
setOpacity(document.getElementById ?
document.getElementById('green') : document.all('green'), 0.5);
}



</script>
<style type="text/css" media="all">
body {margin: auto; padding: 100px; width: 370px;}
.hideForNow { display: none; }
#arrows a {display: none; width: 70px; float: left;}
</style>
</head>
<body onload="loader();">
<div id="imagePlaceholder">
<div id="option1" class="hideForNow"><img src="../../i/004.jpg"
alt="image one"><br />caption for image one</div>
<div id="option2" class="hideForNow"><img src="../../i/021.jpg"
alt="image two"><br />caption for image two</div>
<div id="option3" class="hideForNow"><img src="../../i/022.jpg"
alt="image three"><br />caption for image three</div>
<div id="option4" class="hideForNow"><img src="../../i/023.jpg"
alt="image four"><br />caption for image four</div>
<div id="option5" class="hideForNow"><img src="../../i/025.jpg"
alt="image five"><br />caption for image five</div>
</div>
<div id="arrows">
<a href="#" onclick="return !showMe(0);"
id="previousArrow">previous</a>
<a href="#" onclick="return !showMe(1);" id="nextArrow">next</a>
</div>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,415
Latest member
PeggyCramp

Latest Threads

Top