|
6 | 6 |
|
7 | 7 | function tcSticky(CONSTANTS, $window) {
|
8 | 8 | return {
|
9 |
| - restrict: 'A', |
10 |
| - link: function(scope, element) { |
11 |
| - scope.width = element.prop('offsetWidth'); |
12 |
| - var elWidth = scope.width + 'px', |
13 |
| - elChild = angular.element(element[0].querySelector(':first-child')); |
14 |
| - // elChild.css('width', elWidth); |
15 |
| - var hitBottom = false; |
16 |
| - angular.element($window).bind("scroll", function() { |
17 |
| - var affixElement = document.getElementById('affix'), |
18 |
| - xPosition = 0, |
19 |
| - yPosition = 0; |
20 |
| - function getPosition(element) { |
21 |
| - while(element) { |
22 |
| - yPosition += (element.offsetTop - element.scrollTop + element.clientTop); |
23 |
| - element = element.offsetParent; |
24 |
| - } |
25 |
| - } |
26 |
| - getPosition(affixElement); |
27 |
| - var height = angular.element(affixElement).prop('offsetHeight'); |
28 |
| - var top = angular.element(affixElement).prop('offsetTop'); |
29 |
| - // console.log(yPosition); |
30 |
| - // console.log(angular.element(document).scrollTop); |
31 |
| - // console.log(elChild[0].clientTop); |
32 |
| - if (yPosition >= 0 || $window.innerHeight <= elChild[0].offsetHeight || $window.innerWidth < 768) { |
33 |
| - elChild.removeClass('affix affix-bottom'); |
34 |
| - } else if ( yPosition < 0) { |
35 |
| - var bodyRect = document.body.getBoundingClientRect() |
36 |
| - var elChildRect = elChild[0].getBoundingClientRect() |
37 |
| - var elChildAbsOffset = elChildRect.top - bodyRect.top; |
38 |
| - |
39 |
| - var affixElementRect = affixElement.getBoundingClientRect(); |
40 |
| - var affixElementAbsOffset = affixElementRect.top - bodyRect.top; |
41 |
| - |
42 |
| - var elChildOffset = elChildAbsOffset - affixElementAbsOffset; |
43 |
| - var elChildHeight = elChild[0].clientHeight; |
44 |
| - var affixElementHeight = affixElement.clientHeight; |
45 |
| - |
46 |
| - console.log(elChildRect.top); |
47 |
| - |
48 |
| - var pastBottom = elChildOffset + elChildHeight > affixElementHeight; |
49 |
| - var topCovered = elChildRect.top > 10; |
50 |
| - |
51 |
| - console.log('topcovered', topCovered); |
52 |
| - console.log('pastBottom', pastBottom); |
53 |
| - |
54 |
| - elChild.addClass('affix'); |
55 |
| - |
56 |
| - if (pastBottom && !hitBottom) { |
57 |
| - hitBottom = true; |
58 |
| - elChild.addClass('affix-bottom'); |
59 |
| - elChild.removeClass('affix'); |
60 |
| - } |
61 |
| - |
62 |
| - if (topCovered) { |
63 |
| - hitBottom = false; |
64 |
| - elChild.removeClass('affix-bottom'); |
65 |
| - elChild.addClass('affix'); |
66 |
| - } |
67 |
| - } |
68 |
| - }); |
69 |
| - } |
| 9 | + restrict: 'A', |
| 10 | + link: function(scope, element) { |
| 11 | + scope.width = element.prop('offsetWidth'); |
| 12 | + |
| 13 | + var elWidth = scope.width + 'px'; |
| 14 | + var elChild = angular.element(element[0].querySelector(':first-child')); |
| 15 | + // elChild.css('width', elWidth); |
| 16 | + |
| 17 | + var hitBottom = false; |
| 18 | + |
| 19 | + angular.element($window).bind("scroll", function() { |
| 20 | + var affixElement = document.getElementById('affix'); |
| 21 | + var xPosition = 0; |
| 22 | + var yPosition = 0; |
| 23 | + |
| 24 | + function getPosition(element) { |
| 25 | + while(element) { |
| 26 | + yPosition += (element.offsetTop - element.scrollTop + element.clientTop); |
| 27 | + element = element.offsetParent; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + getPosition(affixElement); |
| 32 | + |
| 33 | + var height = angular.element(affixElement).prop('offsetHeight'); |
| 34 | + var top = angular.element(affixElement).prop('offsetTop'); |
| 35 | + |
| 36 | + if (yPosition >= 0 || $window.innerHeight <= elChild[0].offsetHeight || $window.innerWidth < 768) { |
| 37 | + elChild.removeClass('affix affix-bottom'); |
| 38 | + } else if ( yPosition < 0) { |
| 39 | + var bodyRect = document.body.getBoundingClientRect() |
| 40 | + var elChildRect = elChild[0].getBoundingClientRect() |
| 41 | + var elChildAbsOffset = elChildRect.top - bodyRect.top; |
| 42 | + |
| 43 | + var affixElementRect = affixElement.getBoundingClientRect(); |
| 44 | + var affixElementAbsOffset = affixElementRect.top - bodyRect.top; |
| 45 | + |
| 46 | + var elChildOffset = elChildAbsOffset - affixElementAbsOffset; |
| 47 | + var elChildHeight = elChild[0].clientHeight; |
| 48 | + var affixElementHeight = affixElement.clientHeight; |
| 49 | + |
| 50 | + var pastBottom = elChildOffset + elChildHeight > affixElementHeight; |
| 51 | + var topCovered = elChildRect.top > 10; |
| 52 | + |
| 53 | + elChild.addClass('affix'); |
| 54 | + |
| 55 | + if (pastBottom && !hitBottom) { |
| 56 | + hitBottom = true; |
| 57 | + elChild.addClass('affix-bottom'); |
| 58 | + elChild.removeClass('affix'); |
| 59 | + } |
| 60 | + |
| 61 | + if (topCovered) { |
| 62 | + hitBottom = false; |
| 63 | + elChild.removeClass('affix-bottom'); |
| 64 | + elChild.addClass('affix'); |
| 65 | + } |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
70 | 69 | };
|
71 | 70 | }
|
72 | 71 | })();
|
0 commit comments