Skip to content

Instantly share code, notes, and snippets.

@Kalyse
Created September 12, 2013 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kalyse/6534936 to your computer and use it in GitHub Desktop.
Save Kalyse/6534936 to your computer and use it in GitHub Desktop.
Sticky.js Implementation in Dojo
define( [
"dojo/_base/declare",
"dojo/_base/connect",
"dojo/_base/lang",
"dojo/_base/fx",
"dojo/fx/easing",
"dojo/dom-style",
"dojo/dom-construct",
"dojo/dom-class",
"dojo/dom",
"dijit/_Widget",
"dojo/window",
"dojo/_base/window"
]
, function(declare, connect, lang, fx, easing, domStyle, domConstruct, domClass, dom, _Widget, win, winCore ) {
/* new Sticky({
targetNode: dom.byId("simpleContactFormContainer"),
constrainNode: dom.byId("zone-content"),
parentNode: dom.byId("region-right"),
placeInConstrainNode: false,
zIndex: 80,
delayDropIn: 0,
animationDuration: 500
});
*/
return declare( "my.layout.Sticky", [ _Widget ], {
targetNode : null,
constrainNode : null,
parentNode : null,
easing: "backOut",
placeInConstrainNode : true,
relativeYAdjust : 0,
placePos : "last",
zIndex : false,
delayDropIn : 0,
constrainYAdjustment : 0,
toggleClass : false,
isFixed : false,
animationDuration : 400,
constructor : function(args){
lang.mixin( this, args);
this.inherited(arguments);
},
postCreate: function(){
connect.connect( winCore.global , "onscroll", this, "_onScroll");
if( !dom.byId(this.targetNode)){
}
if(this.parentNode == null){
this.parentNode = this.targetNode.parentNode;
}
this.parentNodeWidth = domStyle.get(this.parentNode, "width");
this._targetNodeOriginalTop = this._getAbsoluteTop( this.targetNode);
this._constrainNodeOriginalTop = this._getAbsoluteTop( this.constrainNode);
this._originalZIndex = domStyle.get( this.targetNode, "zIndex");
this.easing = easing[this.easing];
this.inherited(arguments);
},
_getAbsoluteTop : function( node ){
var curleft = 0;
var curtop = 0;
do {
curleft += node.offsetLeft;
curtop += node.offsetTop;
} while (node = node.offsetParent);
return curtop;
},
_onScroll : function(scroll){
var w = win.getBox();
// Check to make sure it's not already fixed...
if( !this.isFixed && ( this._targetNodeOriginalTop - w.t ) + this.constrainYAdjustment < this._constrainNodeOriginalTop ){
this.isFixed = true;
// We might want to have the delayDropIn
if(this.delayDropIn > 0 ){
var anim = fx.animateProperty({
node: this.targetNode,
easing: this.easing,
properties: {
top : {
start : 0,
end: parseInt( this._constrainNodeOriginalTop) + parseInt( this.relativeYAdjust )
}
/* backgroundColor: {
start: "#FFFDDF",
end: "#FFF"
} */
},
onBegin: lang.hitch( this, function(){
domStyle.set( this.targetNode, "position", "fixed");
if( this.zIndex ) {
domStyle.set( this.targetNode, "zIndex", this.zIndex );
}
if( this.toggleClass ) {
domClass.toggle( this.targetNode, this.toggleClass, true );
}
domStyle.set( this.targetNode, "width", this.parentNodeWidth + "px" );
}),
duration: this.animationDuration
});
setTimeout( function(){
anim.play();
}, this.delayDropIn );
} else {
domStyle.set( this.targetNode, "position", "fixed");
domStyle.set( this.targetNode, "top", parseInt( this._constrainNodeOriginalTop) + parseInt( this.relativeYAdjust ) + "px");
if(this.placeInConstainNode) {
domConstruct.place( this.targetNode, this.constrainNode);
}
if( this.zIndex ) {
domStyle.set( this.targetNode, "zIndex", this.zIndex );
}
if( this.toggleClass ) {
domClass.toggle( this.targetNode, this.toggleClass, true );
}
}
} else if( this.isFixed == true && !(( this._targetNodeOriginalTop - w.t ) < this._constrainNodeOriginalTop) ) {
this.isFixed = false;
domConstruct.place( this.targetNode, this.parentNode, this.placePos);
domStyle.set( this.targetNode, "position", "");
domStyle.set( this.targetNode, "top", "");
if( this.zIndex ) {
domStyle.set( this.targetNode, "zIndex", this._originalZIndex );
}
if( this.toggleClass ) {
domClass.toggle( this.targetNode, this.toggleClass, false );
}
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment