Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/wshaver/fixed-sticky into…
Browse files Browse the repository at this point in the history
… wshaver-master

Conflicts:
	fixedsticky.js
  • Loading branch information
zachleat committed Dec 18, 2014
2 parents ec77271 + 15c7d53 commit dbb66b2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "filament-sticky",
"version": "0.1.3",
"version": "0.1.4",
"main": ["fixedsticky.js", "fixedsticky.css"],
"ignore": [
"**/.*"
Expand Down
36 changes: 26 additions & 10 deletions fixedsticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
return parseInt( unit, 10 ) || 0;
}

var uniqueIdCounter = 0;

var S = {
classes: {
plugin: 'fixedsticky',
Expand All @@ -28,7 +30,8 @@
},
keys: {
offset: 'fixedStickyOffset',
position: 'fixedStickyPosition'
position: 'fixedStickyPosition',
id: 'fixedStickyId'
},
tests: {
sticky: featureTest( 'position', 'sticky' ),
Expand All @@ -48,6 +51,16 @@
!S.tests.fixed ||
win.FixedFixed && !$( win.document.documentElement ).hasClass( 'fixed-supported' );
},
refresh: function( el ) {
var $el = $( el );
return $el.each(function() {
$( this )
.removeData( [ S.keys.offset, S.keys.position ] )
.removeClass( S.classes.active + ' ' + S.classes.inactive )
.next( '.' + S.classes.clone ).remove();
S.update( this );
});
},
update: function( el ) {
if( !el.offsetWidth ) { return; }

Expand Down Expand Up @@ -130,11 +143,13 @@
return;
}

$( win ).unbind( '.fixedsticky' );

return $el.each(function() {
$( this )
.removeData( [ S.keys.offset, S.keys.position ] )
var $this = $( this );
var id = $this.data( S.keys.id );
$( win ).unbind( '.fixedsticky' + id );

$this
.removeData( [ S.keys.offset, S.keys.position, S.keys.id ] )
.removeClass( S.classes.active )
.removeClass( S.classes.inactive )
.next( '.' + S.classes.clone ).remove();
Expand All @@ -149,13 +164,14 @@

return $el.each(function() {
var _this = this;
$( win ).bind( 'scroll.fixedsticky', function() {
S.update( _this );
});
var id = uniqueIdCounter++;
$( this ).data( S.keys.id, id );

S.update( this );
$( win ).bind( 'scroll.fixedsticky' + id, function() {
S.update( _this );
}).trigger( 'scroll.fixedsticky' + id );

$( win ).bind( 'resize.fixedsticky', function() {
$( win ).bind( 'resize.fixedsticky' + id , function() {
if( $el.is( '.' + S.classes.active ) ) {
S.update( _this );
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fixed-sticky",
"version": "0.1.3",
"version": "0.1.4",
"description": "A position: sticky polyfill that works with filamentgroup/fixed-fixed for a safer position:fixed fallback.",
"main": "fixedsticky.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions test/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"browser": true,
"predef": [
"jQuery",
"console",
"QUnit",
"module",
"test",
Expand Down
53 changes: 52 additions & 1 deletion test/fixed-sticky-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
equal( $sticky.css( 'position' ), 'static' );
});


test( 'Cleanup', function() {
$( '#qunit-fixture' ).html( '<div id="sticky" class="fixedsticky">Sticky</div>' );

Expand All @@ -111,4 +112,54 @@
ok( !$sticky.siblings( '.' + FixedSticky.classes.clone ).length );
});

}( jQuery ));
test( 'Destroying one fixedsticky should not cleanup others', function() {
$( '#qunit-fixture' ).html(
['<style>#sticky { top: 0; } #sticky2 { top: 0; }</style>',
'<div id="sticky" class="fixedsticky">Sticky</div>',
'<div id="sticky2" class="fixedsticky">Sticky</div>',
'<div style="height: 2000px">Test</div>'].join( '' ) );

var $sticky = $( '#sticky' );
$sticky.fixedsticky();

var $sticky2 = $( '#sticky2' );
$sticky2.fixedsticky();

$sticky.fixedsticky( 'destroy' );

ok( $sticky2.hasClass( 'fixedsticky' ) );
$(window).scrollTop( 1000 ).trigger( 'scroll' );
equal( $sticky2.css( 'position' ), 'fixed' );
equal( $sticky2.offset().top, 1000 );
});

test( 'Refreshing a fixedsticky should update offset', function() {
$( '#qunit-fixture' ).html(
['<style>#sticky { top: 0; }</style>',
'<div id="sticky" class="fixedsticky">Sticky</div>',
'<div style="height: 2000px">Test</div>'].join( '' ) );

var $sticky = $( '#sticky' );
$sticky.fixedsticky();

ok( $sticky.hasClass( 'fixedsticky' ) );
$(window).scrollTop( 1000 ).trigger( 'scroll' );
equal( $sticky.css( 'position' ), 'fixed' );
equal( $sticky.offset().top, 1000 );

$sticky.before( '<div style="height: 1500px">another div</div>' );

$(window).scrollTop( 1000 ).trigger( 'scroll' );
equal( $sticky.css( 'position' ), 'fixed' );
equal( $sticky.offset().top, 1000 );

$sticky.fixedsticky( 'refresh' );

equal( $sticky.css( 'position' ), 'static' );

$(window).scrollTop( 2000 ).trigger( 'scroll' );
equal( $sticky.css( 'position' ), 'fixed' );
equal( $sticky.offset().top, 2000 );
});

}( jQuery ));

0 comments on commit dbb66b2

Please sign in to comment.