Last active
April 3, 2016 06:08
-
-
Save pinzolo/571a2c8899bc20cff25a to your computer and use it in GitHub Desktop.
特定の距離をスクロールされるとトップで要素を固定する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
angular.module('app').directive('fixedOnTop', function($window) { | |
return { | |
restrict: 'A', | |
link: function($scope, element, attrs) { | |
angular.element($window).on('scroll', function() { | |
var baseTop = element.get(0).offsetTop; | |
if (angular.element(this).scrollTop() >= baseTop) { | |
element.addClass('fixed-on-top'); | |
element.width(angular.element('#' + attrs.fixedOnTop).width()); | |
} else { | |
element.removeClass('fixed-on-top'); | |
element.width('100%'); | |
} | |
}); | |
} | |
} | |
}); | |
}()); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.fixed-on-top { | |
position: fixed; | |
top: 98px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<table id="table-header" fixed-on-top="table-body"> | |
<!-- header cells --> | |
</table> | |
<table id="table-body"> | |
<!-- data cells --> | |
</table> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
var baseTop = $('#table-header').get(0).offsetTop; | |
$(window).on('scroll', function() { | |
if ($(this).scrollTop() > baseTop) { | |
$('.header').addClass('fixed-on-top'); | |
} else { | |
$('.header').removeClass('fixed-on-top'); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
こんにちは