Scrolltop偏移量最大的问题 [英] Scrolltop offset top issue
本文介绍了Scrolltop偏移量最大的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面提到的代码滚动到div.我在移动视图中应用了scrollTop: $(target).offset().top-250
,以便该部分显示在标题高度以下,但似乎不起作用.
I used the below mentioned code for scrolling to a div. I applied scrollTop: $(target).offset().top-250
in the mobile view so that the section will display below the header height, but it seems it does not work.
$('#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;
});
推荐答案
以下脚本节省了我的时间.该脚本可能对其他人有用.
The below script saved my time. This script may be useful for others.
$('#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时处于活动状态.这个脚本可以吗
If want to make menu active on the scroll to div. can you this script
$(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");
}
});
};
这篇关于Scrolltop偏移量最大的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文