2013-10-24 75 views
1

我认为这很容易。使用jquery滚动到父div top

我有以下的HTML

<div> 
<div id="child"></div> 
</div> 

我试过几件事情

if ($('#child').length){ 
    $('html, body').animate({scrollTop: $(this).parent().offset().top}, 'slow'); 
} 

if ($('.success_div').length){ 
    pdiv = $(this).parent(); 
    $('html, body').animate({scrollTop: pdiv.offset().top}, 'slow'); 
} 

错误:类型错误:pdiv.offset(...)是未定义

回答

2

这个怎么样?

if ($('#child').length){ 
    $('body').animate({scrollTop: $('#child').parent().offset().top},'slow'); 
}); 

调用的元素在if语句不选择它,所以$(本)没有匹配的if ($('#child').length){里面,所以我再次呼吁$('#child')语句的内部。