Log in

View Full Version : How to fade in/out images



Roger1941
08-01-2009, 04:29 PM
Hi
Gratefull for help in achieving a fade in and out effect on image enlarge on mouseover scripts.

Many thanks

Roger

vwphillips
08-01-2009, 07:34 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
// Basic Element Animator (14-May-2009)
// by Vic Phillips http://www.vicsjavascripts.org.uk

// To progressively change the Left, Top, Width, Height or Opacity of an element over a specified period of time.
// With the ability to scale the effect time on specified minimum/maximum values
// and with three types of progression 'sin' and 'cos' and liner and an optional 'Bounce'.

// **** Application Notes

// **** The HTML Code
//
// when moving an element the inline or class rule style position of the element should be assigned as
// 'position:relative;' or 'position:absolute;'.
//
// The element would normally be assigned a unique ID name.
//

// **** Executing the Effect(Script)
//
// The effect is executed by an event call to function 'zxcBAnimator('width#',document.getElementById('tst'),10,800,5000,[10,800],'sin');'
// where:
// parameter 0 = the mode(see Note 2). (string)
// parameter 1 = the unique ID name or element object. (string or element object)
// parameter 2 = the start position of the effect. (digits, for opacity minimum 0, maximum 100)
// parameter 3 = the finish position of the effect. (digits, for opacity minimum 0, maximum 100)
// parameter 4 = (optional) period of time between the start and finish of the effect in milliseconds. (digits or defaults to 2000 milliSeconds)
// parameter 5 = (optional) to scale the effect time on a specified minimum/maximum. (array, see Note 5)
// field 0 the minimum. (digits)
// field 1 the maximum. (digits)
// parameter 6 = (optional) the type of progression, 'sin', 'cos' or 'liner'. (string, default = 'liner')
// 'sin' progression starts fast and ends slow.
// 'cos' progression starts slow and ends fast.
//
// Note 1: The default units(excepting opacity) are 'px'.
// Note 2: Examples modes: 'left', 'top', 'width', 'height', 'opacity.
// For hyphenated modes, the first character after the hyphen must be upper case, all others lower case.
// Note 3: To 'toggle' the effect include '#' in parameter 0.
// The first call will set the toggle parameters.
// Subsequent calls with '#' in parameter 0 and the same start and finish parameters will 'toggle' the effect.
// Note 4: The function may be re-executed with a different set of parameters (start/finish time or period)
// whenever required, say from an onclick/mouseover/out event.
// The period parameter will be retained unless re-specified.
// Note 5: parameter 5 is of particular use when re-calling the effect
// in mid travel to retain an constant rate of progression.
// Note 6: parameters 2 and 3 must be different values to execute the script.
//

// **** Advanced Applications
//
// Calling function 'zxcBAnimator' returns the instance of the script,
// this may be assigned to a variable and used to access the current value of the effect or to control the effect.
// alternatively the script instance by elementobject[mode.replace(/\W/g,'')+'oop'];
// where mode is parameter 0 of the initial call.
// An array storing the current, start and finish values of the element effect may be accessed
// from the element [instance].data as fields 0, 1 and 2 respectively where [instance] is the instance of the script.
//
// Once initialised the effect may be updated by calling function
// [instance].update([100,200])
// where:
// parameter 0 = an array defining the start and finish values. (array)
// field[0] =the start position of the effect. (digits, for opacity minimum 0, maximum 100)
// field[0] = the finish position of the effect. (digits, for opacity minimum 0, maximum 100)
// parameter 1 = (optional) period of time between the start and finish of the effect in milliseconds. (digits or defaults to 2000 milliSeconds)
// parameter 2 = (optional) to scale the effect time on a specified minimum/maximum. (array, see Note 5)
// field 0 the minimum. (digits)
// field 1 the maximum. (digits)
// parameter 3 = (optional) the type of progression, 'sin', 'cos' or 'liner'. (string, default = 'liner')
// 'sin' progression starts fast and ends slow.
// 'cos' progression starts slow and ends fast.
//
// ** Bounce
// A 'bounce' effect may be applied by assigning the script instance property 'Bounce' an array
// defining the 'bounce' properties.
// e.g.
// [instance].Bounce=[100,90,4,200];
// where:
// field[0] = the 'bounce' maximum. (digits)
// field[1] = the 'bounce' minimum. (digits)
// field[2] = the number of bounces. (digits)
// field[3] = the 'bounce' duration. (digits)
//


// **** General
//
// Function names are prefixed with 'zxc' to minimise conflicts with other JavaScripts.
// These characters may be changed to characters of choice using global find and replace.
//
// The Functional Code, about 2.41K with 'bounce' and 'opacity' or 1.97K without is best as an External JavaScript.
//
// Tested with IE7 and Mozilla FireFox on a PC.
//

// **** Functional Code - NO NEED to Change


function zxcBAnimator(mde,obj,srt,fin,ms,scale,curve){
if (typeof(obj)=='string'){ obj=document.getElementById(obj); }
if (!obj) return;
var oop=obj[mde.replace(/\W/g,'')+'oop'];
if (oop){
if (oop.srtfin[0]==srt&&oop.srtfin[1]==fin&&mde.match('#')) oop.update([oop.data[0],(oop.srtfin[0]==oop.data[2])?fin:srt],ms,scale,curve);
else oop.update([srt,fin],ms,scale,curve);
}
else oop=obj[mde.replace(/\W/g,'')+'oop']=new zxcBAnimatorOOP(mde,obj,srt,fin,ms,scale,curve);
return oop;
}

function zxcBAnimatorOOP(mde,obj,srt,fin,ms,scale,curve){
this.srtfin=[srt,fin];
this.to=null;
this.obj=obj;
this.mde=mde.replace(/\W/g,'');
this.update([srt,fin],ms,scale,curve);
}

zxcBAnimatorOOP.prototype.update=function(srtfin,ms,scale,curve){
clearTimeout(this.to);
this.time=ms||this.time||2000;
this.data=[srtfin[0],srtfin[0],srtfin[1]];
this.mS=this.time*(!scale?1:Math.abs((srtfin[1]-srtfin[0])/(scale[1]-scale[0])));
this.ms=this.mS;
this.curve=(typeof(curve)=='string')?curve.charAt(0).toLowerCase():(this.curve)?this.curve:'x';
this.inc=Math.PI/(2*this.mS);
this.srttime=new Date().getTime();
this.cng();
}

zxcBAnimatorOOP.prototype.cng=function(){
this.ms=new Date().getTime()-this.srttime;
this.data[0]=(this.curve=='s')?Math.floor((this.data[2]-this.data[1])*Math.sin(this.inc*this.ms)+this.data[1]):(this.curve=='c')?(this.data[2])-Math.floor((this.data[2]-this.data[1])*Math.cos(this.inc*this.ms)):(this.data[2]-this.data[1])/this.mS*this.ms+this.data[1];
this.apply();
if (this.ms<this.mS) this.to=setTimeout(function(oop){return function(){oop.cng();}}(this),10);
else {
this.data[0]=this.data[2];
this.apply();
if (this.Bounce&&this.Bounce[2]>0) this.bounce();
}
}

zxcBAnimatorOOP.prototype.apply=function(){
if (isFinite(this.data[0])){
if (this.mde!='left'&&this.mde!='top'&&this.data[0]<0) this.data[0]=0;
if (this.mde!='opacity') this.obj.style[this.mde]=this.data[0]+'px';
else zxcOpacity(this.obj,this.data[0]);
}
}

zxcBAnimatorOOP.prototype.bounce=function(){
if (this.Bounce[2]%2==0)
this.Bounce[1]+=(this.Bounce[0]-this.Bounce[1])/(this.Bounce[2])
this.update([this.data[0],this.Bounce[this.Bounce[2]%2==0?1:0]],this.Bounce[3]/this.Bounce[2]);
this.Bounce[2]--;
}

function zxcOpacity(obj,opc){
if (opc<0||opc>100) return;
obj.style.filter='alpha(opacity='+opc+')';
obj.style.opacity=obj.style.MozOpacity=obj.style.KhtmlOpacity=opc/100-.001;
}

/*]]>*/
</script>

</head>

<body>
<img id="tst" src="http://www.vicsjavascripts.org.uk/StdImages/Egypt10.jpg" />
<input type="button" name="" value="Toggle Fade" onclick="zxcBAnimator('opacity#','tst',100,0,2000)"/>
</body>

</html>

you will need to expaine your requirement more fully

Roger1941
08-02-2009, 10:58 AM
Hi Vic. Many thanks for your reply, I have tried it but not quite what I am looking for.

To explain more clearly what I am looking for:-

Could you maybe take a look at www.ownersdirectspain.org/tajsell.htm . On mouse over the images individually enlarge. But its all very abrupt in presentation and I am looking to have the enlarged images fade in and out slowly.

Regards

Roger

vwphillips
08-02-2009, 12:39 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
.thumb {
width:70px;height:50px;
}

#Main {
position:relative;width:240px;height:200px;
}

#Main IMG {
position:absolute;left:0px;top:0px;width:240px;height:200px;
}

/*]]>*/
</style>

<script src="http://www.vicsjavascripts.org.uk/AnimatorBasic/AnimatorBasic.js" type="text/javascript">


</script>
<script type="text/javascript">
/*<![CDATA[*/

function Swap(main,tcls,ms){
var imgs=document.getElementById(main).getElementsByTagName('IMG');
var thumbs=zxcByClassName(tcls);
this.oops=[]
for (var z0=imgs.length-1;z0>=0;z0--){
imgs[z0].style.zIndex=z0!=imgs.length-1?'0':'1';
this.oops.push(zxcBAnimator('opacity',imgs[z0],50,z0!=imgs.length-1?0:100,10));
this.addevt(thumbs[z0],'mouseover','swap',z0);
}
this.lst=0;
this.ms=ms||1000;
}

Swap.prototype.swap=function(e,nu){
this.oops[this.lst].obj.style.zIndex='0';
this.oops[this.lst].update([this.oops[this.lst].data[0],0],this.ms);
this.lst=nu;
this.oops[this.lst].obj.style.zIndex='1';
this.oops[this.lst].update([this.oops[this.lst].data[0],100],this.ms);
}


Swap.prototype.addevt=function(o,t,f,p){
var oop=this;
if (o.addEventListener) o.addEventListener(t,function(e){ return oop[f](e,p);}, false);
else if (o.attachEvent) o.attachEvent('on'+t,function(e){ return oop[f](e,p); });
else {
var prev=o['on'+t];
if (prev) o['on'+t]=function(e){ prev(e); oop[f](e,p); };
else o['on'+t]=o[f];
}
}

function zxcByClassName(nme,el,tag){
if (typeof(el)=='string') el=document.getElementById(el);
el=el||document;
for (var tag=tag||'*',reg=new RegExp('\\b'+nme+'\\b'),els=el.getElementsByTagName(tag),ary=[],z0=0; z0<els.length;z0++){
if(reg.test(els[z0].className)) ary.push(els[z0]);
}
return ary;
}



/*]]>*/
</script>

</head>

<body onload="S=new Swap('Main','thumb',1000)">
<img class="thumb" src="http://www.ownersdirectspain.org/Taj13.jpg" />
<img class="thumb" src="http://www.ownersdirectspain.org/Taj12.jpg" />
<img class="thumb" src="http://www.ownersdirectspain.org/Taj9.jpg" />
<div id="Main" >
<img src="http://www.ownersdirectspain.org/Taj9.jpg" />
<img src="http://www.ownersdirectspain.org/Taj12.jpg" />
<img src="http://www.ownersdirectspain.org/Taj13.jpg" />
</div>

</body>

</html>

Roger1941
08-02-2009, 12:44 PM
Hi again Vic.

Have been looking at your web site, http://www.vicsjavascripts.org.uk/ImageExpand/ImageExpand.htm?1249212588109 this script looks perfect for what I want to achieve.

Problem is that having copied and pasted it into Frontpage I cannot get it to run. I am clearly doing something (or more likely many) things wrong.

Does part of the script go in the <head> and part in the <body>? Grateful for any help you can give.

Regards

Roger

vwphillips
08-02-2009, 01:30 PM
the script would normally be positioned in the <HEAD></HEAD> tags

or immediately before the </BODY> tag

also see

http://www.vicsjavascripts.org.uk/ElementMoveExpand/ElementMoveExpand.htm

Roger1941
08-02-2009, 04:09 PM
Hi Vic
Very many thanks for all your help. The last script is just that I was looking for. I am at the moment working on www.amateur-painting.org, but will use your script on www.ownersdirectspain.org also if that is OK with you?

More help if you dont mind! Maybe you could take a look at :-
http://www.amateur-painting.org/new_page_3.htm Is it possible to include a description of the enlarged images?.

Regards

Roger

vwphillips
08-03-2009, 10:32 AM
but will use your script

Please leave all credits intact



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
.thumb {
width:70px;height:50px;
}

#Main {
position:relative;width:400px;height:300px;
}

#Main IMG {
position:absolute;left:0px;top:0px;width:400px;height:300px;
}

#Text {
position:relative;width:400px;height:30px;
}

.txt {
position:absolute;left:0px;top:0px;width:400px;height:30px;background-Color:#FFFFCC;text-Align:center;
}
/*]]>*/
</style>

<script src="http://www.vicsjavascripts.org.uk/AnimatorBasic/AnimatorBasic.js" type="text/javascript">


</script>
<script type="text/javascript">
/*<![CDATA[*/

function Swap(main,tcls,ms){
var clds=document.getElementById(main).childNodes;
var imgs=[];
for (var z0=0;z0<clds.length;z0++){
if (clds[z0].nodeType==1) imgs.push(clds[z0]);
}
var thumbs=zxcByClassName(tcls);
this.oops=[]
for (var z1=imgs.length-1;z1>=0;z1--){
imgs[z1].style.zIndex=z1!=imgs.length-1?'0':'1';
this.oops.push(zxcBAnimator('opacity',imgs[z1],50,z1!=imgs.length-1?0:100,10));
this.addevt(thumbs[z1],'mouseover','swap',z1);
}
this.lst=0;
this.ms=ms||1000;
}

Swap.prototype.swap=function(e,nu){
this.oops[this.lst].obj.style.zIndex='0';
this.oops[this.lst].update([this.oops[this.lst].data[0],0],this.ms);
this.lst=nu;
this.oops[this.lst].obj.style.zIndex='1';
this.oops[this.lst].update([this.oops[this.lst].data[0],100],this.ms);
}


Swap.prototype.addevt=function(o,t,f,p){
var oop=this;
if (o.addEventListener) o.addEventListener(t,function(e){ return oop[f](e,p);}, false);
else if (o.attachEvent) o.attachEvent('on'+t,function(e){ return oop[f](e,p); });
else {
var prev=o['on'+t];
if (prev) o['on'+t]=function(e){ prev(e); oop[f](e,p); };
else o['on'+t]=o[f];
}
}

function zxcByClassName(nme,el,tag){
if (typeof(el)=='string') el=document.getElementById(el);
el=el||document;
for (var tag=tag||'*',reg=new RegExp('\\b'+nme+'\\b'),els=el.getElementsByTagName(tag),ary=[],z0=0; z0<els.length;z0++){
if(reg.test(els[z0].className)) ary.push(els[z0]);
}
return ary;
}



/*]]>*/
</script>

</head>

<body background="texturesat1606845.jpg" link="#FFFF99" vlink="#99FFCC" onload="S=new Swap('Main','thumb',1000);S=new Swap('Text','thumb',1000);" body>

<p align="center"></p>
<div align="center">
<center>
<table border="0" cellpadding="6" cellspacing="0" width="100%">
<tr>
<td width="50%">
<p align="center"><font color="#FFFFFF" face="Verdana" size="4">Welcome to
Clare´s gallery</font>hhhh</p>

<p align="center"><img border="0" src="http://www.amateur-painting.org/clare.jpg" width="157" height="176"></p>
</td>
<td width="50%"><div align="center">
<center>
<p>
<div id="Main" >
<img src="http://www.amateur-painting.org/clare6.jpg" width="400" height="300" />
<img src="http://www.amateur-painting.org/clare5.jpg" width="400" height="300" />
<img src="http://www.amateur-painting.org/clare4.jpg" width="400" height="300" />
<img src="http://www.amateur-painting.org/clare3.jpg" width="400" height="300" />
<img src="http://www.amateur-painting.org/clare2.jpg" width="400" height="300" />
<img src="http://www.amateur-painting.org/clare1.jpg" width="400" height="300" />

</div>
<div id="Text" >
<div class="txt" >Image 6</div>
<div class="txt" >Image 5</div>
<div class="txt" >Image 4</div>
<div class="txt" >Image 3</div>
<div class="txt" >Image 2</div>
<div class="txt" >Image 1</div>
</div>
</center>
</div>
<div align="center">


<p>
<div align="center">
<center>


<img class="thumb" src="http://www.amateur-painting.org/clare1.jpg" width="1760" height="880" />

<img class="thumb" src="http://www.amateur-painting.org/clare2.jpg" width="1416" height="1180" />

<img class="thumb" src="http://www.amateur-painting.org/clare3.jpg" width="1548" height="732" />


<img class="thumb" src="http://www.amateur-painting.org/clare4.jpg" width="2098" height="1438" />

<img class="thumb" src="http://www.amateur-painting.org/clare5.jpg" width="1591" height="1196" />

<img class="thumb" src="http://www.amateur-painting.org/clare6.jpg" width="566" height="389" />

</center>
</td>
</tr>
</table>
</center>
</div>

</body>

Roger1941
08-03-2009, 11:41 AM
Hi again Vic

And again many thanks, beginning to wander if you ever have time for paid work!

Got a problem with this one. Copied , pasted and uploaded at:-

http://www.amateur-painting.org/new_page_1.htm

I am clearly doing something wrong, only source being displayed

Regards

Roger

vwphillips
08-03-2009, 12:21 PM
you can see from the source that it has not been copied correctly

copy it again and check what you have copied

Roger1941
08-05-2009, 02:29 PM
Hi Vic
All is now working. Again, many thanks for all your help
Regards
Roger

Roger1941
08-06-2009, 10:58 AM
Vic. Many thanks for all your help, now all working well! Regards Roger