angular
.module('app', [])
.controller('MainCtrl', MainCtrl);
function MainCtrl($scope, $http, $window, $document, $timeout) {
var ctop = 0;
var sectionArr = [];
var initEvents = function() {
//delaying for dom height calculation
$timeout(function() {
angular.forEach($scope.blocks, function(data, index) {
var s = angular.element($document[0].getElementById('block-' + index))[0];
sectionArr[index] = {
top: s.offsetTop,
height: (s.offsetHeight + s.offsetTop)
};
});
}, 200);
//attaching event for scrolling
angular.element($window).off('scroll').on('scroll', function(ev) {
var scrollOffsetTop = ev.pageY || ($window.pageYOffset !== undefined) ? $window.pageYOffset : ($document[0].documentElement || $document[0].body.parentNode || $document[0].body).scrollTop;
var gabBetween = 1;
angular.forEach(sectionArr, function(data, id) {
var head = angular.element($document[0].getElementById('header-' + id));
if (scrollOffsetTop > data.top && scrollOffsetTop < data.height) {
if (scrollOffsetTop > (data.height - head[0].clientHeight) && scrollOffsetTop < data.height) {
head.addClass('absolute')
.removeClass('fixed')
.css('top', (data.height - (data.top + head[0].clientHeight) - gabBetween) + 'px');
} else {
head.addClass('fixed')
.removeClass('absolute')
.css('top', 0);
}
} else {
head.removeClass('fixed')
.removeClass('absolute')
.removeAttr('style');
}
});
});
};
//initializing for fetching json
var init = function() {
$scope.blocks = [{
"header": "Header First",
"content": "Header First with content............. First Header"
}, {
"header": "Header Second",
"content": "Header Second with content............. Second Header"
}, {
"header": "Header Third",
"content": "Header Third with content............. Third Header"
}, {
"header": "Header Fourth",
"content": "Header Fourth with content............. Fourth Header"
}, {
"header": "Header Fifth",
"content": "Header Fifth with content............. Fifth Header"
}, {
"header": "Header Sixth",
"content": "Header Sixth with content............. Sixth Header"
}, {
"header": "Header Seventh",
"content": "Header Seventh with content............. Seventh Header"
}];
//events attachment occurs after fetching data
initEvents();
};
//starting
init();
}
body {
background: #efefef;
overflow-x: hidden;
margin: 0;
padding: 0;
font-family: 'Ubuntu', verdana;
}
section {
position: relative;
display: inline-block;
width: 100%;
min-height: 700px;
padding: 0;
margin: 0;
}
section>header {
display: block;
min-height: 75px;
}
header>div {
color: #fff;
z-index: 1;
background: #fe001a;
background: rgba(254, 0, 26, 0.8);
padding: 5px 25px;
}
header>.fixed {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 0;
}
header>.absolute {
position: absolute;
left: 0;
right: 0
}
section>p {
text-align: left;
padding: 0 25px;
line-height: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body data-ng-app="app" data-ng-cloak>
<h2>Sticky Header</h2>
<p>Checkout example of each section making a sticky header</p>
<main data-ng-controller="MainCtrl">
<section data-ng-repeat="data in blocks" id="block-{{$index}}">
<header>
<div id="header-{{$index}}">
<h3>{{data.header}}<span style="float:right">#{{$index+1}}</span></h3>
</div>
</header>
<p data-ng-bind="data.content"></p>
</section>
</main>
<script src="script.js"></script>
</body>
最簡單的辦法是使用'位置:fixed'和修復搜索欄要在其中所以即使當用戶滾動將保持在相同的位置的位置。或者你想讓它動畫? – Manish
我測試過'css position:fixed'.but但我希望我的div像上面的jQuery example.yes一樣平滑地移動。 – Tje123
您可以將相同的jQuery代碼放入您的控制器中。並在搜索框中添加「跟隨滾動」類。 –