由於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