当点击一个元素时,我试图让页面从当前位置向下滚动150px.因此,假设您大约在页面中滚动了一半.您单击此链接,它将向下滑动150个像素.
这有可能与jQuery?
我一直在看scrollTop和scrollTo插件,但我似乎无法连接点.
我使用以下代码使用jQuery滚动到锚点:
$(document).ready(function() {
function filterPath(string) {
return string
.replace(/^\//,'')
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
}
var locationPath = filterPath(location.pathname);
var scrollElem = scrollableElement('html', 'body');
$('a[href*=#]').each(function() {
var thisPath = filterPath(this.pathname) || locationPath;
if ( locationPath == thisPath
&& (location.hostname == this.hostname || !this.hostname)
&& this.hash.replace(/#/,'') ) {
var $target = $(this.hash), target = this.hash;
if (target) {
var targetOffset = $target.offset().top;
$(this).click(function(event) {
event.preventDefault();
$(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
location.hash = target;
});
});
}
}
});
// use the first element that …Run Code Online (Sandbox Code Playgroud) 我从stackoverflow获得了一个滚动到页面顶部的解决方案
$('html, body').animate({scrollTop: $("#page").offset().top}, 2000);
Run Code Online (Sandbox Code Playgroud)
但我不确定为什么要使用'html,body'作为scrollTop而不仅仅是'html'?
我有一个带有列表的div容器,这个列表中只有一个项目一次可见,其余项目被隐藏(容器有溢出:隐藏).
我希望jQuery在点击完全链接后显示所请求的项目:
有任何想法吗?请问滚动来帮助我吗?我试过这个插头但没有运气.我宁愿不使用iframe.