- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在寻找通过滚动添加/删除类的方法,并找到了几个接近的示例,但我似乎无法将其用于我正在处理的网站。
我知道这里有很多线程提出了类似的问题,但我找到的解决方案并没有按照我需要的方式工作。
旁注:我是 jQuery 的新手,所以也许我遗漏了一些小东西?
所以我有一个单页网站,响应式,固定标题,当你点击一个导航链接时,你会平滑地向下滚动(使用 CSS)到你点击的 anchor 。很简单。
现在,有一个 CSS 动画,当您单击 header 中的任何导航链接时会发生,它会应用于 h1
标记。例如,您单击“介绍”(我有一个类“1”,所以我可以选择它),向下滚动,现在看到内容中“介绍”一词的动画。
$('.intro').click(function(){
setTimeout(function(){
$('.1').addClass('txt_anim');
},1000);
setTimeout(function(){
$('.1').removeClass('txt_anim');
},11000);
});
<h1 class="1">intro</h1>
同样,简单。我可以通过这种方式在所有必要的标题上完成动画,但用户必须单击 anchor 才能看到动画。
所以,现在我需要在用户滚动时应用此动画。我找到的许多解决方案都是一系列 if/else
语句,这些语句取决于用户向下滚动的距离,使用 scrollTop()
这个例子使用了一个他们定义的变量scroll
。
if (scroll <= 500) {
$("#m1").addClass("active");
}
此解决方案的问题:由于此网站是响应式的,因此根据您的浏览器的宽度或某人设备的宽度,网站的内容被压缩并下推,从而使网站变长。因此,很有可能将动画应用于标题,而用户甚至在屏幕上都看不到它。
我问同事是否有解决方案,他做了一些挖掘并找到了这个 jsfiddle并说我应该尝试类似于他们使用的代码:
function onScroll(event){
var scrollPos = $(document).scrollTop();
$('#menu-center a').each(function () {
var currLink = $(this);
var refElement = $(currLink.attr("href"));
if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
$('#menu-center ul li a').removeClass("active");
currLink.addClass("active");
}
else{
currLink.removeClass("active");
}
});
}
我已经研究了很长时间了,但我得不到任何结果……再说一次,jQuery 的东西还是很新。
我想我想问的是:有没有办法告诉浏览器“嘿,当这个标题离顶部“这么远”时(比如顶部之间的一定数量的像素或百分比)文档的顶部和元素的顶部),应用此类”,而不是“当用户滚动“这么多”时应用此类。”
最佳答案
下面的所有内容都在下面的 fiddle 中:http://jsfiddle.net/NuVAv/
首先,我会考虑更动态地设置导航,使用 data
属性或 href
将导航链接关联到内容部分,如下所示:
HTML
<ul id="nav">
<li><a href="#intro">Intro</a></li>
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
</ul>
<div id="intro">...</div>
<div id="section1">...</div>
<div id="section2">...</div>
JS
$("#nav a").click(function(e){
var $link = $(this),
sectionID = $link.attr("href"),
$section = $(sectionID),
scrollTo = $section.offset().top;
$("html,body").animate({scrollTop: scrollTo}, 300);
e.preventDefault();
});
其次,这是我可能如何编写我的滚动处理程序,内嵌注释:
JS
var $allScrollSections = $("div"), // cache references to static items
$w = $(window);
$w.scroll(function(){
var scrolled = $w.scrollTop(),
scrolledPast = [],
$currentSection = null;
$allScrollSections.each(function(i,section){
var $section = $(section),
sectionTop = $section.offset().top;
// add all sections that you have scrolled beyond to an array
if(scrolled >= sectionTop) scrolledPast.push($section);
});
// the last section you have scrolled past will be your "current"
$currentSection = scrolledPast.length ? scrolledPast[scrolledPast.length-1] : null;
// add/remove the "animate" class to/from the "current section"
if($currentSection && !$currentSection.hasClass("animate")){
$allScrollSections.removeClass("animate");
$currentSection.addClass("animate");
} else if (!$currentSection){
$allScrollSections.removeClass("animate");
}
});
关于jquery - 通过滚动向 h1 标签添加/删除类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23922111/
我尝试在安装了多类型 MFC 库的 visual studio 2015 MFC 上运行以前编写的 MFC c++ 代码。 但是,我这里仍然有 12 个关于缺少函数的错误: IntelliSense:
我正在学习 OOP 并且有疑问。假设我有一个包含 ClassB.h 的文件 ClassA.h,并且在某些时候我的 ClassB.h 需要包含 ClassA .h。 这会产生一个错误,我想我明白为什么会
我开始使用 CUDA 进行编程,在一些示例中我找到了包含文件 cuda.h、cuda_runtime.h 和 cuda_runtime_api.h 包含在代码中。有人可以向我解释一下这些文件之间的区别
我有一些生成正则表达式的代码。那么下面的表达式实际上是: ^(?:\s*((exclude|include|hide|show|protect|risk|dir-merge|merge)),\s*((
我一直在查看一些源代码,以更好地了解我们使用的这款游戏的核心,并编写更可靠、更快速的插件。然后我发现了这段奇怪的代码...... public void setMaxH(double amount)
通常我们会使用标准类型作为 std::unordered_map 的键和值.但现在我需要自定义我自己的键和值类。 键类在block_cache_key.h 中定义如下: #ifndef BLOCK_C
例如,我想要两个头文件,它们可以依赖于另一个头文件中的函数。 //Header1.h file #include Header2.h void h1(){ //... func1(); } v
我正在研究来自 Sedgewick 的 Shell 排序 Algorithms in C part 1-4在第 172 页。 我使用 size (数组的长度),而不是 l和 r (开始和结束);所以我
我在 macOS BigSur 上通过 VMWare 使用 Ubuntu 20.04.2 LTS。我安装了最新版本的 tcl、tcl-dev、tk 和 tk-dev - 版本 8.6。我想编译 Arc
我用我的 glu 和 gl 头文件构建了一个 OpenGL 程序,默认包含在 windows 7 专业版中。现在,我买了一本描述 OpenGL 游戏开发的书。这本书的作者说,我必须在我的项目中包含 g
我想在 token 中保留特殊字符,同时仍对特殊字符进行 token 化。说我有话 "H&R Blocks" 我想将其标记为 "H", "R", "H&R", "Blocks" 我读了http://w
关于 hash 作为 trans 参数的另一个问题。在下面的代码中,简单地使用 hash 会给出不正确的结果,但是将其替换为 keys 和 values 会使其正确。怎么了? my @alph1 =
我已经编写了一个 C 程序,它获取屏幕像素的 RGB 值 (0-255),并知道其位置 (x,y)。它可以在 Linux 中运行,但是当我尝试在 Visual Studio (Windows) 中编译
我已经使用 Windows 7 专业版中默认包含的 glu 和 gl 头文件构建了一个 OpenGL 程序。现在,我买了一本描述 OpenGL 游戏开发的书。这本书的作者说,我必须将glew head
#include using namespace std; #include //#include int main() { initscr();
h:messages h:form 内的组件还显示与外部组件相关的消息。 如何限制它只显示与包含 h:form 内的组件相关的消息? 我不喜欢用单独的h:message来使我的代码膨胀。每个输入组件的
我下载了示例代码和 cpp 文件,其中包含 list.h、queue.h 和 vector.h 等头文件,如果我尝试构建,我会收到“ fatal error :没有这样的文件或目录编译终止”我想我应该
我有一个编译成功的桌面项目,但是在我向项目添加新配置以支持 Windows Mobile 平台后,我收到以下错误: error C2146: syntax error : missing ';' be
有很多关于这个错误的帖子,但我无法解决它,我希望你能拿出解决方案。我在 Ubuntu 机器上。 ~/graphmap2$ 在这个文件夹中,我下载了 zlib。可以看图 经过一番谷歌搜索后,我还注意到没
是否可以在 Visual C++ 中使用以下 header : 图.h dos.h bios.h 最佳答案 据我所知,无法在 Visual C++ 中使用它, 与此同时,我希望您关注 Open Wat
我是一名优秀的程序员,十分优秀!