On this page, you can browse user submitted effects, and maybe add your own! Follow the format you find here:
Resizable
by V. Popovski
Can be found at Resizable
Effect Resize Extension
by Thomas Hansen, in use in Gaia Ajax Widgets
Is also a pretty nice example of how to extend classes in scriptaculous.
Effect Resize Extension
Carousel Effect
by Thomas Hansen, in use in Gaia Ajax Widgets
Ever wanted to have Flash commercials without having to use Flash. This effects creates a nice animation that makes e.g. your Images run in from right to left, one at the time if you combine them with a timer.
Carousel Effect
Moved to Drag Scrollable.
Slides open a div when a link is clicked on and slides closed the currently open div.
Usage:
Note the wrapper Div around the text drop downs
Show source
Usage:-
<a href='#' onClick="Effect.divSwap('text1','text');">Text1</a>
<a href='#' onClick="Effect.divSwap('text2','text');">Text2</a>
<a href='#' onClick="Effect.divSwap('text3','text');">Text3</a>
<div id="text" style="width:250px;height:100px;border:1px solid #E6EDFF;background-color:#E6EDFF;">
<div id="text1" style="width:250px;height:100px;border:1px solid #E8A400;background-color:white;"><div>I am text div 1</div></div>
<div id="text2" style="display:none;width:250px;height:100px;border:1px solid #E8A400;background-color:white;"><div>I am text div 2</div></div>
<div id="text3" style="display:none;width:250px;height:100px;border:1px solid #E8A400;background-color:white;"><div>I am text div 3</div></div>
</div>
Effect:-
Effect.divSwap = function(element,container){
var div = document.getElementById(container);
var nodeList = div.childNodes;
var queue = Effect.Queues.get('menuScope');
if(queue.toArray().length<1){
if(Element.visible(element)==false){
for(i=0;i<nodeList.length;i++){
if(nodeList.item(i).nodeName=="DIV" && nodeList.item(i).id!=element){
if(Element.visible(nodeList.item(i))==true){
Effect.SlideUp(nodeList.item(i),{queue:{position:'end',scope:'menuScope',limit:2}})
}
}
}
Effect.SlideDown(element,{queue:{position:'end',scope:'menuScope',limit:2}})
}
}
}
Pop Out
Instead of Drop Out and Drop In… Pop Out the reverse of Drop Out. It fades out whilst going up. By buggedcom
Show source
Effect.PopOut = function(element) {
element = $(element);
var oldTop = element.style.top;
var oldLeft = element.style.left;
var pos = Position.cumulativeOffset(element);
return new Effect.Parallel(
[ new Effect.MoveBy(element, -100, 0, { sync: true }),
new Effect.Opacity(element, { sync: true, from:1, to: 0 }) ],
Object.extend(
{ duration: 0.5,
beforeSetup: function(effect) {
Element.makePositioned(effect.effects[0].element);
Element.setOpacity(element, 1);
element.style.position = 'absolute';
// element.style.top = (pos[1]-100) + 'px';
}
}, arguments[1] || {}));
}
Vertical Shake
This is a simple modification of Effect.Shake, it shakes the element vertically, instead of horizontally. – rixth
Show source
Effect.ShakeVertical = function(element) {
element = $(element);
var oldStyle = {
top: Element.getStyle(element, 'top'),
left: Element.getStyle(element, 'left') };
return new Effect.Move(element,
{ x: 0, y: 20, duration: 0.05, afterFinishInternal: function(effect) {
new Effect.Move(effect.element,
{ x: 0, y: -40, duration: 0.1, afterFinishInternal: function(effect) {
new Effect.Move(effect.element,
{ x: 0, y: 40, duration: 0.1, afterFinishInternal: function(effect) {
new Effect.Move(effect.element,
{ x: 0, y: -40, duration: 0.1, afterFinishInternal: function(effect) {
new Effect.Move(effect.element,
{ x: 0, y: 40, duration: 0.1, afterFinishInternal: function(effect) {
new Effect.Move(effect.element,
{ x: 0, y: -20, duration: 0.05, afterFinishInternal: function(effect) { with(Element) {
undoPositioned(effect.element);
setStyle(effect.element, oldStyle);
}}}) }}) }}) }}) }}) }});
}
Drop Elements In
Drop In elements, the opposite of Drop Out….
By buggedcom
Show source
Effect.DropIn = function(element) {
element = $(element);
var oldTop = element.style.top;
var oldLeft = element.style.left;
var pos = Position.cumulativeOffset(element);
return new Effect.Parallel(
[ new Effect.MoveBy(element, 100, 0, { sync: true }),
new Effect.Opacity(element, { sync: true, from:0.0, to: 1.0 }) ],
Object.extend(
{ duration: 0.5,
beforeSetup: function(effect) {
Element.makePositioned(effect.effects[0].element);
Element.setOpacity(element, 0);
element.style.position = 'absolute';
element.style.top = (pos[1]-100) + 'px';
}
}, arguments[1] || {}));
}
Slide Right Into View, Slide Right Out Of View
By Joao Prado Maia
Show source
Effect.SlideRightIntoView = function(element) {
$(element).style.width = '0px';
$(element).style.overflow = 'hidden';
$(element).firstChild.style.position = 'relative';
Element.show(element);
new Effect.Scale(element, 100,
{ scaleContent: false,
scaleY: false,
scaleMode: 'contents',
scaleFrom: 0,
afterUpdate: function(effect)
{ effect.element.firstChild.style.width =
(effect.originalWidth - effect.element.clientWidth) + 'px'; }
}.extend(arguments[1] || {})
);
}
Effect.SlideRightOutOfView = function(element) {
$(element).style.overflow = 'hidden';
$(element).firstChild.style.position = 'relative';
Element.show(element);
new Effect.Scale(element, 0,
{ scaleContent: false,
scaleY: false,
afterUpdate: function(effect)
{ effect.element.firstChild.style.width =
(effect.originalWidth - effect.element.clientWidth) + 'px'; },
afterFinish: function(effect)
{ Element.hide(effect.element); }
}.extend(arguments[1] || {})
);
}
How do you include this code into a project to use it?
I’ve tried using it in the actual page (inside script tags) as well as including all the required script.aculo.us files – but it doesn’t work. Can someone please give an example of how to use this?
sunsean: Add the above code to the bottom of effects.js
Simple slide up/down with the same button
By Robert Gaal
Show source
var folded = new Object();
function slide(el)
{
if(folded[el] == 1) {
Effect.SlideUp(el);
folded[el] = 0;
} else {
Effect.SlideDown(el);
folded[el] = 1;
}
}
Slide up or down with the same button
this one slides up if the element is visible, and down if it is not.
Show source
# Prototype 1.3.1 or earlier
Effect.SlideUpAndDown = function(element) {
element = $(element);
if(element.style.display != 'none') new Effect.SlideUp(element);
else new Effect.SlideDown(element);
}
# Prototype 1.4.0 or later
Effect.SlideUpAndDown = function(element) {
element = $(element);
if(Element.visible(element)) new Effect.SlideUp(element);
else new Effect.SlideDown(element);
}
move AND resize an element
pass me the desired most outer bouding box position and dimensions. calculations includes margin/border/paddings, so when you know you want a div positioned
and sized a specific way, this will position and animate the transition. set duration=0 for no aninmation. note you will need to click ‘edit’ to view the real source! the stupid wiki interpreter is parsing asterisks as bold text dispite being inside a code block!
by jake richardson {jaecob.gmail.com}
Link to Class Source – {jason.at.shaped.ca} hosting this to hopefully save someone from having to sign up at the wiki to view the source properly.
Show source
Effect.MoveAndResizeTo = Class.create();
Object.extend(Object.extend(Effect.MoveAndResizeTo.prototype, Effect.Base.prototype), {
initialize: function(element, toTop, toLeft, toWidth, toHeight) {
this.element = $(element);
this.toTop = toTop;
this.toLeft = toLeft;
this.toWidth = toWidth;
this.toHeight = toHeight;
this.originalTop = parseFloat(Element.getStyle(this.element,'top') || 0);
this.originalLeft = parseFloat(Element.getStyle(this.element,'left') || 0);
this.originalWidth = parseFloat(Element.getStyle(this.element,'width') || 0);
this.originalHeight = parseFloat(Element.getStyle(this.element,'height') || 0);
this.effectiveTop = this.toTop;
this.effectiveLeft = this.toLeft;
this.effectiveWidth = this.toWidth
- parseFloat(Element.getStyle(this.element,'margin-left') || 0)
- parseFloat(Element.getStyle(this.element,'margin-right') || 0)
- (document.compatMode == 'BackCompat' ? 0 : // height includes padding & border in IE BackCompat mode
parseFloat(Element.getStyle(this.element,'padding-left') || 0)
+ parseFloat(Element.getStyle(this.element,'padding-right') || 0)
+ parseFloat(Element.getStyle(this.element,'border-left-width') || 0)
+ parseFloat(Element.getStyle(this.element,'border-right-width') || 0));
this.effectiveHeight = this.toHeight
- parseFloat(Element.getStyle(this.element,'margin-top') || 0)
- parseFloat(Element.getStyle(this.element,'margin-bottom') || 0)
- (document.compatMode == 'BackCompat' ? 0 : // height includes padding & border in IE BackCompat mode
parseFloat(Element.getStyle(this.element,'padding-top') || 0)
+ parseFloat(Element.getStyle(this.element,'padding-bottom') || 0)
+ parseFloat(Element.getStyle(this.element,'border-top-width') || 0)
+ parseFloat(Element.getStyle(this.element,'border-bottom-width') || 0));
this.options = arguments[5] || {};
if (this.effectiveWidth < 0) this.effectiveWidth = 0;
if (this.effectiveHeight < 0) this.effectiveHeight = 0;
if (this.originalTop this.effectiveTop &&
this.originalLeft this.effectiveLeft &&
this.originalWidth this.effectiveWidth &&
this.originalHeight this.effectiveHeight) {
// no need to start!
return;
}
this.start(this.options);
},
update: function(position) {
topd = this.effectiveTop (position) + this.originalTop (1 - position);
leftd = this.effectiveLeft (position) + this.originalLeft (1 - position);
widthd = this.effectiveWidth (position) + this.originalWidth (1 - position);
heightd = this.effectiveHeight (position) + this.originalHeight (1 - position);
this.setPosition(topd, leftd, widthd, heightd);
},
setPosition: function(topd, leftd, widthd, heightd) {
}
});
this.element.style.top = topd+'px';
this.element.style.left = leftd+'px';
this.element.style.width = widthd+'px';
this.element.style.height = heightd+'px';
Phase Elements smoothly
Phase is an implementation of smooth appearing elements. It combines the appear/fade and blind effects to create a smooth transition. Insert code at bottom of effects.js sunsean
[Updated by Daniel Vandersluis to allow for 2 properties arguments (for example, if you have an afterFinish function that needs to run only once); backwards compatibility maintained]
Show source
Effect.PhaseIn = function(element) {
element = $(element);
new Effect.BlindDown(element, arguments[1] || {});
new Effect.Appear(element, arguments[2] || arguments[1] || {});
}
Effect.PhaseOut = function(element) {
element = $(element);
new Effect.Fade(element, arguments[1] || {});
new Effect.BlindUp(element, arguments[2] || arguments[1] || {});
}
Effect.Phase = function(element) {
element = $(element);
if (element.style.display == 'none')
new Effect.PhaseIn(element, arguments[1] || {}, arguments[2] || arguments[1] || {});
else new Effect.PhaseOut(element, arguments[1] || {}, arguments[2] || arguments[1] || {});
}
Effect.Transitions.exponential
Add a new smooth transition with Effect.Transitions.exponential. This transition starts quick, but slows down near the end. Great for moving elements. sunsean
Show source
Effect.Transitions.exponential = function(pos) {
return 1-Math.pow(1-pos,2);
}
Effect.Transitions.slowstop
Add a new smooth transition with Effect.Transitions.slowstop. This transition starts very quickly, but comes to a gradual stop. Great for scrolling somewhere on a page with a duration of 2. sunsean
Show source
Effect.Transitions.slowstop = function(pos) {
return 1-Math.pow(0.5,20*pos);
}
Slide Right Into View, Slide Right Out Of View, Slide Left And Right
It’s fixed for IE and current version of scriptaculous(1.5.1) and prototype(1.4.0) versions of above functions Slide Right Into View, Slide Right Out Of View. Added simple function Slide Left And Right.
Show source
Effect.SlideRightIntoView = function(element) {
$(element).style.width = '0px';
$(element).style.overflow = 'hidden';
$(element).firstChild.style.position = 'relative';
Element.show(element);
new Effect.Scale(element, 100,
Object.extend(arguments[1] || {}, {
scaleContent: false,
scaleY: false,
scaleMode: 'contents',
scaleFrom: 0,
afterUpdate: function(effect){}
})
);
}
Effect.SlideRightOutOfView = function(element) {
$(element).style.overflow = 'hidden';
$(element).firstChild.style.position = 'relative';
Element.show(element);
new Effect.Scale(element, 0,
Object.extend(arguments[1] || {}, {
scaleContent: false,
scaleY: false,
afterUpdate: function(effect){},
afterFinish: function(effect)
{ Element.hide(effect.element); }
})
);
}
Effect.SlideLeftAndRight = function(element) {
element = $(element);
if(Element.visible(element)) new Effect.SlideRightOutOfView(element);
else new Effect.SlideRightIntoView(element);
}
Can you show a code-example using this effect?
modifying Effects.Blind Up and Effects.Blind Down to make, Blind Left and Blind Right
BlindUp to BlindLeft (changes to make in Effects.js)
// CHANGES MADE FROM BLINDUP
// CHANGE from 'scaleX: false,' to scaleY: false
Effect.BlindLeft = function(element) {
element = $(element);
element.makeClipping();
return new Effect.Scale(element, 0,
Object.extend({ scaleContent: false,
scaleY: false,
restoreAfterFinish: true,
afterFinishInternal: function(effect) {
effect.element.hide().undoClipping();
}
}, arguments[1] || {})
);
}
BlindDown to BlindRight (changes to make in Effects.js)
// CHANGES MADE FROM BLINDDOWN
// CHANGE from 'scaleX: false,' to scaleY: false
// CHANGE afterSetup function statement's "makeClipping().setStyle({height: '0px'})" to width: '0px'
Effect.BlindRight = function(element) {
element = $(element);
var elementDimensions = element.getDimensions();
return new Effect.Scale(element, 100, Object.extend({
scaleContent: false,
scaleY: false,
scaleFrom: 0,
scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
restoreAfterFinish: true,
afterSetup: function(effect) {
effect.element.makeClipping().setStyle({width: '0px'}).show();
},
afterFinishInternal: function(effect) {
effect.element.undoClipping();
}
}, arguments[1] || {}));
}
Usage:
Effects.BlindRight($('someDivId')); //shows the div
Effects.BlindLeft($('someDivId')); //hides the div
by Harish palaniappan
Center Divs
You can center divs with this just
add this into effects.js
fully edited effects.js
http://www.icefuzion.net/area51/new_site/test_templates/javascript/src/effects.js
Show source
Effect.Center = function(element)
{
try
{
element = $(element);
}
catch(e)
{
return;
}
var my_width = 0;
var my_height = 0;
if ( typeof( window.innerWidth ) == 'number' )
{
my_width = window.innerWidth;
my_height = window.innerHeight;
}
else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
{
my_width = document.documentElement.clientWidth;
my_height = document.documentElement.clientHeight;
}
else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
{
my_width = document.body.clientWidth;
my_height = document.body.clientHeight;
}
element.style.position = 'absolute';
element.style.display = 'block';
element.style.zIndex = 99;
var scrollY = 0;
if ( document.documentElement && document.documentElement.scrollTop )
{
scrollY = document.documentElement.scrollTop;
}
else if ( document.body && document.body.scrollTop )
{
scrollY = document.body.scrollTop;
}
else if ( window.pageYOffset )
{
scrollY = window.pageYOffset;
}
else if ( window.scrollY )
{
scrollY = window.scrollY;
}
var elementDimensions = Element.getDimensions(element);
var setX = ( my_width - elementDimensions.width ) / 2;
var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;
setX = ( setX < 0 ) ? 0 : setX;
setY = ( setY < 0 ) ? 0 : setY;
element.style.left = setX + "px";
element.style.top = setY + "px";
}
Here is an example of when to use:
Show source for example
function test()
{
Effect.Center('loadertag');
Effect.Appear('loadertag');
var ret = function(t)
{
Effect.Puff('loadertag');
var dyn = document.getElementById('dyncontent');
dyn.innerHTML = t.responseText;
}
var errFunc = function(t)
{
alert('error');
}
new Ajax.Request('mobo.txt', {method:'get', onSuccess:ret, onFailure:errFunc});
}
This gives a nice effect if you want a centered loading div :P
by Bhavesh Ramburn
Slide Right and Slide Left
Another implementation of this functionality, works better for me with the latest version of script.aculo.us
by Jeff Pennal
Show source
Effect.SlideRight = function(element) {
element = $(element);
Element.cleanWhitespace(element);
// SlideDown need to have the content of the element wrapped in a container element with fixed height!
var oldInnerRight = Element.getStyle(element.firstChild, 'right');
var elementDimensions = Element.getDimensions(element);
return new Effect.Scale(element, 100, Object.extend({
scaleContent: false,
scaleY: false,
scaleFrom: 0,
scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
restoreAfterFinish: true,
afterSetup: function(effect) { with(Element) {
makePositioned(effect.element);
makePositioned(effect.element.firstChild);
if(window.opera) setStyle(effect.element, {top: ''});
makeClipping(effect.element);
setStyle(effect.element, {width: '0px'});
show(element); }},
afterUpdateInternal: function(effect) { with(Element) {
setStyle(effect.element.firstChild, {right:
(effect.dims[0] - effect.element.clientWidth) + 'px' }); }},
afterFinishInternal: function(effect) { with(Element) {
undoClipping(effect.element);
undoPositioned(effect.element.firstChild);
undoPositioned(effect.element);
setStyle(effect.element.firstChild, {right: oldInnerRight}); }}
}, arguments[1] || {})
);
}
Effect.SlideLeft = function(element) {
element = $(element);
Element.cleanWhitespace(element);
var oldInnerRight = Element.getStyle(element.firstChild, 'right');
return new Effect.Scale(element, 0,
Object.extend({ scaleContent: false,
scaleY: false,
scaleMode: 'box',
scaleFrom: 100,
restoreAfterFinish: true,
beforeStartInternal: function(effect) { with(Element) {
makePositioned(effect.element);
makePositioned(effect.element.firstChild);
if(window.opera) setStyle(effect.element, {top: ''});
makeClipping(effect.element);
show(element); }},
afterUpdateInternal: function(effect) { with(Element) {
setStyle(effect.element.firstChild, {right:
(effect.dims[0] - effect.element.clientWidth) + 'px' }); }},
afterFinishInternal: function(effect) { with(Element) {
[hide, undoClipping].call(effect.element);
undoPositioned(effect.element.firstChild);
undoPositioned(effect.element);
setStyle(effect.element.firstChild, {right: oldInnerRight}); }}
}, arguments[1] || {})
);
}
overflow: scroll hack and draggable element substitution
Enable drag scrolling for a div with overflow: scroll, and allow draggable element substitution (like custom draggable from here http://openrico.org/rico/demos.page?demo=rico_drag_and_drop_custom_draggable). The main problem for me, that scriptaculous not allow to drag element outside parent element with overflow: scroll. To fix it I added ability substitute draggable element.
Scriptaculous v1.5.3, IE 6.0 only tested
by Sergey Kojin
Show source
// extentions for scriptaculous dragdrop.js
Object.extend(Class, {
superrise: function(obj, names){
names.each( function(n){ obj['super_' + n] = obj[n] } )
return obj;
}
})
// Draggable that allows substitution of draggable element
var SubsDraggable = Class.create();
SubsDraggable.prototype = Object.extend({}, Draggable.prototype);
Class.superrise(SubsDraggable.prototype, ['initialize', 'initDrag', 'finishDrag'])
Object.extend( SubsDraggable.prototype , {
initialize: function(event) {
this.super_initialize.apply(this, arguments);
if( typeof(this.options.dragelement) == 'undefined' ) this.options.dragelement = false;
},
initDrag: function(event) {
this.dragging = true;
if( this.options.dragelement ){
this._originalElement = this.element;
this.element = this.options.dragelement(this.element);
Position.absolutize(this.element);
Position.clone(this._originalElement, this.element);
}
this.super_initDrag(event);
},
finishDrag: function(event, success) {
this.dragging = false;
this.super_finishDrag(event, success);
if( this.options.dragelement ){
Element.remove(this.element);
this.element = this._originalElement;
this._originalElement = null;
}
}
})
/////////////////////////////////
// EXAMPLE
// gets element that should be dragged instead of original element
// returned element should be added to DOM tree, and will be deleted by dragdrop library
function getDragElement(element){
var el = element.cloneNode(true);
el.id = '';
$('dragContactsHolder').appendChild(el);
//or document.body..appendChild(el), but I used some div in tree to keep element CSS classes work
if( MANY ){
el.innerHTML = "[" + MANY_N + " elements]";
}
return el;
}
new SubsDraggable('dragableElementId', {handle:'draghandle', dragelement:getDragElement});
Could you show an example of how to use this effect?
Edit 01/08/2007 by Greg Hinch – added a line to turn this.dragging on and off when calling initDrag() and finishDrag(). This was to fix an issue where if a draggable was clicked but not dragged the duplicate element would not be removed from the page.
Effect.Bounce
I have just extended the Effect.Move to let an Object bounce. It should behave physically correct. As example you can look at my website, I currently work on: siegfried.puchbauer.com (mouse over the navigation triggers the effect)
Show source
Effect.Bounce = Class.create();
Object.extend(Object.extend(Effect.Bounce.prototype, Effect.Base.prototype), {
initialize: function(element) {
this.element = $(element);
var options = Object.extend({
x:0, y:200,
acceleration: 9.81,
transition: Effect.Transitions.linear,
mode: 'relative'
}, arguments[1] || {});
this.start(options);
},
setup: function() {
Element.makePositioned(this.element);
this.originalLeft = parseFloat(Element.getStyle(this.element,'left') || '0');
this.originalTop = parseFloat(Element.getStyle(this.element,'top') || '0');
if(this.options.mode == 'absolute') {
this.options.x = this.options.x - this.originalLeft;
}
},
mytransition: function(pos){
var temp = (pos < 0.5 ? 0.5-pos : 0.5+(1-pos));
return (pos < 0.5 ? this.options.acceleration/2*temp*temp : this.options.acceleration/2*(1-temp)*(1-temp) ) *8 /this.options.acceleration - 1;
},
update: function(position) {
Element.setStyle(this.element, {
left: this.options.x * position + this.originalLeft + 'px',
top: this.originalTop + this.options.y * this.mytransition(position) + 'px'
});
}
});
Effect.Queue This
Execute any function after any queue.
Show source and example
Effect.QueueThis = Class.create();
Object.extend( Object.extend( Effect.QueueThis.prototype, Effect.Base.prototype ), {
initialize: function(element) {
this.element = $(element);
this.func = arguments[1];
this.start(arguments[2]);
},
finish: function(position){
this.func(this.element);
}
});
To use:
// This will make "myElement" first fade into view (taking 3 seconds)
new Effect.Appear("myElement", { duration: 3, queue: { position: "end", scope: "myQueue" } });
// and when that's done, this will change the foreground color to red
new Effect.QueueThis(
'myElement',
function (elem) { elem.setStyle( { color: "#f00" } ); },
{ queue: {position: "end", scope: "myQueue"} }
);
Effect.Resize To
Effect based on Effect.Move And Resize To (by jake richardson {jaecob.gmail.com}, see above), but the Move part has been removed.
Show source
Effect.ResizeTo = Class.create();
Object.extend(Object.extend(Effect.ResizeTo.prototype, Effect.Base.prototype), {
initialize: function(element, toWidth, toHeight) {
this.element = $(element);
this.toWidth = toWidth;
this.toHeight = toHeight;
this.originalWidth = parseFloat(Element.getStyle(this.element,'width') || 0);
this.originalHeight = parseFloat(Element.getStyle(this.element,'height') || 0);
this.effectiveWidth = this.toWidth
- parseFloat(Element.getStyle(this.element,'margin-left') || 0)
- parseFloat(Element.getStyle(this.element,'margin-right') || 0)
- (document.compatMode == 'BackCompat' ? 0 : // height includes padding & border in IE BackCompat mode
parseFloat(Element.getStyle(this.element,'padding-left') || 0)
+ parseFloat(Element.getStyle(this.element,'padding-right') || 0)
+ parseFloat(Element.getStyle(this.element,'border-left-width') || 0)
+ parseFloat(Element.getStyle(this.element,'border-right-width') || 0));
this.effectiveHeight = this.toHeight
- parseFloat(Element.getStyle(this.element,'margin-top') || 0)
- parseFloat(Element.getStyle(this.element,'margin-bottom') || 0)
- (document.compatMode == 'BackCompat' ? 0 : // height includes padding & border in IE BackCompat mode
parseFloat(Element.getStyle(this.element,'padding-top') || 0)
+ parseFloat(Element.getStyle(this.element,'padding-bottom') || 0)
+ parseFloat(Element.getStyle(this.element,'border-top-width') || 0)
+ parseFloat(Element.getStyle(this.element,'border-bottom-width') || 0));
this.options = arguments[3] || {};
if (this.effectiveWidth < 0) this.effectiveWidth = 0;
if (this.effectiveHeight < 0) this.effectiveHeight = 0;
if (this.originalWidth this.effectiveWidth &&
this.originalHeight this.effectiveHeight) {
return;
}
this.start(this.options);
},
update: function(position) {
widthd = this.effectiveWidth (position) + this.originalWidth (1 - position);
heightd = this.effectiveHeight (position) + this.originalHeight (1 - position);
this.setPosition(widthd, heightd);
},
setPosition: function(widthd, heightd) {
this.element.style.width = widthd+'px';
this.element.style.height = heightd+'px';
}
});
Element.firstElemental Child
I ran into some problems because many of my nodes seem to have text-type children before their elemental child. This method returns the first elemental child node.
Show source
Element.addMethods(
{
firstElementalChild : function(element)
{
element = $(element);
for(var x=0; x < element.childNodes.length; x++)
{
if(element.childNodes[x].nodeType == Node.ELEMENT_NODE)
{
return element.childNodes[x];
}
}
return;
}
} ); // end addMethods
Fade a table row
This effect will fade a table row and make it desappear, it can take options as Effect.fade would do.
tested under Fire Fox 1.5 / Internet Explorer 6 / Opera 9
Show source
/**
@param string rowid the id o fthe row you want to make disappear
@param oject opts Effect options such as {duration:3}
*/
function fadeTableRow(rowid,opts){
if(!opts){
opts = {};
}
var row = $(rowid);
var cells= row.childNodes;
for(i=0;i<cells.length;i++){
if(cells[i].tagName == 'TD'){
new Effect.Fade(cells[i],opts);
}
}
new Effect.Fade(row,opts);
}
Accordion Menu
Allows you to create a smooth accordion menu with fading expand and collapse icons. An example can be seen here.
// Created by Mike Ellan: Mike @ F I G I T D O T C O M
Show source
fadeDur = 0.15 // Fade out duration
AppearDur = 0.15 // Fade in duration
SlideDur = 0.3 // Panel slide duration
Effect.OpenAndCloseModule = function(detectId,openId,expandId,closeId) {
detectId = $(detectId); // So we can detect if the panel is currently visible or not before we run the condition
openId = $(openId); // The element we click on to open a panel, in this case the plus icon
expandId = $(expandId); // The panel we want to expand
closeId = $(closeId); // The element we click on to close a panel, in this case the plus icon
if(detectId.visible(detectId))
Effect.Fade(closeId,{duration:fadeDur,afterFinish: function(){Effect.SlideUp(expandId,{duration:SlideDur, afterFinish: function(){Effect.Appear(openId, {duration:AppearDur})}})}});
else
new Effect.Fade(openId,{duration:fadeDur,afterFinish: function(){new Effect.SlideDown(expandId,{duration:SlideDur, afterFinish: function(){new Effect.Appear(closeId, {duration:AppearDur})}})}});
}
Effect.Chain and Effect.Delayed Chain
Simply fires the Effect for multiple elements sequently (or delayed in the case of Delayed Chain).
Sample usage:
new Effect.Chain(
'Fade', // The effect name
$$('div.test'), // an array of elements
{ duration: 0.5 } // options for the effect itself
);
new Effect.DelayedChain('Appear', $$('ul#test li'), { duration: 0.5 }, 100);
// Fires an Effect.Appear for each element with a delay of 100 m
Show source
Effect.DelayedChain = Class.create();
Object.extend(Effect.DelayedChain.prototype, {
initialize: function(effect, elements, options, timeout){
this.elements = elements;
this.effect = effect;
this.timeout = timeout || 100;
this.options = Object.extend({}, options || {});
this.afterFinish = this.options.afterFinish || Prototype.emptyFunction;
this.options.afterFinish = Prototype.emptyFunction;
setTimeout(this.action.bind(this),1);
},
action: function() {
if(this.elements.length){
new Effect[this.effect](this.elements.shift(), this.options);
setTimeout(this.action.bind(this), this.timeout);
} else {
if(this.afterFinish) this.afterFinish();
}
}
});
Effect.Chain = Class.create();
Object.extend(Effect.Chain.prototype, {
initialize: function(effect, elements, options){
this.elements = elements || [];
this.effect = effect;
this.options = options || {};
this.afterFinish = this.options.afterFinish || Prototype.emptyFunction;
this.options.afterFinish = this.nextEffect.bind(this);
setTimeout(this.nextEffect.bind(this), 1);
},
nextEffect: function(){
if(this.elements.length)
new Effect[this.effect](this.elements.shift(), this.options);
else
this.afterFinish();
}
});
A better diaporama
This is an alternative implementation to the diaporama showed in the Visual effects page.
The parameters are:
- element where the images will be shown,
- the base url for the images, the number of images to show,
- the effect to use,
- the duration of the effect and
- the duration of each slide.
and images must have a number at the end, “image1.jpg” p.e.
Example:
diaporama(container, '../img/promo', 4, 'appear', 2, 4)
By Ismael Jurado
Show source
function diaporama(e, base, counter, eff, dur, inter) {
var count = counter + 1
do {
count --
var img = document.createElement("img")
img.src = base + count + ".jpg"
img.id = 'img' + count
img.style.display = (count == 1)? 'inline' : 'none'
e.appendChild(img)
} while (count != 1)
e.style.backgroundImage = 'none'
setInterval(function() {
Effect.toggle('img' + ((count 0)? 1 : count), eff, { duration: dur })
Effect.toggle('img' + ((count 0)? counter : count + 1), eff, { duration: dur })
count = (count == (counter – 1))? 0 : count +1
},inter * 1000);
}
A another diaporama
I couldn’t get the above diaporama to work and I didn’t like the way the original worked (disappearing not fading out).
So here’s another alternative (a mixture of the previous two), it’s more static than Ismael Jurado’s version but fades in and out nicely unlike the original.
I’m new to javascript so if i’ve made a huge error somewhere could someone please point it out.
Trish McDonough
p.s. what is a diaporama anyway?
Show source
<html>
<head>
<style type="text/css">
#container
{
position:relative;
}
.rotateimg
{
position:absolute;
top:0px;
left:0px;
width:800px;
height:163px;
}
</style>
<script src="js/prototype.js" type="text/javascript"></script>
<script src="js/scriptaculous.js?load=effects" type="text/javascript"></script>
<script type="text/javascript">
function majDiaporama()
{
var counter = 5;
var eff = 'appear';
var dur = 3;
var inter = 6;
//show first (last numerically) picture as page loads
Effect.toggle('rotate' + counter, eff, { duration: 0 });
var count = counter - 1;
var tohide = counter;
setInterval(function()
{
//fades out last picture - fades in next
Effect.toggle('rotate' + tohide, eff, { duration: dur });
Effect.toggle('rotate' + count, eff, { duration: dur });
tohide = count;
count = (count == 1)? counter : count - 1;
},inter * 1000);
}
</script>
</head>
<body onLoad="majDiaporama()">
<div id="container">
<noscript>
<img src="rotate/header5.jpg" class="rotateimg" alt="random selection of pictures" id="rotate4"/>
</noscript>
<img src="rotate/header5.jpg" class="rotateimg" style="display:none;" alt="random selection of pictures" id="rotate5"/>
<img src="rotate/header4.jpg" class="rotateimg" style="display:none;" alt="random selection of pictures" id="rotate4"/>
<img src="rotate/header3.jpg" class="rotateimg" style="display:none;" alt="random selection of pictures" id="rotate3"/>
<img src="rotate/header2.jpg" class="rotateimg" style="display:none;" alt="random selection of pictures" id="rotate2"/>
<img src="rotate/header1.jpg" class="rotateimg" style="display:none;" alt="random selection of pictures" id="rotate1"/>
</div>
</body>
</html>
Horizontal Blind Down/Up Effect
by Fabio Gruppioni (Ytanium)
Simple modification of the Effect.Blind Down/Up. Effect.toggle support included :-).
Show source
Effect.BlindUpFromRight = function(element) {
element = $(element);
element.makeClipping();
return new Effect.Scale(element, 0,
Object.extend({ scaleContent: false,
scaleY: false,
restoreAfterFinish: true,
afterFinishInternal: function(effect) {
effect.element.hide();
effect.element.undoClipping();
}
}, arguments[1] || {})
);
}
Effect.BlindDownFromRight = function(element) {
element = $(element);
var elementDimensions = element.getDimensions();
return new Effect.Scale(element, 100, Object.extend({
scaleContent: false,
scaleY: false,
scaleFrom: 0,
scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
restoreAfterFinish: true,
afterSetup: function(effect) {
effect.element.makeClipping();
effect.element.setStyle({width: '0px'});
effect.element.show();
},
afterFinishInternal: function(effect) {
effect.element.undoClipping();
}
}, arguments[1] || {}));
}
//Modification to apply @Effect->PAIRS, add the follow element, to enable Effect.toggle support:
'blindr': ['BlindDownFromRight','BlindUpFromRight']
Effect.Switch Color – switch between 2 text colors
by Fabio Gruppioni (Ytanium)
A very simple effect to colorize text. Default text colors are:
#000000 and #0000ff
Sample usage:
new Effect.SwitchColor('elid'); // switch between #000000 and #0000FF
new Effect.SwitchColor('elid', {endcolor:'#ff0000'}); // switch between #000000 and #FF0000
new Effect.SwitchColor('elid', {endcolor'#ff0000', startcolor:'#002244'}); // switch between #002244 and #FF0000
Show source
Effect.SwitchColor = function(element) {
element = $(element);
var options = Object.extend({
startcolor: '#000000',
endcolor: '#0000ff'
}, arguments[1] || {});
var defcol = options.endcolor;
if (element.style.color.parseColor() == defcol)
defcol = options.startcolor;
element.style.color = defcol;
}
Ultra simple DOM rollovers.
This is a very simple example showing a rollover effect, I needed some clean rollovers and this works nicely so I figured I’d roll it into the chest. An example can be seen here, the css and js are inline.
// Created by Mike Ellan: Mike @ F I G I T D O T C O M
Show source
function mOver(el,img){
$(el).setAttribute("src","images/navigation/"+img+".gif");
}
function mOut(el,img){
$(el).setAttribute("src","images/navigation/"+img+".gif");
}
Fixed position to a droppable box
This is a patch to make possible to make fixed droppable elements. (Example)
1) At the line 1223 on prototype.js, add this code
else if (pos == 'fixed') {
element._madePositioned = true;
element.style.position = 'fixed';
}
2) Attribute a fixed position to the droppable element and, the droppable field will be fixed to !
Effect.DropDown (by Gilles St. Cyr)
This patch allows you to take an element of fixed height, and expand it to it’s full height, or viseversa, contract it to a specified Height (instead of to 0). It’s possible that you can do this using Effect.Blind Up and Blind Down, but I couldn’t get it working. If you don’t pass the ‘endSize’ option (value in pixels), the element will try to expand to it’s natural height. Hint: You’ll probably want to use this with “overflow:hidden”
Example:
new Effect.DropDown('id');
new Effect.DropDown('id',{endSize:20});
Show source
Effect.DropDown = Class.create();
Object.extend(Object.extend(Effect.DropDown.prototype, Effect.Base.prototype), {
initialize: function(element) {
this.element = $(element);
var options = arguments[1] || {};
this.start(options);
},
setup: function(element){
this.beginH = this.element.offsetHeight;
this.endH;
if(this.options.endSize) this.endH = this.options.endSize;
else {
this.element.style.visibility = 'hidden';
this.element.style.height = '';
this.endH = this.element.offsetHeight;
this.element.style.visibility = 'visible';
this.element.style.height = this.beginH+'px';
if(this.endH < this.beginH && !this.options.endSize){ this.cancel(); }
}
},
update: function(position){
this.element.style.height = Math.round(this.beginH+position*(this.endH-this.beginH))+'px';
},
finish: function(position) {
this.element.style.height = this.endH+'px';
}
});
Effect.HighlightText (by m3nt0r)
I made slight modifications to the core Effect.Hightlight. With this one you can highlight text instead of a background. Kinda nice and a bit more unobstrusive.
Example:
new Effect.HighlightText('id');
Show source
Effect.HighlightText = Class.create();
Object.extend(Object.extend(Effect.HighlightText.prototype, Effect.Base.prototype), {
initialize: function(element) {
this.element = $(element);
if(!this.element) throw(Effect._elementDoesNotExistError);
var options = Object.extend({ startcolor: '#ffff99' }, arguments[1] || {});
this.start(options);
},
setup: function() {
if(this.element.getStyle('display')=='none') { this.cancel(); return; }
if(!this.options.endcolor)
this.options.endcolor = this.element.getStyle('color').parseColor('#000000');
if(!this.options.restorecolor)
this.options.restorecolor = this.element.getStyle('color');
// init color calculations
this._base = $R(0,2).map(function(i){ return parseInt(this.options.startcolor.slice(i*2+1,i*2+3),16) }.bind(this));
this._delta = $R(0,2).map(function(i){ return parseInt(this.options.endcolor.slice(i*2+1,i*2+3),16)-this._base[i] }.bind(this));
},
update: function(position) {
this.element.setStyle({color: $R(0,2).inject('#',function(m,v,i){
return m+(Math.round(this._base[i]+(this._delta[i]*position)).toColorPart()); }.bind(this)) });
},
finish: function() {
this.element.setStyle({color: this.options.restorecolor});
}
});
Effect.Unfold (by Jonny)
This effect unfolds an object. It works just like the fold effect, but backwards. The effects aren’t able to be toggled, but I hope to have that working soon.
Example:
new Effect.Unfold('id');
Show source
Effect.Unfold = function(element) {
element = $(element);
var elementDimensions = element.getDimensions();
var oldStyle = {
top: element.style.top,
left: element.style.left,
width: element.style.width,
height: element.style.height };
element.makeClipping();
return new Effect.Scale(element, 100, Object.extend({
scaleContent: false,<br />
scaleY: false,
scaleFrom: 1,
scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
afterSetup: function(effect) {
effect.element.makeClipping().setStyle({width: '1px', height: '1px'}).show();
},
afterFinishInternal: function(effect) {
new Effect.Scale(element, 100, {
scaleContent: false,
scaleX: false,
scaleFrom: 5,
scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
restoreAfterFinish: true,
afterSetup: function(effect) {
effect.element.makeClipping().setStyle({height: '1px'}).show();
},
afterFinishInternal: function(effect) {
effect.element.undoClipping().setStyle(oldStyle);
}
});
}}, arguments[1] || {}));
};
Effect.Gravity (by Jeff Conrad)
This effect causes an object to move, and grow simultaneously, for creating an apparent z-dimension to your draggable objects.
Example:
var deltaX = 400;
var deltaY = 100;
var boxWidth = 100;
var boxHeight = 100;
var magnification = 3;
Effect.Gravity.up('testDiv1', deltaX, deltaY, boxWidth, boxHeight, magnification);
Show source
Effect.Transitions.exponential = function(pos) {
return 1-Math.pow(1-pos,2);
}
Effect.Transitions.exponentialReverse = function(pos) {
return Math.pow(pos,2);
}
Effect.Gravity = {
up:function(divId, distX, distY, boxWidth, boxHeight, magnification) {
var parallel = new Effect.Parallel(
[
new Effect.Move(divId, { x: distX/2 - boxWidth*(magnification-1)/2, y: distY/2 - boxHeight*(magnification-1)/2, mode: 'relative', transition: Effect.Transitions.exponential})
,
new Effect.Scale(divId, magnification * 100.0, { transition: Effect.Transitions.exponential })
],
Object.extend({
divId: divId,
distX: distX,
distY: distY,
boxWidth: boxWidth,
boxHeight: boxHeight,
magnification: magnification,
afterFinishInternal: function(effect) {
Effect.Gravity.down(this.divId, this.distX, this.distY, this.boxWidth, this.boxHeight, this.magnification);
},
delay: 0.0,
duration: 0.5
})
)
},
down:function(divId, distX, distY, boxWidth, boxHeight, magnification) {
var parallel = new Effect.Parallel([
new Effect.Move(divId, { x: distX/2 + boxWidth*(magnification-1)/2, y: distY/2 + boxHeight*(magnification-1)/2, mode: 'relative', transition: Effect.Transitions.exponentialReverse})
,
new Effect.Scale(divId, 100.0 / magnification, {transition: Effect.Transitions.exponentialReverse})
],
Object.extend({
delay: 0.0,
duration: 0.5
})
)
}
};
Effect.Curve (by Baglan)
Effect.Move moving object on a curve instead of straight line.
I’ve written a tiny article about it where you can download the working code and see an example using Bezier curve (included). In case you want to use some other kind of curve it can be implemented as an object with x() and y() functions, accepting a position argument between 0 and 1 (just look at the Bezier Curve code) and passed to the Effect.Curve as a ‘curve’ option.
Example
new Effect.Curve('box',{curve: new BezierCurve(
185, 85, // A
25, 355, // B
560, 330, // C
440, 210 // D
)});
Show source
Effect.Curve = Class.create(Effect.Base, {
initialize: function(element) {
this.element = $(element);
if (!this.element) throw(Effect._elementDoesNotExistError);
var options = Object.extend({
curve: new BezierCurve(0,0,0,0,0,0,0,0)
}, arguments[1] || { });
this.start(options);
},
setup: function() {
this.element.makePositioned();
},
update: function(position) {
this.element.setStyle({
left: this.options.curve.x(position).round() + 'px',
top: this.options.curve.y(position).round() + 'px'
});
}
})