更改当前页面的 li 元素 id
这是我的问题: 我有一个修改过的 spasticNav jQuery 脚本,用于导航菜单,它在 id="selected" 的 li 项上设置一个“blob”。当您将鼠标悬停在菜单中的另一个链接上时,该斑点会移动,但随后会重置为 id="selected" 的 li 项目。
这一切都工作正常,但 li 项目必须具有 id="selected" ,否则 blob 将不会显示。我希望能够将当前页面的li项的id设置为“已选择”,而不必为每个页面制作唯一的导航列表。
这是 jquery:
(function($) {
$.fn.spasticNav = function(options) {
options = $.extend({
overlap : -15,
speed : 500,
reset : 1500,
color : '',
easing : 'easeOutExpo'
}, options);
return this.each(function() {
var nav = $(this),
currentPageItem = $('#selected', nav),
blob,
reset;
$('<li id="blob"></li>').css({
width : currentPageItem.outerWidth() + options.overlap,
height : currentPageItem.outerHeight() + options.overlap,
left : currentPageItem.position().left - options.overlap / 2,
top : currentPageItem.position().top - options.overlap / 2,
backgroundColor : options.color
}).appendTo(this);
blob = $('#blob', nav);
$('li:not(#blob)', nav).hover(function() {
// mouse over
clearTimeout(reset);
blob.animate(
{
left : $(this).position().left - options.overlap / 2,
width : $(this).width() + options.overlap
},
{
duration : options.speed,
easing : options.easing,
queue : false
}
);
}, function() {
// mouse out
reset = setTimeout(function() {
blob.animate({
width : currentPageItem.outerWidth() + options.overlap,
left : currentPageItem.position().left - options.overlap / 2
}, options.speed)
}, options.reset);
});
}); // end each
};
});
这是我的 html:
<div id="navcontainer"> <!--navigation-->
<div id="nav">
<ul>
<li id="selected"><a href="index.html">Home</a></li>
<li><a href="about_us.html">About Us</a></li>
<li><a href="ministries.html">Ministries</a></li>
<li><a href="calendar.html">Calendar</a></li>
<li><a href="resources.html">Resources</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</div>
</div>
可能有一个简单的解决办法,但我仍然是一个新手。
Here's my issue:
I have a modified spasticNav jQuery script for a navigation menu that sets a "blob" on the li item with id="selected". The blob moves as you hover over another link in the menu but then resets to the li item with id="selected".
This all works fine, but a li item has to have id="selected" or the blob won't show up. I want to be able to set the id of the li item of the current page to "selected" without having to make a unique navigation list for each page.
Here's the jquery:
(function($) {
$.fn.spasticNav = function(options) {
options = $.extend({
overlap : -15,
speed : 500,
reset : 1500,
color : '',
easing : 'easeOutExpo'
}, options);
return this.each(function() {
var nav = $(this),
currentPageItem = $('#selected', nav),
blob,
reset;
$('<li id="blob"></li>').css({
width : currentPageItem.outerWidth() + options.overlap,
height : currentPageItem.outerHeight() + options.overlap,
left : currentPageItem.position().left - options.overlap / 2,
top : currentPageItem.position().top - options.overlap / 2,
backgroundColor : options.color
}).appendTo(this);
blob = $('#blob', nav);
$('li:not(#blob)', nav).hover(function() {
// mouse over
clearTimeout(reset);
blob.animate(
{
left : $(this).position().left - options.overlap / 2,
width : $(this).width() + options.overlap
},
{
duration : options.speed,
easing : options.easing,
queue : false
}
);
}, function() {
// mouse out
reset = setTimeout(function() {
blob.animate({
width : currentPageItem.outerWidth() + options.overlap,
left : currentPageItem.position().left - options.overlap / 2
}, options.speed)
}, options.reset);
});
}); // end each
};
});
and here's my html:
<div id="navcontainer"> <!--navigation-->
<div id="nav">
<ul>
<li id="selected"><a href="index.html">Home</a></li>
<li><a href="about_us.html">About Us</a></li>
<li><a href="ministries.html">Ministries</a></li>
<li><a href="calendar.html">Calendar</a></li>
<li><a href="resources.html">Resources</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</div>
</div>
There is probably an easy fix to this, but i'm still a newb.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保所有导航 li 都没有硬编码的
id="selected"
,然后您可以根据当前 URL 检查导航 href 并动态添加 id,如下所示:Make sure none of the nav li's have a hardcoded
id="selected"
, then you can check the nav hrefs against the current URL and add the id dynamically like so: