jquery - Scrolltop 偏移量最高的问题
问题描述
我使用下面提到的代码滚动到一个 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");
}
});
};
推荐阅读
- amazon-web-services - 为什么我的身份池中的临时 AWS 凭证未经授权?
- python - 如何编写一个程序来显示此数字列表中的值,这些数字按最大素数排序?
- c++ - 在 Linux 上使用 g++ 编译时出现 C++ Invalid Pointer 错误,但在 Windows 上的 CLion 中没有
- java - 数组求和数组的行和列
- admob - Google Admob 广告根本没有展示
- next.js - 在 Next.js 中放置 Router.events 相关代码的位置
- sql-server - 在 SQL Server 与 Excel 中将 int 转换为日期
- node.js - 为不同的 VS 解决方案使用特定版本的 npm/node?
- python-3.x - 比较多个条件时如何仅获得一个布尔值?
- mysql - 如何根据和条件获取数据