Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(subheader/sticky): Stick subheader to content instead of parent.
Browse files Browse the repository at this point in the history
The `$mdSticky` service used to stick it's contents to the parent
of the `<md-content>` container. This resulted in some odd styling
issues and the inability to have two lists with sticky subheaders
side-by-side.

This fix appends the subheaders directly to the `<md-content>` to
fix this issue.

Also, updated subheaders to use `<div>` tags instead of `<h2>`.

fixes #3535. closes #4276.
  • Loading branch information
topherfangio authored and ThomasBurleson committed Aug 24, 2015
1 parent cecba23 commit 8c921a6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 40 deletions.
77 changes: 48 additions & 29 deletions src/components/sticky/sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ angular.module('material.components.sticky', [
* If not provided, it will use the result of `element.clone()`.
*/

function MdSticky($document, $mdConstant, $compile, $$rAF, $mdUtil) {
function MdSticky($document, $mdConstant, $$rAF, $mdUtil) {

var browserStickySupport = checkStickySupport();

Expand Down Expand Up @@ -95,7 +95,9 @@ function MdSticky($document, $mdConstant, $compile, $$rAF, $mdUtil) {
};
self.items.push(item);

contentEl.parent().prepend(item.clone);
$mdUtil.nextTick(function() {
contentEl.prepend(item.clone);
});

debouncedRefreshElements();

Expand Down Expand Up @@ -162,44 +164,61 @@ function MdSticky($document, $mdConstant, $compile, $$rAF, $mdUtil) {
function onScroll() {
var scrollTop = contentEl.prop('scrollTop');
var isScrollingDown = scrollTop > (onScroll.prevScrollTop || 0);

// Store the previous scroll so we know which direction we are scrolling
onScroll.prevScrollTop = scrollTop;

// At the top?
//
// AT TOP (not scrolling)
//
if (scrollTop === 0) {
// If we're at the top, just clear the current item and return
setCurrentItem(null);
return;
}

//
// SCROLLING DOWN (going towards the next item)
//
if (isScrollingDown) {

// Going to next item?
} else if (isScrollingDown && self.next) {
if (self.next.top - scrollTop <= 0) {
// Sticky the next item if we've scrolled past its position.
// If we've scrolled down past the next item's position, sticky it and return
if (self.next && self.next.top <= scrollTop) {
setCurrentItem(self.next);
} else if (self.current) {
// Push the current item up when we're almost at the next item.
if (self.next.top - scrollTop <= self.next.height) {
translate(self.current, self.next.top - self.next.height - scrollTop);
} else {
translate(self.current, null);
}
return;
}

// If the next item is close to the current one, push the current one up out of the way
if (self.current && self.next && self.next.top - scrollTop <= self.next.height) {
translate(self.current, scrollTop + (self.next.top - self.next.height - scrollTop));
return;
}

// Scrolling up with a current sticky item?
} else if (!isScrollingDown && self.current) {
if (scrollTop < self.current.top) {
// Sticky the previous item if we've scrolled up past
// the original position of the currently stickied item.
}

//
// SCROLLING UP (not at the top & not scrolling down; must be scrolling up)
//
if (!isScrollingDown) {

// If we've scrolled up past the previous item's position, sticky it and return
if (self.current && self.prev && scrollTop < self.current.top) {
setCurrentItem(self.prev);
return;
}
// Scrolling up, and just bumping into the item above (just set to current)?
// If we have a next item bumping into the current item, translate
// the current item up from the top as it scrolls into view.
if (self.current && self.next) {
if (scrollTop >= self.next.top - self.current.height) {
translate(self.current, self.next.top - scrollTop - self.current.height);
} else {
translate(self.current, null);
}

// If the next item is close to the current one, pull the current one down into view
if (self.next && scrollTop >= self.next.top - self.current.height) {
translate(self.current, scrollTop + (self.next.top - scrollTop - self.current.height));
return;
}
}

//
// Otherwise, just move the current item to the proper place (scrolling up or down)
//
if (self.current) {
translate(self.current, scrollTop);
}
}

function setCurrentItem(item) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/subheader/subheader.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ function MdSubheaderDirective($mdSticky, $compile, $mdTheming, $mdUtil) {
replace: true,
transclude: true,
template: (
'<h2 class="md-subheader">' +
'<div class="md-subheader">' +
' <div class="md-subheader-inner">' +
' <span class="md-subheader-content"></span>' +
' </div>' +
'</h2>'
'</div>'
),
link: function postLink(scope, element, attr, controllers, transclude) {
$mdTheming(element);
Expand Down
9 changes: 0 additions & 9 deletions src/components/subheader/subheader.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ $subheader-sticky-shadow: 0px 2px 4px 0 rgba(0,0,0,0.16) !default;
margin: 0;
}

.md-subheader:after {
position: absolute;
left: 0;
bottom: 0;
top: 0;
right: -$subheader-margin-right;
content: '';
}

transition: 0.2s ease-out margin;

&.md-sticky-clone {
Expand Down

0 comments on commit 8c921a6

Please sign in to comment.