JQuery 导航活动页面
我正在构建一个 jQuery 导航,但我似乎无法弄清楚如何使导航保持在活动页面上的滚动状态。
HTML...
<body>
<form id="form1" runat="server">
<div>
<div id="pageWrap">
<div id="pageBody">
<a class="hoverBtn" href="#"></a>
<a class="hoverBtn1" href="#"></a>
<a class="hoverBtn2" href="#"></a>
<a class="hoverBtn3" href="#"></a>
<a class="hoverBtn4" href="#"></a>
<a class="hoverBtn5" href="#"></a>
<a class="hoverBtn6" href="#"></a>
<div class="clear">
</div>
</div>
</div>
</div>
</form>
JQ ...
$(function(){
$("a.hoverBtn").show("fast", function() {
$(this).wrap("<div class=\"hoverBtn\">");
$(this).attr("class", "");
});
//display the hover div
$("div.hoverBtn").show("fast", function() {
//append the background div
$(this).append("<div></div>");
//get link's size
var wid = $(this).children("a").width();
var hei = $(this).children("a").height();
//set div's size
$(this).width(wid);
$(this).height(hei);
$(this).children("div").width(wid);
$(this).children("div").height(hei);
//on link hover
$(this).children("a").hover(function(){
//store initial link colour
if ($(this).attr("rel") == "") {
$(this).attr("rel", $(this).css("color"));
}
//fade in the background
$(this).parent().children("div")
.stop()
.css({"display": "none", "opacity": "1"})
.fadeIn("fast");
//fade the colour
$(this) .stop()
.css({"color": $(this).attr("rel")})
.animate({"color": hoverColour}, 350);
},function(){
//fade out the background
$(this).parent().children("div")
.stop()
.fadeOut("slow");
//fade the colour
//$(this) .stop()
//.animate({"color": $(this).attr("rel")}, 250);
});
});
});
css ...
div.hoverBtn1 {
position: relative;
/*float: left;*/
background: black url(nav_imgs/pbtn2a.png) repeat-x 0 0 scroll;
}
div.hoverBtn1 a {
position: relative;
z-index: 2;
display: block;
width: 223px;
height: 39px;
line-height: 30px;
text-align: center;
font-size: 1.1em;
text-decoration:none;
color: #000;
background: transparent none repeat-x 0 0 scroll;
}
div.hoverBtn1 div
{
display: none;
position: absolute;
z-index: 1;
top: 0px;
background: white url(nav_imgs/pbtn2b.png) repeat-x 0 0 scroll;
}
我尝试过,
$("#nav a").each(function() {
var hreflink = $(this).attr("href");
if (hreflink.toLowerCase()==location.href.toLowerCase()) {
$(this).parent("li").addClass("selected");
}
});
但没有成功,我已经关闭了。
$(function() {
$("a.hoverBtn").click(function(){
$("a.hoverBtn").wrap("<div class=\"active\">");
});
});
这使得单击后可以使 btn 保持翻转状态,但我找不到从中释放它的方法单击该页面和/或另一个按钮后的状态。 我尝试了 .unwrap() 函数,但这不起作用
I'm building a jQuery navigation and i cant seem to figure out how to make the navigation stay in the roll over state on the active page.
HTML...
<body>
<form id="form1" runat="server">
<div>
<div id="pageWrap">
<div id="pageBody">
<a class="hoverBtn" href="#"></a>
<a class="hoverBtn1" href="#"></a>
<a class="hoverBtn2" href="#"></a>
<a class="hoverBtn3" href="#"></a>
<a class="hoverBtn4" href="#"></a>
<a class="hoverBtn5" href="#"></a>
<a class="hoverBtn6" href="#"></a>
<div class="clear">
</div>
</div>
</div>
</div>
</form>
JQ...
$(function(){
$("a.hoverBtn").show("fast", function() {
$(this).wrap("<div class=\"hoverBtn\">");
$(this).attr("class", "");
});
//display the hover div
$("div.hoverBtn").show("fast", function() {
//append the background div
$(this).append("<div></div>");
//get link's size
var wid = $(this).children("a").width();
var hei = $(this).children("a").height();
//set div's size
$(this).width(wid);
$(this).height(hei);
$(this).children("div").width(wid);
$(this).children("div").height(hei);
//on link hover
$(this).children("a").hover(function(){
//store initial link colour
if ($(this).attr("rel") == "") {
$(this).attr("rel", $(this).css("color"));
}
//fade in the background
$(this).parent().children("div")
.stop()
.css({"display": "none", "opacity": "1"})
.fadeIn("fast");
//fade the colour
$(this) .stop()
.css({"color": $(this).attr("rel")})
.animate({"color": hoverColour}, 350);
},function(){
//fade out the background
$(this).parent().children("div")
.stop()
.fadeOut("slow");
//fade the colour
//$(this) .stop()
//.animate({"color": $(this).attr("rel")}, 250);
});
});
});
css...
div.hoverBtn1 {
position: relative;
/*float: left;*/
background: black url(nav_imgs/pbtn2a.png) repeat-x 0 0 scroll;
}
div.hoverBtn1 a {
position: relative;
z-index: 2;
display: block;
width: 223px;
height: 39px;
line-height: 30px;
text-align: center;
font-size: 1.1em;
text-decoration:none;
color: #000;
background: transparent none repeat-x 0 0 scroll;
}
div.hoverBtn1 div
{
display: none;
position: absolute;
z-index: 1;
top: 0px;
background: white url(nav_imgs/pbtn2b.png) repeat-x 0 0 scroll;
}
I have tried
$("#nav a").each(function() {
var hreflink = $(this).attr("href");
if (hreflink.toLowerCase()==location.href.toLowerCase()) {
$(this).parent("li").addClass("selected");
}
});
with no luck the closed i have come was with..
$(function() {
$("a.hoverBtn").click(function(){
$("a.hoverBtn").wrap("<div class=\"active\">");
});
});
This made it possible to leave the btn the the rollover state once clicked but i couldn't find a way to release it from that state once the page and\or another btn was clicked.
i tried the .unwrap() function but that didn't` work ether
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以,我只是在自己的项目中解决了这个问题。基本上我指定了一个 CSS 类来创建翻转效果(在本例中为 .current )。然后,我选择菜单中的所有项目,删除其中包含当前类的项目,然后将 href 属性与文档的地址进行匹配。然后我将课程设置为剩余的比赛。如果您获得多个匹配项,您可能需要进行一些过滤...但应该就是这样。
这里的关键是使用 CSS 创建悬停效果(我看到你在 JS 中做了额外的工作)并确保你的 href 标签被正确定义。
So, I just solved this problem on my own project. Basically I have a CSS class specified that creates the rollover effect (.current in this case). I then select all items in my menu, remove the one with the current class on it and then match the href attribute to the document's address. I then set the class on the leftover match. You may need to do some filtering if you get more than one match...but that should be it.
The key here is to use CSS to create the hover effect (I see you're doing additional work in you JS) and to ensure your href tags are properly defined.