Skip to content

Commit

Permalink
strickies recalculate and rebind on window resize
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaswise committed Jun 19, 2015
1 parent fa8e97d commit 6757efe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Ie9 Shim for `<input>` line height
- Ie9 Shim for `.tooltip` line height
- Prefixerize transform property in tooltips
- Resizing window recalculates and rebind stickies


## 0.9.2
Expand Down
39 changes: 26 additions & 13 deletions lib/js/calcite-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// └────────────┘
// define all public api methods (excluding patterns)
var calcite = {
version: 'v0.10.0',
version: 'v0.10.1',
click: click,
addEvent: addEvent,
removeEvent: removeEvent,
Expand Down Expand Up @@ -538,16 +538,22 @@
// sticks things to the window
calcite.sticky = function () {
var elements = findElements('.js-sticky');
var stickies = elements.map(function (el) {
var offset = el.offsetTop;
var dataTop = el.getAttribute('data-top') || 0;
return {
active: false,
top: offset - parseInt(dataTop, 0),
shim: el.cloneNode('deep'),
element: el
};
});

function calculateStickyPositions () {
var stickies = elements.map(function (el) {
var offset = el.offsetTop;
var dataTop = el.getAttribute('data-top') || 0;
return {
active: false,
top: offset - parseInt(dataTop, 0),
shim: el.cloneNode('deep'),
element: el
};
});
return stickies;
}

var stickies = calculateStickyPositions();

function handleScroll(item, offset) {
var el = item.element;
Expand All @@ -569,13 +575,20 @@
}
}

addEvent(window, 'scroll', function () {
function bindStickies (e) {
var offset = window.pageYOffset;
stickies.forEach(function (sticky) {
handleScroll(sticky, offset);
});
});
}

addEvent(window, 'scroll', bindStickies);

window.onresize = function() {
stickies = calculateStickyPositions();
removeEvent(window, 'scroll', bindStickies);
addEvent(window, 'scroll', bindStickies);
};
};

// ┌────────────────────┐
Expand Down

0 comments on commit 6757efe

Please sign in to comment.