我使用了以下提到的代码滚动到DIV。我在移动视图中应用了scrollTop: $(target).offset().top-250
,以便该部分显示在标题高度下方,但似乎不起作用。
$('#portfolioNavbar ul li a[href*=#]').bind('click', function(e) {
e.preventDefault();
var target = $(this).attr("href");
$('html, body').stop().animate({
scrollTop: $(target).offset().top - 250
}, 600, function() {
location.hash = target; //attach the hash (#jumptarget) to the pageurl
});
return false;
});
以下脚本节省了我的时间。这个脚本可能对他人有用。
$('#portfolioNavbar ul li a').click(function() {
var tghsh = $(this).attr('href').substring(1);
var headerHeight = $('.portfolioi_left').outerHeight();
var winwid = $(window).width();
var doffset = $('#'+tghsh).offset().top-20;
var doffset1 = $('#'+tghsh).offset().top-280;
if(winwid <= 991) {
doffset1 -= headerHeight;
$('html, body').animate({scrollTop:doffset1},700);
return false;
}
$('html, body').animate({scrollTop:doffset},700);
return false;
});
如果要使菜单上的菜单在滚动中处于div。您可以这个脚本
吗$(document).ready(function() {
$('#portfolioNavbar ul li a').click(function() {
$(document).off("scroll");
$('#portfolioNavbar ul li a').each(function () {
$(this).removeClass('active');
})
$(this).addClass('active');
var tghsh = $(this).attr('href').substring(1);
var headerHeight = $('.portfolioi_left').outerHeight();
var winwid = $(window).width();
var doffset = $('#'+tghsh).offset().top-20;
var doffset1 = $('#'+tghsh).offset().top-280;
if(winwid <= 991) {
doffset1 -= headerHeight;
$('html, body').animate({scrollTop:doffset1},700);
return false;
}
$('html, body').animate({scrollTop:doffset},700);
return false;
});
});
function onScroll(event){
var scrollPos = $(document).scrollTop();
$('#portfolioNavbar ul li a').each(function () {
var currLink = $(this);
var refElement = $(currLink.attr("href"));
if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
$('#portfolioNavbar ul li a').removeClass("active");
currLink.addClass("active");
}
else{
currLink.removeClass("active");
}
});
};