用jQuery实现这种风格的下拉菜单
我在浏览网页时发现了此网站。我注意到导航栏下拉菜单的工作原理,并且我想在我的网站上实现类似的功能。
查看页面的源代码后,我发现这些下拉区域包含在 fOut
类的 div
中。
事实证明,这是通过 MooTools 完成的。 这是脚本本身(在原始页面中引用后Mootools 脚本本身):
window.addEvent('domready', function() {
$("primaryNav").getChildren("li").addEvents({
"mouseenter": function(){
$(this).addClass("hover").getChildren("a").addClass("hover");
},
"mouseleave": function(){
$(this).removeClass("hover").getChildren("a").removeClass("hover");
}
});
$$(".fOut").each(function(el,i){
var ifr = $(document.createElement("iframe"));
ifr.className = "ieBgIframe";
ifr.frameBorder = "0";
ifr.src="about:blank";
ifr.injectInside(el);
var p = el.getParent();
p.addClass("hover");
var h = el.getSize().size.y;
p.removeClass("hover");
ifr.height=h;
});
$$(".olderVersionsToggle").addEvents({
"click": function(e){
var event = new Event(e);
event.stop();
var p = $(this).getParent().getNext();
if(p.hasClass("open")){
p.removeClass("open");
$(this).setText("Show previous versions...");
}
else{
p.addClass("open");
$(this).setText("Hide previous versions...");
}
return false;
}
});
});
我的问题
我对这段代码有两个问题。
- 它是如何工作的?我不太明白它在使用
iframe
等做什么。 - 如何在 jQuery 中实现这一点?
I was browsing the web and found this site. I noticed how the nav bar drop-down works, and I want to implement something similar on my website.
Afer going into the source code of the page, I found that those drop-down areas are contained in div
s with class fOut
.
It turns out that this is being done with MooTools. Here is the script itself (referenced in the original page after the Mootools script itself):
window.addEvent('domready', function() {
$("primaryNav").getChildren("li").addEvents({
"mouseenter": function(){
$(this).addClass("hover").getChildren("a").addClass("hover");
},
"mouseleave": function(){
$(this).removeClass("hover").getChildren("a").removeClass("hover");
}
});
$(".fOut").each(function(el,i){
var ifr = $(document.createElement("iframe"));
ifr.className = "ieBgIframe";
ifr.frameBorder = "0";
ifr.src="about:blank";
ifr.injectInside(el);
var p = el.getParent();
p.addClass("hover");
var h = el.getSize().size.y;
p.removeClass("hover");
ifr.height=h;
});
$(".olderVersionsToggle").addEvents({
"click": function(e){
var event = new Event(e);
event.stop();
var p = $(this).getParent().getNext();
if(p.hasClass("open")){
p.removeClass("open");
$(this).setText("Show previous versions...");
}
else{
p.addClass("open");
$(this).setText("Hide previous versions...");
}
return false;
}
});
});
My Question(s)
I have two questions about this code.
- How does it work? I don't quite understand what it's doing, with the
iframe
s and all. - How can this be implemented in jQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它的工作方式就像任何其他飞越菜单一样,子菜单是静态定位的,当您将鼠标悬停在菜单项上时,它们会添加悬停状态(类)。 (从 DOM 来看)他们似乎正在使用 iframe 来破解一些 IE 问题。打开控制台会话并观察元素以了解我的意思,iframe 不会随时间变化,它们只是空着。
至于在 jQuery 中实现它,我将从 dom 布局开始(确保所有内容都在同一区域中排列,并用精心设计的内容填充子菜单)。然后只需绑定 mouseenter 和 mouseleave 事件,例如:
他们使用 iframe 在元素之间设置一致的高度(看起来),您可以通过简单地将 div 高度设置为静态量或在渲染每个子之后来完成此操作菜单只需找到最高的一个(根据您的需要使用innerHeight或outerHeight)并设置其余的以匹配它的高度。
It works just like any other fly over menu, the submenu is statically positioned and they add the hover state (class) when you mouse over a menu item. It looks (from the DOM) like they are using the iframe to hack some IE issues. Open up a console session and watch the elements to see what I mean, the iframes do not change over time, they just sit there empty.
As for implementing it in jQuery I would start with your dom layout (make sure everything lines up in the same area and fill in your sub-menus with well designed content). Then just bind up the mouseenter and mouseleave events like:
They are using iframe to set a consistent height across the elements (it seems), you could do this by simply setting the div height to be either a static amount or after rendering each sub-menu just find the tallest one (using innerHeight or outerHeight depending on your need) and set the rest to match it in height.