由于css属性,我滚动到div标签的margin-top太多。所以我认为jquery是解决这个问题的最佳解决方案。使用jquery滚动到div ID
我不知道为什么这不起作用,我对Js和Jquery很新。任何帮助我们不胜感激。
下面是对Js的简要介绍。我发现,当你的DIV ID在容器的(“HTML,身体”)更改为('集装箱)
jQuery(document).ready(function($){
var prevScrollTop = 0;
var $scrollDiv = jQuery('div#container');
var $currentDiv = $scrollDiv.children('div:first-child');
var $sectionid = 1;
var $numsections = 5;
$scrollDiv.scroll(function(eventObj)
{
var curScrollTop = $scrollDiv.scrollTop();
if (prevScrollTop < curScrollTop)
{
// Scrolling down:
if ($sectionid+1 > $numsections) {
console.log("End Panel Reached");
}
else {
$currentDiv = $currentDiv.next().scrollTo();
console.log("down");
console.log($currentDiv);
$sectionid=$sectionid+1;
console.log($currentDiv.attr('id'));
var divid =$currentDiv.attr('id');
jQuery('#container').animate({scrollTop:jQuery('#'+divid).position().top}, 'slow');
}
}
else if (prevScrollTop > curScrollTop)
{
// Scrolling up:
if ($sectionid-1 == 0) {
console.log("Top Panel Reached");
}
else {
$currentDiv = $currentDiv.prev().scrollTo();
console.log("up");
console.log($currentDiv);
$sectionid=$sectionid-1;
var divid =$currentDiv.attr('id');
jQuery('html, body').animate({scrollTop:jQuery('#'+divid).position().top}, 'slow');
}
}
prevScrollTop = curScrollTop;
});
});
检查:http://stackoverflow.com/questions/6677035/jquery-scroll-to-element – 2013-05-05 13:09:31
请减磅你的代码提交的相关部分,这将有助于人们试图回答你的问题。 – 2013-05-05 13:11:43