Skip to content

Instantly share code, notes, and snippets.

@pinzolo
Last active April 3, 2016 06:08
Show Gist options
  • Save pinzolo/571a2c8899bc20cff25a to your computer and use it in GitHub Desktop.
Save pinzolo/571a2c8899bc20cff25a to your computer and use it in GitHub Desktop.
特定の距離をスクロールされるとトップで要素を固定する
(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%');
}
});
}
}
});
}());
.fixed-on-top {
position: fixed;
top: 98px;
}
<div>
<table id="table-header" fixed-on-top="table-body">
<!-- header cells -->
</table>
<table id="table-body">
<!-- data cells -->
</table>
</div>
$(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');
}
});
});
@MyAllureLove
Copy link

こんにちは

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment