拥有平滑滚动到 id 的现有 JavaScript,希望帮助为特定 id 添加例外
Have existing JavaScript that smooth scrolls to id's, would like help adding an exception for a specific id
这里的JavaScript将平滑滚动到所有哈希链接。我需要id=nav
及其生成的哈希链接,/#nav
下面的JavaScript忽略。 id=nav
用于菜单响应能力,当它被下面的JavaScript征用时,它会破坏菜单。
有人可以建议我如何忽略以下 JavaScript 中的特定 id 吗?
我还制作了一个有效的HTML演示,您可以在下面看到。
.top {
position: fixed;
top: 0;
}
a {
margin-left: 20px;
display: block;
}
#nav {
height: 800px;
background-color: #777;
display: block;
}
#test1 {
height: 800px;
background-color: #ccc;
display: block;
}
#test2 {
height: 800px;
background-color: #111;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
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 - 80
}, 500);
return false;
}
}
});
});
</script>
<body>
<div class="top">
<a href="#nav">nav</a>
<a href="#test1">1</a>
<a href="#test2">2</a>
</div>
<div id="nav"></div>
<div id="test1"></div>
<div id="test2"></div>
</body>
在
JavaScript 的第二行之后添加这一行:
if($(this).attr("href") == "#nav") return;
所以它看起来像这样:
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if($(this).attr("href") == "#nav") return;
//... Rest of code
JSFiddle
这是如何工作的,它检查单击链接的href
属性的值是否"#nav"
。如果是,则函数返回false
并且不会执行。
只需在选择器中添加条件,或与 .not 选择器组合,如下所示。
.top {
position: fixed;
top: 0;
}
a {
margin-left: 20px;
display: block;
}
#nav {
height: 800px;
background-color: #777;
display: block;
}
#test1 {
height: 800px;
background-color: #ccc;
display: block;
}
#test2 {
height: 800px;
background-color: #111;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(function() {
$('[href="#nav"]').click(function(e) {e.preventDefault; return false;}); // Use this line if you don't want to go to top. Personally I would not use anchors if their base functionality is removed.
$('a[href*="#"]:not([href="#"])').not('[href="#nav"]').click(function() {
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 - 80
}, 500);
return false;
}
}
});
});
</script>
<body>
<div class="top">
<a href="#nav">nav</a>
<a href="#test1">1</a>
<a href="#test2">2</a>
</div>
<div id="nav"></div>
<div id="test1"></div>
<div id="test2"></div>
</body>
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 需要帮助设置json数组
- difference between '$(<%= DDL.ID %>) & $('
- 使用Javascript获取所选选项ID
- JQuery需要帮助理解(i,el)
- 根据id将json数组组合为一个json数组
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 对id以某个字符串开头的元素进行计数
- 需要帮助谷歌地图方向面板在FancyBox中显示
- 如果元素's的ID以数字开头
- 我需要为我的朋友在这个项目上提供帮助
- 通过id和class属性获取元素
- 根据选项卡内部的文本链接中的哈希ID动态选择jqueryUI选项卡
- 使用当前日期生成随机id
- Javascript-ID冲突的几率
- 使用密码对话框Javascript请求帮助
- 如何在ID的帮助下获得HTML elment
- 在
id 的帮助下更改 中的内容 - 拥有平滑滚动到 id 的现有 JavaScript,希望帮助为特定 id 添加例外