jquery - Jquery滚动到偏移目标div总是
问题描述
我添加了一个新的粘性菜单,并且我希望辅助子菜单(在加载时具有偏移)在滚动时保持偏移。
我有点n00b,我不明白。
我使用这个链接作为参考: Jquery scroll to offset target div on page load
// Goddamn scroll-to links
// Get offset height depending on our sweet ass screen width
if (window.innerWidth < 768) {
var theOffset = 16;
} else if ( $(this).is(':first')) {
//var offset = $('.entry-header').height() + 30;
} else {
var theOffset = $('.subheader').height() + 30;
}
$('a[href*=#]:not([href=#])').click(function() {
//$('.subheader li').removeClass('active');
//$(this).closest('li').addClass('active');
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
|| location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: (target.offset().top - theOffset)
}, 500);
return false;
}
}
});
and this:
if(window.location.hash) {
$('html,body').animate({
scrollTop: $(window.location.hash).offset().top-theOffset
}, 900, 'swing');
}
任何帮助,将不胜感激
解决方案
推荐阅读
- javascript - 将数组作为值传递给 JS
- django - 将数据从 API 上传到 Postgres DB 会停止,没有错误消息
- android - tns 医生说“未安装来自 Android SDK 的 abd”
- git - 存储一个特定的大块,Git
- java - 如何通过读取 .txt 来填充 TextView 并替换以前存在的 .txt。?
- google-chrome - 如何更新 apt-cache 存储库?
- django - 我正在尝试在 models.py 国家/地区实现一个可供用户选择的选项,但我无法正确显示它
- sql - 我们如何在 SQL 中将 2018-12-31 转换为 2019 年的第 1 周?
- java - 有没有办法在 Java 中模仿 Ruby 中的类实例变量概念?
- android - ListAdapter:在片段中抓取 item 或 itemPos