Javascript 将overflow-y设置为Scroll(jQuery)时,自动将div水平滚动到最末端
我使用的jQuery脚本根据div的宽度自动水平滚动div。但是我需要它根据div内部内容的结尾滚动到div的最末尾。div有一个“overflow-y:scroll”属性,所以我希望它滚动所有内容,直到到达末尾 这是我当前使用的脚本:Javascript 将overflow-y设置为Scroll(jQuery)时,自动将div水平滚动到最末端,javascript,jquery,css,scroll,autoscroll,Javascript,Jquery,Css,Scroll,Autoscroll,我使用的jQuery脚本根据div的宽度自动水平滚动div。但是我需要它根据div内部内容的结尾滚动到div的最末尾。div有一个“overflow-y:scroll”属性,所以我希望它滚动所有内容,直到到达末尾 这是我当前使用的脚本: function animatethis(targetElement, speed) { var width = $(targetElement).width(); $(targetElement).animate({ marginLeft: "
function animatethis(targetElement, speed) {
var width = $(targetElement).width();
$(targetElement).animate({ marginLeft: "-="+width},
{
duration: speed,
complete: function ()
{
targetElement.animate({ marginLeft: "+="+width },
{
duration: speed,
complete: function ()
{
animatethis(targetElement, speed);
}
});
}
});
};
animatethis($('#q1'), 5000);
它确实会滚动,但不会滚动到div内内容的最后。下面是一个jFiddle,它显示了我的意思:
如何让它自动水平滚动到内容的末尾,而不仅仅是div的宽度
我希望这一切都有意义。谢谢。您可以使用
scrollWidth
和clientWidth
设置scrollLeft
属性的动画:
function animatethis(targetElement, speed) {
var scrollWidth = $(targetElement).get(0).scrollWidth;
var clientWidth = $(targetElement).get(0).clientWidth;
$(targetElement).animate({ scrollLeft: scrollWidth - clientWidth },
{
duration: speed,
complete: function () {
targetElement.animate({ scrollLeft: 0 },
{
duration: speed,
complete: function () {
animatethis(targetElement, speed);
}
});
}
});
};
animatethis($('#q1'), 5000);
从中可以看出结果。所以当滚动条向右结束时,您希望最后一个单词“END”也隐藏在左侧?@EnmanuelDuran否,只是为了显示单词“END”。不需要再进一步了。谢谢