简体   繁体   English

css和javascript onmouseover类型的效果

[英]css and javascript onmouseover type effect

When I visit some sites and take mouse pointer over some menu item, another sub menu items comes up in another panel adjacent to main menu item. 当我访问某些网站并将鼠标指针放在某个菜单项上时,另一个子菜单项出现在与主菜单项相邻的另一个面板中。 Thus giving an effect like onmouseover. 从而产生像onmouseover这样的效果。 But when I see the source code (like View source option in IE) there is no onmouseover / onmouseout event defined in the menu item list element. 但是当我看到源代码(如IE中的View source选项)时,菜单项列表元素中没有定义onmouseover / onmouseout事件。

For example, in the website http://www.seoconsultants.com/ - take mouse pointer over SEO Search on the left panel or in the website http://www.znetindia.com take mouse pointer Email option on top menubar 例如,在网站http://www.seoconsultants.com/中 - 在左侧面板或网站http://www.znetindia.com上将鼠标指针放在SEO搜索上,在顶部菜单栏上取鼠标指针电子邮件选项

How to get such effect using css and javascript. 如何使用css和javascript获得这样的效果。

Without JS, just with CSS. 没有JS,只有CSS。 Take a look at the source code: http://www.seoconsultants.com/css/seo.css 请查看源代码: http//www.seoconsultants.com/css/seo.css

/* Begin CSS Popout Menus at Left */
#menuleft ul li{position:relative;}
#menuleft li ul{position:absolute;left:180px;top:0;display:none;padding:0;}

div#menuleft ul li:hover ul{display:block;}

Basically you say: "When the mouse is hovering over a parent list element, the child list should be visible." 基本上你会说:“当鼠标悬停在父列表元素上时,子列表应该是可见的。”

This is done through the use of the :hover CSS attribute attached to the CSS rule of the parent node. 这是通过使用附加到父节点的CSS规则的:hover CSS属性来完成的。

Consider the following HTML code: 请考虑以下HTML代码:

<div class="parent">
 <span class="label">Always on!</span>
 <span class="hiddenLabel">Show on Mouse</span>
</div>

You achieve the effect you mention with the following css code: 使用以下css代码实现您提到的效果:

.parent .hiddenLabel {
    visibility: hidden;
}

.parent:hover .hiddenLabel {
    visibility: visible;
}

This basically tells the browser that when a mouse hover event occurs on the "parent" node, the nodes with the CSS class of "hiddenLabel" will appear to the user and disappear when the mouse moves off the node. 这基本上告诉浏览器当“父”节点上发生鼠标悬停事件时,CSS类为“hiddenLabel”的节点将向用户显示,并在鼠标离开节点时消失。

This is the best practice for achieving this effect because of the load time and processing required for the javascript to start running on the page is longer than CSS being loaded. 这是实现此效果的最佳实践,因为javascript开始在页面上运行所需的加载时间和处理比加载的CSS更长。

Here is a great write-up on pseudo selectors and what each of them do: http://css-tricks.com/pseudo-class-selectors/ 这是关于伪选择器的一个很好的写作,以及它们各自的作用: http//css-tricks.com/pseudo-class-selectors/

Take a look at jQuery and some plugins. 看看jQuery和一些插件。 See this site for a list of jQuery dropdown plugins. 有关jQuery下拉插件的列表,请访问此站点。 http://www.1stwebdesigner.com/resources/38-jquery-and-css-drop-down-multi-level-menu-solutions/ http://www.1stwebdesigner.com/resources/38-jquery-and-css-drop-down-multi-level-menu-solutions/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM