jquery scrollTop不工作
所以我试图让你点击旋转木马中的链接,它会将你向下滚动到页面内容 以下是我的jquery:jquery scrollTop不工作,jquery,html,syntax,slide,Jquery,Html,Syntax,Slide,所以我试图让你点击旋转木马中的链接,它会将你向下滚动到页面内容 以下是我的jquery: $('#picks li a').click(function(){ $('html, body').animate({ scrollTop: $( $.attr(this, 'href') ).offset().top }, 500); return false; }); 但是,我在控制台中收到此错误 错误:语法错误,无法识别的表达式: 这是正确的链接位置,但不确
$('#picks li a').click(function(){
$('html, body').animate({
scrollTop: $( $.attr(this, 'href') ).offset().top
}, 500);
return false;
});
但是,我在控制台中收到此错误
错误:语法错误,无法识别的表达式:
这是正确的链接位置,但不确定如何更正它,以便jquery在幻灯片中添加
谢谢 attr方法返回一个包含整个URL的字符串。在字符串中的“#”之前,您需要清除所有内容。可以使用查找“#”字符的索引,也可以使用仅获取所需字符串的部分。如果属性href引用元素的id,则需要在实际id之前使用
$('#picks li a').click(function(){
$('html, body').animate({
scrollTop: $( '#'+$.attr(this, 'href') ).offset().top
}, 500);
return false;
});
还要注意,这将触发两次;我知道你添加html是为了和firefox一起工作
所以你可以:
$('#picks li a').click(function(){
$(Query.browser.mozilla ? "html" : "body").animate({
scrollTop: $( '#'+$.attr(this, 'href') ).offset().top
}, 500);
return false;
});
或者,至少在以下情况之前停止动画:
$('#picks li a').click(function(){
$('html, body').stop(true,true).animate({
scrollTop: $( '#'+$.attr(this, 'href') ).offset().top
}, 500);
return false;
});
为了防止此双重调用出现一些问题,当我转到您的链接时,我遇到的第一个问题是,
$
未定义,当我尝试从控制台进行测试时。然而,jQuery
工作正常,因此您的库显然已加载
下一个问题是你的代码。您正在调用href
,它将获取整个链接,而不仅仅是散列标记
jQuery('#picks li a').click(function(e){
e.preventDefault();
var id = $(this).attr('href').split('#');
id = '#' + id[id.length-1];
jQuery('html, body').animate({
scrollTop: $( id ).offset().top
}, 500);
return false;
});
我试过几种方法,但都不管用 对我来说,这与Angularjs的组合有效
$( "html, body" ).scrollTop( $("#main_div").offset().top );
main_div是您想要的任何HTML元素中的id
我不喜欢URL顶部的Angularjs标签,这就是为什么我没有使用它们
$location.hash('main_div');
我认为动画的东西使事情复杂化了