Javascript 仅ios safari浏览器的滚动固定位置延迟
当在移动设备上查看此网站时,我设置了特定的滚动功能(Javascript 仅ios safari浏览器的滚动固定位置延迟,javascript,jquery,html,ios,css,Javascript,Jquery,Html,Ios,Css,当在移动设备上查看此网站时,我设置了特定的滚动功能( function mobileScrollInit(){ console.log("Mobile initiate"); $("body").animate({ scrollTop: 0 }); var stickyHeaders = (function() { var $stickies; var topOffset = new Array(); var load
function mobileScrollInit(){
console.log("Mobile initiate");
$("body").animate({ scrollTop: 0 });
var stickyHeaders = (function() {
var $stickies;
var topOffset = new Array();
var load = function(stickies, target) {
if (typeof stickies === "object" && stickies instanceof jQuery && stickies.length > 0) {
$stickies = stickies.each(function(index,element) {
var $thisSticky = $(this);
//for each header, get the height and top position
$thisSticky
.data('originalPosition', $thisSticky.offset().top)
.data('originalHeight', $thisSticky.outerHeight());
console.log("Index is: "+index);
console.log("Offset: "+$thisSticky.offset().top);
topOffset.push($thisSticky.data('originalPosition'));
});
//only on scroll, run function
target.off("scroll.stickies").on("scroll.stickies", function(event) {
_whenScrolling(event);
});
}
};
var _whenScrolling = function(event) {
//get scrolling position
var $scrollTop = $(event.currentTarget).scrollTop();
console.log($scrollTop);
var scrolled = 0;
var count = 0;
$stickies.each(function(index,value) {
var $thisSticky = $(this),
//$stickyPosition = $stickies[count].data('originalPosition'),
$stickyHeight = $thisSticky.data('originalHeight'),
$nextStickyHeight = 0,
$newPosition,
$nextSticky;
$nextSticky = $stickies.eq(index + 1);
$nextStickyHeight = $stickies.eq(index + 1).outerHeight();
//if last header
if ($scrollTop >= topOffset[$stickies.length - 1]) {
console.log("Reached the end");
var $lastStickyElement = $stickies.eq($stickies.length - 1);
$lastStickyElement.addClass("header--fixed");
$lastStickyElement.css({
"top": 0
});
$nextSticky.removeClass("header--fixed");
$nextSticky.addClass("header--static");
$('.screen_inner_holder').css({
"padding-top": $stickyHeight
});
}
//if you scroll past the header nav
else if ($scrollTop >= topOffset[count] && $scrollTop < topOffset[count + 1]) {
scrolled = 1;
//push up the next one
var triggerToPushUp = (topOffset[count + 1] - $stickyHeight);
if($scrollTop >= triggerToPushUp && topOffset[count + 1] > $scrollTop) {
$thisSticky.css({
"top": -($scrollTop - triggerToPushUp)
});
$nextSticky.removeClass("header--static");
$nextSticky.addClass("header--fixed");
$nextSticky.css({
"top": topOffset[count + 1] - $scrollTop,
});
$('.screen_inner_holder').css({
"padding-top": $stickyHeight + $nextStickyHeight
});
console.log("Scroll top is greater that: " + triggerToPushUp);
console.log("Pushing sticky up");
return false;
}
else {
$thisSticky.removeClass("header--static");
$thisSticky.addClass("header--fixed");
$thisSticky.css({
"top": 0
});
$nextSticky.removeClass("header--fixed");
$nextSticky.addClass("header--st
function mobileScrollInit(){
console.log("Mobile initiate");
$("body").animate({ scrollTop: 0 });
var stickyHeaders = (function() {
var $stickies;
var topOffset = new Array();
var load = function(stickies, target) {
if (typeof stickies === "object" && stickies instanceof jQuery && stickies.length > 0) {
$stickies = stickies.each(function(index,element) {
var $thisSticky = $(this);
//for each header, get the height and top position
$thisSticky
.data('originalPosition', $thisSticky.offset().top)
.data('originalHeight', $thisSticky.outerHeight());
console.log("Index is: "+index);
console.log("Offset: "+$thisSticky.offset().top);
topOffset.push($thisSticky.data('originalPosition'));
});
//only on scroll, run function
target.off("scroll.stickies").on("scroll.stickies", function(event) {
_whenScrolling(event);
});
}
};
var _whenScrolling = function(event) {
//get scrolling position
var $scrollTop = $(event.currentTarget).scrollTop();
console.log($scrollTop);
var scrolled = 0;
var count = 0;
$stickies.each(function(index,value) {
var $thisSticky = $(this),
//$stickyPosition = $stickies[count].data('originalPosition'),
$stickyHeight = $thisSticky.data('originalHeight'),
$nextStickyHeight = 0,
$newPosition,
$nextSticky;
$nextSticky = $stickies.eq(index + 1);
$nextStickyHeight = $stickies.eq(index + 1).outerHeight();
//if last header
if ($scrollTop >= topOffset[$stickies.length - 1]) {
console.log("Reached the end");
var $lastStickyElement = $stickies.eq($stickies.length - 1);
$lastStickyElement.addClass("header--fixed");
$lastStickyElement.css({
"top": 0
});
$nextSticky.removeClass("header--fixed");
$nextSticky.addClass("header--static");
$('.screen_inner_holder').css({
"padding-top": $stickyHeight
});
}
//if you scroll past the header nav
else if ($scrollTop >= topOffset[count] && $scrollTop < topOffset[count + 1]) {
scrolled = 1;
//push up the next one
var triggerToPushUp = (topOffset[count + 1] - $stickyHeight);
if($scrollTop >= triggerToPushUp && topOffset[count + 1] > $scrollTop) {
$thisSticky.css({
"top": -($scrollTop - triggerToPushUp)
});
$nextSticky.removeClass("header--static");
$nextSticky.addClass("header--fixed");
$nextSticky.css({
"top": topOffset[count + 1] - $scrollTop,
});
$('.screen_inner_holder').css({
"padding-top": $stickyHeight + $nextStickyHeight
});
console.log("Scroll top is greater that: " + triggerToPushUp);
console.log("Pushing sticky up");
return false;
}
else {
$thisSticky.removeClass("header--static");
$thisSticky.addClass("header--fixed");
$thisSticky.css({
"top": 0
});
$nextSticky.removeClass("header--fixed");
$nextSticky.addClass("header--static");
$('.screen_inner_holder').css({
"padding-top": $stickyHeight
});
}
}
//if you haven't scrolled past any headers
else {
$(this).removeClass("header--fixed");
$(this).addClass("header--static");
}
count++;
});
if (scrolled == 0) {
$('.screen_inner_holder').css({
"padding-top": 0
});
}
};
return {
load: load
};
})();
$(function() {
stickyHeaders.load($(".header"), $(window));
});
}