分享
 
 
 

javascript-鼠标在焦点时移动位置(兼容FF)

王朝学院·作者佚名  2009-06-24
窄屏简体版  字體: |||超大  

程序代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>鼠标在焦点时移动位置</title>

<style type="text/css">

/* Reset style */

* { margin:0; padding:0; word-break:break-all; }

body { background:#FFF; color:#878787; font:12px/1.6em Helvetica, Arial, sans-serif; }

h1, h2, h3, h4, h5, h6 { font-size:1em; }

a { color:#0287CA; text-decoration:none; }

a:hover { text-decoration:underline; }

ul, li { list-style:none; }

img { border:none; }

#vlist { width:800px; height:150px; margin:100px 0 0 20px; border-bottom:1px dashed #D6D6D6; background:#F0F0F0; }

#vlist ul li { position:relative; display:inline; float:left; width:180px; height:120px; overflow:hidden; margin:15px 0 0 10px; border:2px solid #B1B1B1; cursor:pointer; }

.po_go { position:absolute; background:#FFF; }

.in {}

.in img { display:block; width:180px; height:120px; }

.out { padding:10px; }

.v_date { padding-bottom:5px; text-align:right; font-size:11px; }

.v_date strong { float:left; font-size:22px; }

</style>

<script type="text/javascript">

/*add --- cssrain */

/*

由于ff 不支持 onmouseenter 和 onmouseleave。

下面这个方法 就是为了解决 ff 下的这个问题。

*/

var xb =

{

evtHash: [],

ieGetUniqueID: function(_elem)

{

if (_elem === window) { return 'theWindow'; }

else if (_elem === document) { return 'theDocument'; }

else { return _elem.uniqueID; }

},

addEvent: function(_elem, _evtName, _fn, _useCapture)

{

if (typeof _elem.addEventListener != 'undefined')

{

if (_evtName == 'mouseenter')

{ _elem.addEventListener('mouseover', xb.mouseEnter(_fn), _useCapture); }

else if (_evtName == 'mouseleave')

{ _elem.addEventListener('mouseout', xb.mouseEnter(_fn), _useCapture); }

else

{ _elem.addEventListener(_evtName, _fn, _useCapture); }

}

else if (typeof _elem.attachEvent != 'undefined')

{

var key = '{FNKEY::obj_' + xb.ieGetUniqueID(_elem) + '::evt_' + _evtName + '::fn_' + _fn + '}';

var f = xb.evtHash[key];

if (typeof f != 'undefined')

{ return; }

f = function()

{

_fn.call(_elem);

};

xb.evtHash[key] = f;

_elem.attachEvent('on' + _evtName, f);

// attach unload event to the window to clean up possibly IE memory leaks

window.attachEvent('onunload', function()

{

_elem.detachEvent('on' + _evtName, f);

});

key = null;

//f = null; /* DON'T null this out, or we won't be able to detach it */

}

else

{ _elem['on' + _evtName] = _fn; }

},

removeEvent: function(_elem, _evtName, _fn, _useCapture)

{

if (typeof _elem.removeEventListener != 'undefined')

{ _elem.removeEventListener(_evtName, _fn, _useCapture); }

else if (typeof _elem.detachEvent != 'undefined')

{

var key = '{FNKEY::obj_' + xb.ieGetUniqueID(_elem) + '::evt' + _evtName + '::fn_' + _fn + '}';

var f = xb.evtHash[key];

if (typeof f != 'undefined')

{

_elem.detachEvent('on' + _evtName, f);

delete xb.evtHash[key];

}

key = null;

//f = null; /* DON'T null this out, or we won't be able to detach it */

}

},

mouseEnter: function(_pFn)

{

return function(_evt)

{

var relTarget = _evt.relatedTarget;

if (this == relTarget || xb.isAChildOf(this, relTarget))

{ return; }

_pFn.call(this, _evt);

}

},

isAChildOf: function(_parent, _child)

{

if (_parent == _child) { return false };

while (_child && _child != _parent)

{ _child = _child.parentNode; }

return _child == _parent;

}

};

/* end */ http://bizhi.knowsky.com/

function addLoadEvent(func){

var oldonload = window.onload;

if (typeof window.onload != 'function') {

window.onload = func;

} else {

window.onload = function(){

oldonload();

func();

}

}

}

function getElementsByClassName(oElm, strTagName, strClassName) {

var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);

var arrReturnElements = new Array();

strClassName = strClassName.replace(/-/g, "\-");

var oregExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");

var oElement;

for(var i=0; i<arrElements.length; i++){

oElement = arrElements[i];

if(oRegExp.test(oElement.className)){

arrReturnElements.push(oElement);

}

}

return (arrReturnElements)

}

function moveElement(elementID,final_x,final_y,interval) {

if (!document.getElementById) return false;

if (!document.getElementById(elementID)) return false;

var elem = document.getElementById(elementID);

if (elem.movement) {

clearTimeout(elem.movement);

}

if (!elem.style.left) {

elem.style.left = "0px";

}

if (!elem.style.top) {

elem.style.top = "0px";

}

var xpos = parseInt(elem.style.left);

var ypos = parseInt(elem.style.top);

if (xpos == final_x && ypos == final_y) {

return true;

}

if (xpos < final_x) {

var dist = Math.ceil((final_x - xpos)/10);

xpos = xpos + dist;

}

if (xpos > final_x) {

var dist = Math.ceil((xpos - final_x)/10);

xpos = xpos - dist;

}

if (ypos < final_y) {

var dist = Math.ceil((final_y - ypos)/10);

ypos = ypos + dist;

}

if (ypos > final_y) {

var dist = Math.ceil((ypos - final_y)/10);

ypos = ypos - dist;

}

elem.style.left = xpos + "px";

elem.style.top = ypos + "px";

var repeat = "moveElement('"+elementID+"',"+final_x+","+final_y+","+interval+")";

elem.movement = setTimeout(repeat,interval);

}

function msgTip() {

var elem = document.getElementById('vlist');

var poGo = getElementsByClassName(elem, 'div', 'po_go');

for (var i=0; i<poGo.length; i++) {

var j = 'vlist' + '_' + i;

poGo[i].setAttribute('id',j);

xb.addEvent(poGo[0], 'mouseenter', function(){

moveElement('vlist_0',0,0,10)

}, false);

xb.addEvent(poGo[0], 'mouseleave', function(){

moveElement('vlist_0',0,-120,10)

}, false);

xb.addEvent(poGo[1], 'mouseenter', function(){

moveElement('vlist_1',0,0,10)

}, false);

xb.addEvent(poGo[1], 'mouseleave', function(){

moveElement('vlist_1',0,-120,10)

}, false);

xb.addEvent(poGo[2], 'mouseenter', function(){

moveElement('vlist_2',0,0,10)

}, false);

xb.addEvent(poGo[2], 'mouseleave', function(){

moveElement('vlist_2',0,-120,10)

}, false);

}

}

addLoadEvent(msgTip);

</script>

</head>

<body>

<div id="vlist">

<ul>

<li>

<div class="po_go" style="top:-120px;" >

<div class="in" ><a href="#"><img src="k2008715211916.jpg" alt="" /></a></div>

<div class="out">

<p class="v_date"><strong>1</strong>09:48:56</p>

<p>描述一段浪漫唯美的苦恋:深谙泳技的渔村少女,某日在偶然间,救起溺水的富家公子,后来富家公子被救活而离开</p>

</div>

</div>

</li>

<li>

<div class="po_go" style="top:-120px;">

<div class="in"><a href="#"><img src="r2008715211938.jpg" alt="" /></a></div>

<div class="out">

<p class="v_date"><strong>2</strong>09:48:56</p>

<p>描述一段浪漫唯美的苦恋:深谙泳技的渔村少女,某日在偶然间,救起溺水的富家公子,后来富家公子被救活而离开</p>

</div>

</div>

</li>

<li>

<div class="po_go" style="top:-120px;">

<div class="in"><a href="#"><img src="12008715211953.jpg" alt="" /></a></div>

<div class="out">

<p class="v_date"><strong>3</strong>09:48:56</p>

<p>描述一段浪漫唯美的苦恋:深谙泳技的渔村少女,某日在偶然间,救起溺水的富家公子,后来富家公子被救活而离开</p>

</div>

</div>

</li>

</ul>

</div>

</body>

</html>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有