- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此代码最初用于在页面加载时平滑滚动到 anchor 。
尝试添加一个监听器,以便它可以在单击时滚动到同一页面上的 anchor ,但我被卡住了。
// <!--smooth scroll to anchors-->
(function(jQuery){
var jump=function(e){
if (e){
e.preventDefault(); //prevent the "normal" behavior which would be a "hard" jump
var mytarget = jQuery(this).attr("href"); //get the target
}else{
var mytarget = location.hash; //sets the target to the anchor part of a URL
}
jQuery('html,body').animate( //perform animated scrolling
{
scrollTop: jQuery(mytarget).offset().top - 100 //get top-position of target-element and set it as scroll target and move down 100px for fixed nav
}, 1000,function(){ //scrolldelay: 2 seconds
location.hash = mytarget; //attach the hash (#jumptarget) to the pageurl
});
}
jQuery('html, body').hide()
// on load
jQuery(document).ready(function(){
jQuery('a[href^="#"]').bind("click", jump);
if (location.hash){
setTimeout(function(){
jQuery('html, body').scrollTop(0).show()
jump()
}, 0);
}else{
jQuery('html, body').show()
}
});
// on click
var inpage-nav =document.querySelectorAll('.in-page-nav');
for(var i=0;i<cell.length;i++){
inpage-nav[i].addEventListener('click',jump);
}
})(jQuery)
// <!--End smooth scroll-->
页面内导航的 HTML:
<div class="just-a-nav">
<ul>
<li class="filter"><a class="in-page-nav" href="#item1">Item 1</a></li>
<li class="filter"><a class="in-page-nav" href="#item2">Item 2</a></li>
<li class="filter"><a class="in-page-nav" href="#item3">Item 3</a></li>
</ul>
</div>
最佳答案
示例 HTML:希望这会有所帮助;这是我的 fiddle :https://jsfiddle.net/Ledg92c7/
<a href="#goto-1" class="in-page-nav">Go-Target-1</a> | <a href="#goto-2" class="in-page-nav">Go-Target-2</a>
<div id="goto-0" style="height:1200px;background:green">
.....Demo Content 0....
</div>
<div id="goto-1" style="height:300px;background:red">
.....Demo Content 1....
</div>
<div id="goto-2" style="height:400px;background:blue">
.....Demo Content 2....
</div>
jQuery:
(function($){
var jump=function(e){
if (e){
e.preventDefault(); //prevent the "normal" behavior which would be a "hard" jump
var mytarget = $(this).attr("href"); //get the target
}else{
var mytarget = location.hash; //sets the target to the anchor part of a URL
}
$('html,body').animate({ //perform animated scrolling
scrollTop: $(mytarget).offset().top - 100 //get top-position of target-element and set it as scroll target and move down 100px for fixed nav
}, 1000,function(){ //scrolldelay: a few milliseconds
location.hash = mytarget; //attach the hash (#jumptarget) to the pageurl
});
}
$('html, body').hide();
// on load
$(document).ready(function(){
$('a[href^="#"]').bind("click", jump);
if (location.hash){
setTimeout(function(){
$('html, body').scrollTop(0).show();
jump();
}, 0);
}else{
$('html, body').show();
}
});
// on click
/*var inpageNav =document.querySelectorAll('.in-page-nav');
for(var i=0;i<inpageNav.length;i++){
inpageNav[i].addEventListener('click',jump);
}*/
$('.in-page-nav').on('click',jump);
})(jQuery);
关于javascript - 同一函数的 jquery、onclick 和 onload 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41366772/
所以我想隐藏一个div,所以我有我的代码 function hide(){hide div code} document.onload = hide(); 不起作用。 window.onload =
我有一个外部 js 文件,它有一个函数,该函数旨在在文档正文加载后运行。这是我到目前为止所拥有的,但它并没有像它应该的那样工作。请一些帮助。 外部JS文件 window.document.body.a
我觉得这很有趣。考虑以下代码, window.onload 和 body onload=""调用相同的函数。但是,结果不同。在我看来,window.onload 的集合有问题。这是代码:
我多次在链接上使用 Shadowbox,我知道 href 指示 Shadowbox 像这样在框中加载什么页面: 但是,我现在需要在用户进入页面时立即显示一个阴影框。不是onclick,而是onloa
我正在制作一个页眉和页脚不变的网站。单击站点时,主 div 会更改。这是代码: Item 1 ... $("#item").click(function(){$("#main").load("i
我在网页中有 2 个函数 (js) 用于将图像附加到 Canvas 元素: function attachImage(tile, x, y) { base_image = new
首先向大家问好。我是 jquery 的新手,并且在学习曲线上。 我的脚本是这样的: $(function () { $('#content').find('#nav a').on('click'
我今天遇到了一个奇怪的错误。我有一个简单的用户脚本,它添加了一个 window.onload事件到所有网站。但是其中一个网站有一个 定义。发生的事情是 window.onload像往常一样工作但是安装
今天我遇到了一个奇怪的问题: window.onload 在引用名为 onload 的函数时似乎不会触发。 我以前从未注意到这一点: 测试 1: window.onload=onload; funct
假设你想用 JS 把一个元素变成蓝色,像这样: Boy, I sure do wish I was blue 上面的行不起作用,因为 this 以该上下文中的 window 对象为目标。您可以使用 I
我的问题与此处提出的问题类似,但又略有不同,window.onload vs 那道题是用 window.onload 和内联 js 的对比。 我的问题是下面的区别。假设正文标签的 ID 为“body
window.onload事件和body标签的onload事件到底有什么区别?我什么时候使用哪个以及如何正确使用? 最佳答案 window.onload = myOnloadFunc和 是使用同一事件
哪个得到更广泛的支持:window.onload 或 document.onload? 最佳答案 他们什么时候开火? window.onload 默认情况下,它会在整个页面加载时触发,包括其内容(图像
之间有什么区别: window.onload = someFunction; window.onload = someFunction(); 括号在最后。它们有什么不同吗? 我们一般使用第一个!如果我
在 this question 的答案中,我们读到 function f() {}在本地定义名称,而 [var] f = function() {}全局定义它。这对我来说很有意义,但是这两个声明之间存
当我将索引值传递给 onLoad() 时,因此当它开始在后台运行时,它将使用索引值来索引数组。问题是,从表面上看,当 onLoad() 开始运行时,它引用的索引值始终是最后一个索引。 我尝试将每个请求
假设我有这个页面: Hello, world! test.html 包含以下内容: huge.png由于某种原因是100mb。 我无法控制 test.html,但我必须在我的页面中包含 ifram
我正在做以下工作。如果启用了 js,它基本上会通过 ?answer=1。在我添加 onload 参数之前它一直有效(因为我希望这在没有用户触发的情况下发生)。但是,添加 onload 似乎会停止(否则
我可以同时使用body onload和window.onload吗?我已经使用此代码尝试过 window.onload = alertSec; 但是没有成功。我只是需要有人向我确认一下
我可以同时使用body onload和window.onload吗?我已经使用此代码尝试过 window.onload = alertSec; 但是没有成功。我只是需要有人向我确认一下
我是一名优秀的程序员,十分优秀!