【发布时间】:2016-08-09 23:37:23
【问题描述】:
我想捕捉到 div 中的部分,但是我想在不滚动窗口的情况下执行此操作。我希望能够在该 div 内滚动。
我认为我对自己的解释不够好。当我滚动时,窗口滚动,然后文章被卡入到位,依此类推。我希望这样当我在#content 中滚动时,文章将被对齐到位。
有什么建议吗?
$(document).ready(function() {
$("article").css("height", $(window).height() + "px");
$("#content").sectionsnap({
delay : 100
, selector : 'article'
, reference : 1
, animationTime : 600
});
});
(function($) {
$.fn.sectionsnap = function( options ) {
var $wrapper = this
, direction = 'down'
, currentScrollTop = $("section").scrollTop()
, scrollTimer
, animating = false;
// check the direction
var updateDirection = function() {
if ($("section").scrollTop() >= currentScrollTop)
direction = 'down';
else
direction = 'up';
currentScrollTop = $("section").scrollTop();
}
// return the closest element (depending on direction)
var getClosestElement = function() {
var $list = $wrapper.find(settings.selector)
, wt = $("section").scrollTop()
, wh = $("section").height()
, refY = wh * settings.reference
, wtd = wt + refY - 1
, $target;
if (direction == 'down') {
$list.each(function() {
var st = $(this).position().top;
if ((st > wt) && (st <= wtd)) {
$target = $(this);
return false; // just to break the each loop
}
});
} else {
wtd = wt - refY + 1;
$list.each(function() {
var st = $(this).position().top;
if ((st < wt) && (st >= wtd)) {
$target = $(this);
return false; // just to break the each loop
}
});
}
return $target;
}
// snap
var snap = function() {
var $target = getClosestElement();
if ($target) {
animating = true;
$('html, body').animate({
scrollTop: ($target.offset().top)
}, settings.animationTime, function() {
window.clearTimeout(scrollTimer);
animating = false;
});
}
}
// on window scroll
$("section").scroll(windowScroll);
return this;
};
})(jQuery);
非常感谢任何建议
【问题讨论】:
-
我同意你解释得不够清楚
-
kuhl.pl/github/sectionsnap 当窗口滚动时,下一个元素立即进入视图。我想这样做,如果该部分(不是窗口)滚动,那么下一个元素就会出现。这说明清楚了吗?