CSS实现onMouseOver、onMouseOut效果和层套菜单

当前位置 : 首页 > 网页制作 > CSS > CSS实现onMouseOver、onMouseOut效果和层套菜单

CSS实现onMouseOver、onMouseOut效果和层套菜单

来源: 作者: 时间:2016-01-20 09:22
在IE中 a 标签的。而实际上 WEB 标准里面, 应该对所有元素都支持。如果使用 Mozilla 或者 Opera, 那么可以看到不需要这个 htc 都可以正常运行的。 因为 htc 是 IE 特有的,别的浏览器不能理
在IE中 <a> 标签的。而实际上 WEB 标准里面, 应该对所有元素都支持。
如果使用 Mozilla 或者 Opera, 那么可以看到不需要这个 htc 都可以正常运行的。 因为 htc 是 IE 特有的,别的不能理解,会忽略掉,不影响结果的显示。
 
以下这段 htc 是一个老外写的针对 IE 的 hover 行为的一个修正。
有了这个代码就方便多了,而且最可贵的是,以上的这个页面例子是可以兼容 IE, Mozilla 和 Opera 的。
 
原先在里都是使用onMouserOver等事件,实现鼠标焦点控制的,从来没有想到过用简化原先繁琐的工作,直到偶然间发现了whatever:hover,才认识到CSS原来可以做很多工作,甚至可是实现繁杂的menubar。
 
实现的核心部分是csshover.htc文件,它其实就是JScript代码,负责处理所有的CSS格式,根据定制的CSS格式,生成onMouseOver和onMouseOut事件,实现了hover的化。使用时只需制定div:hover{ background:#f8f8f8; },就实现了鼠标移动到div图层上时,改变背景色的事件。
 
csshover.htc
 
None.gif<attach event="ondocumentready" handler="parseStylesheets" />
ExpandedBlockStart.gif<script language="JScript">
ExpandedSubBlockStart.gif/**
InBlock.gif* Pseu - V1.30.050121 - hover & active
InBlock.gif* ---------------------------------------------
InBlock.gif* Peterned - http://www.xs4all.nl/~peterned/
InBlock.gif* (c) 2005 - Peter Nederlof
InBlock.gif*
InBlock.gif* Credits - Arnoud Berendsen
InBlock.gif* - Martin Reurings
InBlock.gif* - Robert Hanson
InBlock.gif*
InBlock.gif* howt body { behavior:url("csshover.htc"); }
InBlock.gif* ---------------------------------------------
ExpandedSubBlockEnd.gif*/
InBlock.gif
ExpandedSubBlockStart.gifvar currentSheet, doc = window.document, activators = {
ExpandedSubBlockStart.gifonhover:{on:'onmouver', off:'onmouseout'},
ExpandedSubBlockStart.gifonactive:{on:'onmousedown', off:'onmouseup'}
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedSubBlockStart.giffunction parseStylesheets() {
InBlock.gifvar sheets = doc.styleSheets, l = sheets.length;
InBlock.giffor(var i=0; i<l; i++)
InBlock.gif parseStylesheet(sheets[i]);
ExpandedSubBlockEnd.gif}
ExpandedSubBlockStart.giffunction parseStylesheet(sheet) {
ExpandedSubBlockStart.gif if(sheet.imports) {
ExpandedSubBlockStart.gif try {
InBlock.gif var imports = sheet.imports, l = imports.length;
InBlock.gif for(var i=0; i<l; i++) parseStylesheet(sheet.imports[i]);
ExpandedSubBlockStart.gif } catch(securityException){}
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gif try {
InBlock.gif var rules = (currentSheet = sheet).rules, l = rules.length;
InBlock.gif for(var j=0; j<l; j++) parseCSSRule(rules[j]);
ExpandedSubBlockStart.gif } catch(securityException){}
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedSubBlockStart.giffunction parseCSSRule(rule) {
InBlock.gif var select = rule.selectorText, style = rule.style.cssText;
InBlock.gif if(!(/(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active)/i).test(select) || !style) return;
InBlock.gif 
InBlock.gif var pseudo = select.replace(/[^:]+:([a-z-]+).*/i, 'on$1');
InBlock.gif var newSelect = select.replace(/(\.([a-z0-9_-]+):[a-z]+)|(:[a-z]+)/gi, '.$2' + pseudo);
InBlock.gif var className = (/\.([a-z0-9_-]*on(hover|active))/i).exec(newSelect)[1];
InBlock.gif var affected = select.replace(/:hover.*$/, '');
InBlock.gif var elements = getElementsBySelect(affected);
InBlock.gif
InBlock.gif currentSheet.addRule(newSelect, style);
InBlock.gif for(var i=0; i<elements.length; i++)
InBlock.gif new HoverElement(elements[i], className, activators[pseudo]);
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedSubBlockStart.giffunction HoverElement(node, className, events) {
ExpandedSubBlockStart.gifif(!node.hovers) node.hovers = {};
InBlock.gifif(node.hovers[className]) return;
InBlock.gifnode.hovers[className] = true;
InBlock.gifnode.attachEvent(events.on,
ExpandedSubBlockStart.gif function() { node.className += ' ' + className; });
InBlock.gifnode.attachEvent(events.off,
ExpandedSubBlockStart.gif function() { node.className =
ExpandedSubBlockEnd.gif node.className.replace(new RegExp('\\s+'+className, 'g'),''); });
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedSubBlockStart.giffunction getElementsBySelect(rule) {
InBlock.gifvar parts, nodes = [doc];
InBlock.gifparts = rule.split(' ');
ExpandedSubBlockStart.giffor(var i=0; i<parts.length; i++) {
InBlock.gif nodes = getSelectedNodes(parts[i], nodes);
ExpandedSubBlockEnd.gif} return nodes;
ExpandedSubBlockEnd.gif}
ExpandedSubBlockStart.giffunction getSelectedNodes(select, elements) {
InBlock.gif var result, node, nodes = [];
InBlock.gif var classname = (/\.([a-z0-9_-]+)/i).exec(select);
InBlock.gif var identify = (/\#([a-z0-9_-]+)/i).exec(select);
InBlock.gif var tagName = select.replace(/(\.|\#|\:)[a-z0-9_-]+/i, '');
ExpandedSubBlockStart.gif for(var i=0; i<elements.length; i++) {
InBlock.gif result = tagName? elements[i].all.tags(tagName):elements[i].all;
ExpandedSubBlockStart.gif for(var j=0; j<result.length; j++) {
InBlock.gif node = result[j];
InBlock.gif if((identify && node.id != identify[1]) || (classname && !(new RegExp('\\b' +
InBlock.gif classname[1] + '\\b').exec(node.className)))) continue;
InBlock.gif nodes[nodes.length] = node;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif } return nodes;
ExpandedBlockEnd.gif}
None.gif</script>
 
Test.htm
 
None.gif<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
None.gif<title> New Document </title>
None.gif<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
None.gif<meta name="Generator" content="EditPlus">
None.gif<meta name="Author" content="Roger Chen">
None.gif<meta name="keywords" content="">
None.gif<meta name="description" content="">
ExpandedBlockStart.gif<style>
ExpandedSubBlockStart.gifbody {behavior: url(csshover.htc);}
ExpandedSubBlockStart.giftable#tbl tr:hover {background: #000000; color: #ffffff;}
ExpandedSubBlockStart.gifdiv.test:hover {background: #000000; color: #ffffff;}
ExpandedSubBlockStart.gifa.test:hover {background: #000000; color: #ffffff;}
ExpandedSubBlockStart.gifinput.test2:hover {background: #000000; border: 1px dotted black;}
None.gif</style>
None.gif
None.gif<div class="test">haha</div><a href="" class="test">5456456</a>
None.gif<p>表格1:</p>
None.gif<table id="tbl" border="1" width="100%">
None.gif<tr>
None.gif<td>OK</td>
None.gif<td>yes</td>
None.gif<td>no</td>
None.gif</tr>
None.gif<tr>
None.gif<td>haha</td>
None.gif<td>fsdfsdf</td>
None.gif<td>测试</td>
None.gif</tr>
None.gif<tr>
None.gif<td>木野狐</td>
None.gif<td>html/css</td>
None.gif<td>javascript</td>
None.gif</tr>
None.gif</table>
None.gif<p>表格2:</p>
None.gif<table id="tbl" border="1" width="100%">
None.gif<tr>
None.gif<td>OK</td>
None.gif<td>yes</td>
None.gif<td>no</td>
None.gif</tr>
None.gif<tr>
None.gif<td>haha</td>
None.gif<td>fsdfsdf</td>
None.gif<td>测试</td>
None.gif</tr>
None.gif<tr>
None.gif<td>木野狐</td>
None.gif<td>html/css</td>
None.gif<td>javascript</td>
None.gif</tr>
None.gif</table>
None.gif<input class="test2">
 
menubar.htm 
 
None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
None.gif<head>
None.gif<title> whatever:hover cssmenu </title>
None.gif<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
None.gif
ExpandedBlockStart.gif<style type="text/css">
InBlock.gif
ExpandedSubBlockStart.gifbody {
InBlock.gif behavior:url("csshover.htc");
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedSubBlockStart.gif* {
InBlock.gif font-family:arial,tahoma,verdana,helvetica;
InBlock.gif font-size:12px;
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedSubBlockStart.gif/* the menu */
InBlock.gif
ExpandedSubBlockStart.giful,li,a {
InBlock.gif display:block;
InBlock.gif margin:0;
InBlock.gif padding:0;
InBlock.gif border:0;
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedSubBlockStart.giful {
InBlock.gif width:150px;
InBlock.gif border:1px solid #9d9da1;
InBlock.gif background:white;
InBlock.gif list-style:none;
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedSubBlockStart.gifli {
InBlock.gif position:relative;
InBlock.gif padding:1px;
InBlock.gif padding-left:26px;
InBlock.gif background:url("images/item_moz.gif") no-repeat;
InBlock.gif z-index:9;
ExpandedSubBlockEnd.gif}
ExpandedSubBlockStart.gif li.folder { background:url("images/item_folder.gif") no-repeat; }
ExpandedSubBlockStart.gif li.folder ul {
InBlock.gif position:absolute;
ExpandedSubBlockStart.gif left:120px; /* IE */
InBlock.gif top:5px;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gif li.folder>ul { left:140px; } /* others */
InBlock.gif
ExpandedSubBlockStart.gifa {
InBlock.gif padding:2px;
InBlock.gif border:1px solid white;
InBlock.gif text-decoration:none;
InBlock.gif color:gray;
InBlock.gif font-weight:bold;
ExpandedSubBlockStart.gif width:100%; /* IE */
ExpandedSubBlockEnd.gif}
ExpandedSubBlockStart.gif li>a { width:auto; } /* others */
InBlock.gif
ExpandedSubBlockStart.gifli a.submenu {
InBlock.gif background:url("images/sub.gif") right no-repeat;
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedSubBlockStart.gif/* regular hovers */
InBlock.gif
ExpandedSubBlockStart.gifa:hover {
InBlock.gif border-color:gray;
InBlock.gif background-color:#bbb7c7;
InBlock.gif color:black;
ExpandedSubBlockEnd.gif}
ExpandedSubBlockStart.gif li.folder a:hover {
InBlock.gif background-color:#bbb7c7;
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gif/* hovers with specificity */
InBlock.gif
ExpandedSubBlockStart.gifli.folder:hover { z-index:10; }
InBlock.gif 
ExpandedSubBlockStart.giful ul, li:hover ul ul {
InBlock.gif display:none;
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedSubBlockStart.gifli:hover ul, li:hover li:hover ul {
InBlock.gif display:block;
ExpandedSubBlockEnd.gif}
ExpandedBlockEnd.gif
None.gif</style>
None.gif
None.gif</head>
None.gif
None.gif<body>
None.gif
None.gif<ul id="menu">
None.gif <li><a href="#"> lorem </a></li>
None.gif <li class="folder">
None.gif <a href="#" class="submenu"> adipiscing </a>
None.gif <ul>
None.gif <li><a href="#"> dolor </a></li>
None.gif <li class="folder">
None.gif <a href="#" class="submenu"> consectetuer</a>
None.gif <ul>
None.gif <li><a href="#"> elit </a></li>
None.gif <li><a href="#"> ipsum </a></li>
None.gif <li><a href="#"> Donec </a></li>
None.gif </ul>
None.gif </li>
None.gif <li><a href="#"> vestibulum </a></li>
None.gif </ul>
None.gif </li>
None.gif <li class="folder">
None.gif <a href="#" class="submenu"> consectetuer</a>
None.gif <ul>
None.gif <li><a href="#"> elit </a></li>
None.gif <li><a href="#"> ipsum </a></li>
None.gif <li><a href="#"> Donec </a></li>
None.gif </ul>
None.gif </li>
None.gif <li><a href="#"> sit amet </a></li>
None.gif</ul>
None.gif</body>
None.gif</html>
 
Tag:
网友评论

<