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

​在IE中onmouseover, onmouseout对这两个样式的支持不完全。默认只支持 标签的。而实际上 WEB 标准里面, 应该对所有元素都支持。
如果使用 Mozilla 或者 Opera, 那么可以看到不需要这个 htc 都可以正常运行的。 因为 htc 是 IE 特有的,别的浏览器不能理解,会忽略掉,不影响结果的显示。

以下这段 htc 是一个老外写的针对 IE 的 hover 行为的一个修正。
有了这个代码就方便多了,而且最可贵的是,以上的这个页面例子是可以兼容 IE, Mozilla 和 Opera 的。

原先在html里都是使用onMouserOver等事件,实现鼠标焦点控制的,从来没有想到过用CSS简化原先繁琐的工作,直到偶然间发现了whatever:hover,才认识到CSS原来可以做很多工作,甚至可是实现繁杂的menubar。

实现的核心部分是csshover.htc文件,它其实就是JScript代码,负责处理所有的CSS格式,根据定制的CSS格式,生成onMouseOver和onMouseOut事件,实现了hover的组件化。使用时只需制定div:hover{ background:#f8f8f8; },就实现了鼠标移动到div图层上时,改变背景色的事件。

csshover.htc

<attach event="ondocumentready" handler="parseStylesheets" />
<script language="JScript">
/**
* Pseudos - V1.30.050121 - hover & active
* ---------------------------------------------
* Peterned - http://www.xs4all.nl/~peterned/
* (c) 2005 - Peter Nederlof
*
* Credits - Arnoud Berendsen
* - Martin Reurings
* - Robert Hanson
*
* howt body { behavior:url("csshover.htc"); }
* ---------------------------------------------
*/


var currentSheet, doc = window.document, activators = {
onhover:
{on:'onmouseover', off:'onmouseout'},
onactive:
{on:'onmousedown', off:'onmouseup'}
}


function parseStylesheets() {
var sheets = doc.styleSheets, l = sheets.length;
for(var i=0; i<l; i++)
 parseStylesheet(sheets[i]);
}

function parseStylesheet(sheet) {
 
if(sheet.imports) {
 
try {
 
var imports = sheet.imports, l = imports.length;
 
for(var i=0; i<l; i++) parseStylesheet(sheet.imports[i]);
 }
 catch(securityException){}
 }


 
try {
 
var rules = (currentSheet = sheet).rules, l = rules.length;
 
for(var j=0; j<l; j++) parseCSSRule(rules[j]);
 }
 catch(securityException){}
}


function parseCSSRule(rule) {
 
var select = rule.selectorText, style = rule.style.cssText;
 
if(!(/(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active)/i).test(select) || !style) return;
 
 
var pseudo = select.replace(/[^:]+:([a-z-]+).*/i, 'on$1');
 
var newSelect = select.replace(/(\.([a-z0-9_-]+):[a-z]+)|(:[a-z]+)/gi, '.$2+ pseudo);
 
var className = (/\.([a-z0-9_-]*on(hover|active))/i).exec(newSelect)[1];
 
var affected = select.replace(/:hover.*$/, '');
 
var elements = getElementsBySelect(affected);

 currentSheet.addRule(newSelect, style);
 
for(var i=0; i<elements.length; i++)
 
new HoverElement(elements[i], className, activators[pseudo]);
}


function HoverElement(node, className, events) {
if(!node.hovers) node.hovers = {};
if(node.hovers[className]) return;
node.hovers[className] 
= true;
node.attachEvent(events.on,
 
function() { node.className += ' ' + className; });
node.attachEvent(events.off,
 
function() { node.className =
 node.className.replace(
new RegExp('\\s+'+className, 'g'),''); }
);
}


function getElementsBySelect(rule) {
var parts, nodes = [doc];
parts 
= rule.split(' ');
for(var i=0; i<parts.length; i++{
 nodes 
= getSelectedNodes(parts[i], nodes);
}
 return nodes;
}

function getSelectedNodes(select, elements) {
 
var result, node, nodes = [];
 
var classname = (/\.([a-z0-9_-]+)/i).exec(select);
 
var identify = (/\#([a-z0-9_-]+)/i).exec(select);
 
var tagName = select.replace(/(\.|\#|\:)[a-z0-9_-]+/i, '');
 
for(var i=0; i<elements.length; i++{
 result 
= tagName? elements[i].all.tags(tagName):elements[i].all;
 
for(var j=0; j<result.length; j++{
 node 
= result[j];
 
if((identify && node.id != identify[1]) || (classname && !(new RegExp('\\b' +
 classname[
1+ '\\b').exec(node.className)))) continue;
 nodes[nodes.length] 
= node;
 }

 }
 return nodes;
}

script>


Test.htm

DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<title> New Document title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<meta name="Generator" content="EditPlus">
<meta name="Author" content="Roger Chen">
<meta name="keywords" content="">
<meta name="description" content="">
<style>
body 
{behavior: url(csshover.htc);}
table#tbl tr:hover 
{background: #000000; color: #ffffff;}
div.test:hover 
{background: #000000; color: #ffffff;}
a.test:hover 
{background: #000000; color: #ffffff;}
input.test2:hover 
{background: #000000; border: 1px dotted black;}
style>

<div class="test">hahadiv><href="" class="test">5456456a>
<p>表格1:p>
<table id="tbl" border="1" width="100%">
<tr>
<td>OKtd>
<td>yestd>
<td>notd>
tr>
<tr>
<td>hahatd>
<td>fsdfsdftd>
<td>测试td>
tr>
<tr>
<td>木野狐td>
<td>html/csstd>
<td>javascripttd>
tr>
table>
<p>表格2:p>
<table id="tbl" border="1" width="100%">
<tr>
<td>OKtd>
<td>yestd>
<td>notd>
tr>
<tr>
<td>hahatd>
<td>fsdfsdftd>
<td>测试td>
tr>
<tr>
<td>木野狐td>
<td>html/csstd>
<td>javascripttd>
tr>
table>
<input class="test2">


menubar.htm 

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
<head>
<title> whatever:hover cssmenu title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<style type="text/css">

body 
{
 behavior
:url("csshover.htc");
}


{
 font-family
:arial,tahoma,verdana,helvetica;
 font-size
:12px;
}


/* the menu */

ul,li,a 
{
 display
:block;
 margin
:0;
 padding
:0;
 border
:0;
}


ul 
{
 width
:150px;
 border
:1px solid #9d9da1;
 background
:white;
 list-style
:none;
}


li 
{
 position
:relative;
 padding
:1px;
 padding-left
:26px;
 background
:url("images/item_moz.gif") no-repeat;
 z-index
:9;
}

 li.folder 
{ background:url("images/item_folder.gif") no-repeat; }
 li.folder ul 
{
 position
:absolute;
 left
:120px; /* IE */
 top
:5px;
 
}

 li.folder>ul 
{ left:140px; } /* others */

{
 padding
:2px;
 border
:1px solid white;
 text-decoration
:none;
 color
:gray;
 font-weight
:bold;
 width
:100%; /* IE */
}

 li>a 
{ width:auto; } /* others */

li a.submenu 
{
 background
:url("images/sub.gif") right no-repeat;
}


/* regular hovers */

a:hover 
{
 border-color
:gray;
 background-color
:#bbb7c7;
 color
:black;
}

 li.folder a:hover 
{
 background-color
:#bbb7c7;
 
}


/* hovers with specificity */

li.folder:hover 
{ z-index:10; }
 
ul ul, li:hover ul ul 
{
 display
:none;
}


li:hover ul, li:hover li:hover ul 
{
 display
:block;
}


style>

head>

<body>

<ul id="menu">
 
<li><href="#"> lorem a>li>
 
<li class="folder">
 
<href="#" class="submenu"> adipiscing a>
 
<ul>
 
<li><href="#"> dolor a>li>
 
<li class="folder">
 
<href="#" class="submenu"> consectetuera>
 
<ul>
 
<li><href="#"> elit a>li>
 
<li><href="#"> ipsum a>li>
 
<li><href="#"> Donec a>li>
 
ul>
 
li>
 
<li><href="#"> vestibulum a>li>
 
ul>
 
li>
 
<li class="folder">
 
<href="#" class="submenu"> consectetuera>
 
<ul>
 
<li><href="#"> elit a>li>
 
<li><href="#"> ipsum a>li>
 
<li><href="#"> Donec a>li>
 
ul>
 
li>
 
<li><href="#"> sit amet a>li>
ul>
body>
html>


你可能感兴趣的:(JavaScript)