Javascript Jquery scrollTo仅以小增量向上滚动
我正在使用scrollTo插件滚动到一些标题。问题在于,它只会以浏览器窗口高度确定的小增量向上滚动。它似乎无法识别我正在使用的idJavascript Jquery scrollTo仅以小增量向上滚动,javascript,jquery,jquery-animate,scrollto,Javascript,Jquery,Jquery Animate,Scrollto,我正在使用scrollTo插件滚动到一些标题。问题在于,它只会以浏览器窗口高度确定的小增量向上滚动。它似乎无法识别我正在使用的id ,您需要滚动到页面底部,并用两个手指选择项目。您需要手动向下滚动并单击右侧的链接以查看上面提到的行为。很抱歉,这有点复杂。无需使用插件 请尝试下面的代码- $('html').animate({ scrollTop: $('#id where you want to scroll').offset().top }, 2000); 我用这个小片
,您需要滚动到页面底部,并用两个手指选择项目。您需要手动向下滚动并单击右侧的链接以查看上面提到的行为。很抱歉,这有点复杂。无需使用插件 请尝试下面的代码-
$('html').animate({
scrollTop: $('#id where you want to scroll').offset().top
}, 2000);
我用这个小片段来嘲讽
/// <summary>
/// Scrolls the page to a single matched element.
/// Limitations: The document can only scroll a maximum of it's height. If an element is at the bottom of a page with nothing
/// below it then it cannot move that element to the top of the page as nothing exists to fill the space.
/// </summary>
/// <param name="target" type="String">
/// The element to scroll to
/// </param>
/// <param name="padding" type="Integer">
/// The padding to add to the scrolling destination. Negative values reduce the scrolling distance.
/// </param>
/// <param name="speed" type="Integer">
/// The the speed at which to perform the animation. Positive integers only.
/// </param>
function scrollTo(target, padding, speed) {
// Define our variables.
var target_offset, target_top;
// Fix our value to add the selector.
if (target.indexOf("#") == -1) {
target = "#" + target;
}
// Get the top offset of the target anchor and add any necessarry padding.
target_offset = $(target).offset();
target_top = target_offset.top + padding;
// Scroll to that anchor by setting the body to scroll to the anchor top.
$("html").animate({ scrollTop: target_top }, speed);
}
谢谢你们两位的回答。我发现问题是我用CSS隐藏了我的名字标签的父div,用JS显示。这导致浏览器不知道如何定位名称标记或类似的内容。我删除了显示:没有从CSS和瞧 众所周知,同时使用html和body可以让opera运行代码两次。只要html就行了。