简体   繁体   English

动画{“ Top”}如何在最高位置为0 px时停止div滚动-Jquery

[英]Animate {“Top”} how to stop scrolling div on top position being 0 px - Jquery

Trying to create a scrolling div. 尝试创建滚动div。 Wanted to stop (thescrollingdiv) div once it has reached a particular top position and scrolled all the way to the bottom and not overshoot the parent div into infinity scrolling zone. 想要在到达特定的顶部位置并一直滚动到底部且不将父div越过无限滚动到无限滚动区域时停止(thescrollingdiv) div。 thescrollingdiv does not have any height specified but its parent div does.Thanks. thescrollingdiv没有指定任何高度,但其父div却指定了。谢谢。

$('#div a).click(function(e){ e.preventDefault(); $('#thescrollingdiv').stop(true,true).animate({ "top": '-=100px'}, 500)

ScrollTop tells you where you are at. ScrollTop告诉您您的位置。 Check the existing top against scrolltop and work the math to set your limits. 对照scrolltop检查现有顶部,然后进行数学运算以设置极限。

var scrollTop = $('#thescrollingdiv').scrollTop();
var newTop = parseFloat($('#thescrollingdiv').css('top')) - 100;

if (scrollTop < 0) {
  newTop = 0;
}

$('#thescrollingdiv').stop(true,true).animate({ "top": newTop}, 500)

UPDATE 更新

Something like this. 这样的事情。

var topLimit = 0;
var bottomLimit = 800;

var containerTop = parseFloat($('container').css('top'));
var containerBottom = parseFloat($('container').css('height')) + containerTop;

var destination = containerTop - 100;

// compensate for going too far up
destination = (destination < 0) ? 0 : destination;

// compensate for going too far up
destination = (containerBottom > bottomLimit) ? bottomLimit : destination;

// now that you know you are within your custom limits, animate it.
animate(destination);

This is almost pseudo code as I don't know what your code looks like, but it gives you an idea. 这几乎是伪代码,因为我不知道您的代码是什么样子,但是可以为您提供一个思路。 You have to actually DO THE WORK in setting the limits for your 'newTop', before you call animate in the first place. 在首先调用动画之前,您必须在设置“ newTop”的限制时实际进行工作。

You can figure it out. 您可以弄清楚。 Don't be a lazy programmer, though. 但是,不要成为一个懒惰的程序员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM