1 define(['app/yangui/yangui.module'], function(yangui) {
\r
3 yangui.register.directive('sticky', ['$timeout', function($timeout){
\r
9 link: function($scope, $elem, $attrs){
\r
10 $timeout(function(){
\r
11 var offsetTop = $scope.offset || 0,
\r
12 $window = angular.element(window),
\r
13 doc = document.documentElement,
\r
14 initialPositionStyle = $elem.css('position'),
\r
17 randomNum = Math.floor((Math.random() * 1000) + 1),
\r
18 $wrapper = $('<div class="sticky-element'+randomNum+' mt20"></div>');
\r
21 // Set the top offset
\r
23 $elem.css('top', offsetTop+'px');
\r
26 // Get the sticky line
\r
28 function setInitial(){
\r
29 $elem.addClass('not-sticky');
\r
30 $elem.wrap($wrapper);
\r
35 function getStickyLine(){
\r
36 //stickyLine = $elem[0].offsetTop - offsetTop;
\r
37 stickyLine = $($elem).offset().top - offsetTop;
\r
40 // Check if the window has passed the sticky line
\r
42 function checkSticky(){
\r
43 scrollTop = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
\r
44 // $('.sticky-element'+randomNum).css('height', parseInt($elem.css('height')) === 0 ? 'auto' : $elem.css('height'));
\r
46 if ( scrollTop >= stickyLine ){
\r
47 $elem.css('position', 'fixed').removeClass('not-sticky').addClass('is-sticky');
\r
48 $('.sticky-element'+randomNum).css('height', parseInt($elem.css('height')) === 0 ? 'auto' : $elem.css('height'));
\r
51 $elem.css('position', initialPositionStyle).removeClass('is-sticky').addClass('not-sticky');
\r
52 $('.sticky-element'+randomNum).css('height','auto');
\r
55 var _ = $elem.hasClass('not-sticky') ? getStickyLine() : null;
\r
60 // Handle the resize event
\r
63 $elem.css('position', initialPositionStyle);
\r
64 $timeout(setInitial);
\r
68 // Attach our listeners
\r
70 $window.on('scroll', checkSticky);
\r
71 $window.on('resize', resize);
\r