<!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>
<script>
var sUserAgent = navigator.userAgent;
var isIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1;
var EventUtil = new Object();//事件对像
EventUtil.getEvent = function()//获取事件对像
{
if(window.event)
{
return this.formatEvent(window.event);
}
else
{
return EventUtil.getEvent.caller.arguments[0];
}
}
EventUtil.addEventHandler = function(oTarget,sEventType,fnHandler)//添加事件处理函数
{
if(oTarget.addEventListener)
{
//Firefor浏览器兼容
oTarget.addEventListener(sEventType,fnHandler,false);
}
else if(oTarget.attachEvent)
{
//IE浏览器兼容
oTarget.attachEvent("on" + sEventType,fnHandler);
}
else
{
//其它浏览器兼容
oTarget["on" + sEventType] = fnHandler;
}
}
EventUtil.removeEventHandler = function(oTarget,sEventType,fnHandler)//删除事件处理函数
{
if(oTarget.removeEventListener)
{
//Firefor浏览器兼容
oTarget.removeEventListener(sEventType,fnHandler,false);
}
else if(oTarget.detachEvent)
{
//IE浏览器兼容
oTarget.detachEvent("on" + sEventType,fnHandler);
}
else
{
//其它浏览器兼容
oTarget["on" + sEventType] = null;
}
}
EventUtil.formatEvent = function(oEvent)//格式化事件
{
if(isIE)
{
oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
oEvent.isChar = (oEvent.charCode > 0);//表示按下的按键是否与字符有关
oEvent.pageX = oEvent.clientX + document.body.scrollLeft;//鼠标相对于页面的X坐标
oEvent.pageY = oEvent.clinetY + document.body.scrollTop;//鼠标相对于页面的Y坐标
oEvent.preventDefault = function()//阻止事件的默认行为
{
this.returnValue = false;
}
if(oEvent.type == "mouseout")
{
oEvent.relatedTarget = oEvent.toElement;//鼠标事件中鼠标正在进入的元素
}
else if(oEvent.type == "mouseover")
{
oEvent.relatedTarget = oEvent.fromElement;//鼠标事件中鼠标离开的元素
}
oEvent.stopPropagation = function()
{
this.cancelBubble = true;//阻止冒泡事件发生
}
oEvent.target = oEvent.srcElement;
}
return oEvent;
}
var dragObj;
function blockMouseDown(event, obj)//当滑块按下时候
{
var oEvent = EventUtil.getEvent();
dragObj = obj;
EventUtil.addEventHandler(obj,"mousemove",blockMouseMove);
EventUtil.addEventHandler(obj,"mouseup",blockMouseUp);
obj.setCapture();
}
function blockMouseMove()//当滑块在容器上移动时候
{
var oEvent = EventUtil.getEvent();
var width = oEvent.clientX - music_percent_left - 8;
if(width > 101)
{
width = 101;
}
else if(width < 0)
{
width = 0;
}
$(music_id_prefix + "block").style.marginLeft = width;
}
function blockMouseUp()//当滑块在容器按键松开的时候
{
var oEvent = EventUtil.getEvent();
var obj = dragObj;
EventUtil.removeEventHandler(obj,"mousemove",blockMouseMove);
EventUtil.removeEventHandler(obj,"mouseup",blockMouseUp);
}
function $(obj)
{
return document.getElementById(obj);
}
var music_id_prefix = "music";
var music_img_path = "images/";
var music_player;//播放器object
var music_timer;//定时器
var music_percent_left=0;//滑块的开始位置
var music_percent_top=0;//滑块的结束位置
window.onload = function()
{
music_inIt();
}
function music_inIt(autoStart)//初始化播放器
{
var temp,music_container;
$("musicPosition").innerHTML = echoMusci_frame();
var obj = $(music_id_prefix + "blockContainer");
while(obj.tagName != "BODY")
{
music_percent_left += obj.offsetLeft;
obj = obj.offsetParent;
}
obj = $(music_id_prefix + "blockContainer");
while(obj.tagName != "BODY")
{
music_percent_top += obj.offsetTop;
obj = obj.offsetParent;
}
temp = '<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" id="music_player" ></object>';
document.body.innerHTML += temp;
music_player = $("music_player");
if(autoStart == 1)
{
music_player.AUTOSTART = 1;
}
else
{
music_player.AUTOSTART = 0;
}
//music_player.url = "宁夏.mp3";
}
function music_play(obj)//播放/暂停
{
var state = music_player.playState;//1:已停止 2:暂停 3:正在播放,
if(state == 3)
{
music_player.controls.pause();
obj.src = music_img_path + "play.gif";
obj.title = "播放";
$("music_gif").src = music_img_path + "music_1.gif";
clearInterval(music_timer);//清除定时器
}
else if(state == 1 || state == 2)
{
music_player.controls.play();
obj.src = music_img_path + "pause.gif";
obj.title = "暂停";
$("music_gif").src = music_img_path + "music.gif";
music_timer = setInterval("music_clock",1500);//开始定时器
}
}
function music_stop(obj)//停止
{
music_player.controls.stop();
$(music_id_prefix + "play").src = music_img_path + "play.gif";
$(music_id_prefix + "play").title = "播放";
clearInterval(music_timer);//清除定时器
}
function music_clock()//循环检查进度格
{
}
function echoMusci_frame()
{
return "<table width='300' border='0' cellspacing='0' cellpadding='0' style='color:#ffffff;'><tr> <td width='20' height='87' background='" + music_img_path + "left.gif'></td> <td width='260' align='left' valign='top' background='" + music_img_path + "center.gif'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td height='50'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr valign='bottom' style='color:#ffffff;'><td width='65%' height='23' align='left'>真的爱你-黄家驹</td> <td width='35%' align='right' valign='bottom'>20:23/20:25</td> </tr> <tr> <td height='25' align='left' valign='middle'><img src='" + music_img_path + "music.gif' width='44' height='9' id='music_gif'/></td> <td align='right' valign='middle'>音量:100 + - </td></tr></table></td></tr> <tr> <td align='left' valign='middle'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td height='26' align='left' valign='bottom' style='cursor:pointer'><img src='" + music_img_path + "pause.gif' onclick='music_play(this)' id='" + music_id_prefix + "play' title='暂停'/><img src='" + music_img_path + "stop.gif' onMouseOver='this.src = music_img_path + \"stop_1.gif\"' onmouseout='this.src = music_img_path + \"stop.gif\"' onclick='music_stop(this)'title='停止'/><img src='" + music_img_path + "next.gif' onMouseOver='this.src = music_img_path + \"next_1.gif\"' onmouseout='this.src = music_img_path + \"next.gif\"' /><img src='" + music_img_path + "pre.gif' onMouseOver='this.src = music_img_path + \"pre_1.gif\"' onmouseout='this.src = music_img_path + \"pre.gif\"' /><img src='" + music_img_path + "list.gif' onMouseOver='this.src = music_img_path + \"list_1.gif\"' onmouseout='this.src = music_img_path + \"list.gif\"' /></td> <td width='120' align='left' valign='middle' background='" + music_img_path + "bg_percent.gif' id='" + music_id_prefix + "blockContainer' ><div id='" + music_id_prefix + "block' style='cursor:pointer;border:0px;margin:0px;width:19px;height:8px;background-image:url(" + music_img_path + "block.gif)' onmousedown='blockMouseDown(event,this)'></div></td></tr></table></td> </tr> </table></td><td width='20' background='" + music_img_path + "right.gif'></td> </tr></table>";
}
</script>
</head>
<body style="font-size:12px;">
<div id="musicPosition"></div>
</body></html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
var sUserAgent = navigator.userAgent;
var isIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1;
var EventUtil = new Object();//事件对像
EventUtil.getEvent = function()//获取事件对像
{
if(window.event)
{
return this.formatEvent(window.event);
}
else
{
return EventUtil.getEvent.caller.arguments[0];
}
}
EventUtil.addEventHandler = function(oTarget,sEventType,fnHandler)//添加事件处理函数
{
if(oTarget.addEventListener)
{
//Firefor浏览器兼容
oTarget.addEventListener(sEventType,fnHandler,false);
}
else if(oTarget.attachEvent)
{
//IE浏览器兼容
oTarget.attachEvent("on" + sEventType,fnHandler);
}
else
{
//其它浏览器兼容
oTarget["on" + sEventType] = fnHandler;
}
}
EventUtil.removeEventHandler = function(oTarget,sEventType,fnHandler)//删除事件处理函数
{
if(oTarget.removeEventListener)
{
//Firefor浏览器兼容
oTarget.removeEventListener(sEventType,fnHandler,false);
}
else if(oTarget.detachEvent)
{
//IE浏览器兼容
oTarget.detachEvent("on" + sEventType,fnHandler);
}
else
{
//其它浏览器兼容
oTarget["on" + sEventType] = null;
}
}
EventUtil.formatEvent = function(oEvent)//格式化事件
{
if(isIE)
{
oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
oEvent.isChar = (oEvent.charCode > 0);//表示按下的按键是否与字符有关
oEvent.pageX = oEvent.clientX + document.body.scrollLeft;//鼠标相对于页面的X坐标
oEvent.pageY = oEvent.clinetY + document.body.scrollTop;//鼠标相对于页面的Y坐标
oEvent.preventDefault = function()//阻止事件的默认行为
{
this.returnValue = false;
}
if(oEvent.type == "mouseout")
{
oEvent.relatedTarget = oEvent.toElement;//鼠标事件中鼠标正在进入的元素
}
else if(oEvent.type == "mouseover")
{
oEvent.relatedTarget = oEvent.fromElement;//鼠标事件中鼠标离开的元素
}
oEvent.stopPropagation = function()
{
this.cancelBubble = true;//阻止冒泡事件发生
}
oEvent.target = oEvent.srcElement;
}
return oEvent;
}
var dragObj;
function blockMouseDown(event, obj)//当滑块按下时候
{
var oEvent = EventUtil.getEvent();
dragObj = obj;
EventUtil.addEventHandler(obj,"mousemove",blockMouseMove);
EventUtil.addEventHandler(obj,"mouseup",blockMouseUp);
obj.setCapture();
}
function blockMouseMove()//当滑块在容器上移动时候
{
var oEvent = EventUtil.getEvent();
var width = oEvent.clientX - music_percent_left - 8;
if(width > 101)
{
width = 101;
}
else if(width < 0)
{
width = 0;
}
$(music_id_prefix + "block").style.marginLeft = width;
}
function blockMouseUp()//当滑块在容器按键松开的时候
{
var oEvent = EventUtil.getEvent();
var obj = dragObj;
EventUtil.removeEventHandler(obj,"mousemove",blockMouseMove);
EventUtil.removeEventHandler(obj,"mouseup",blockMouseUp);
}
function $(obj)
{
return document.getElementById(obj);
}
var music_id_prefix = "music";
var music_img_path = "images/";
var music_player;//播放器object
var music_timer;//定时器
var music_percent_left=0;//滑块的开始位置
var music_percent_top=0;//滑块的结束位置
window.onload = function()
{
music_inIt();
}
function music_inIt(autoStart)//初始化播放器
{
var temp,music_container;
$("musicPosition").innerHTML = echoMusci_frame();
var obj = $(music_id_prefix + "blockContainer");
while(obj.tagName != "BODY")
{
music_percent_left += obj.offsetLeft;
obj = obj.offsetParent;
}
obj = $(music_id_prefix + "blockContainer");
while(obj.tagName != "BODY")
{
music_percent_top += obj.offsetTop;
obj = obj.offsetParent;
}
temp = '<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" id="music_player" ></object>';
document.body.innerHTML += temp;
music_player = $("music_player");
if(autoStart == 1)
{
music_player.AUTOSTART = 1;
}
else
{
music_player.AUTOSTART = 0;
}
//music_player.url = "宁夏.mp3";
}
function music_play(obj)//播放/暂停
{
var state = music_player.playState;//1:已停止 2:暂停 3:正在播放,
if(state == 3)
{
music_player.controls.pause();
obj.src = music_img_path + "play.gif";
obj.title = "播放";
$("music_gif").src = music_img_path + "music_1.gif";
clearInterval(music_timer);//清除定时器
}
else if(state == 1 || state == 2)
{
music_player.controls.play();
obj.src = music_img_path + "pause.gif";
obj.title = "暂停";
$("music_gif").src = music_img_path + "music.gif";
music_timer = setInterval("music_clock",1500);//开始定时器
}
}
function music_stop(obj)//停止
{
music_player.controls.stop();
$(music_id_prefix + "play").src = music_img_path + "play.gif";
$(music_id_prefix + "play").title = "播放";
clearInterval(music_timer);//清除定时器
}
function music_clock()//循环检查进度格
{
}
function echoMusci_frame()
{
return "<table width='300' border='0' cellspacing='0' cellpadding='0' style='color:#ffffff;'><tr> <td width='20' height='87' background='" + music_img_path + "left.gif'></td> <td width='260' align='left' valign='top' background='" + music_img_path + "center.gif'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td height='50'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr valign='bottom' style='color:#ffffff;'><td width='65%' height='23' align='left'>真的爱你-黄家驹</td> <td width='35%' align='right' valign='bottom'>20:23/20:25</td> </tr> <tr> <td height='25' align='left' valign='middle'><img src='" + music_img_path + "music.gif' width='44' height='9' id='music_gif'/></td> <td align='right' valign='middle'>音量:100 + - </td></tr></table></td></tr> <tr> <td align='left' valign='middle'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td height='26' align='left' valign='bottom' style='cursor:pointer'><img src='" + music_img_path + "pause.gif' onclick='music_play(this)' id='" + music_id_prefix + "play' title='暂停'/><img src='" + music_img_path + "stop.gif' onMouseOver='this.src = music_img_path + \"stop_1.gif\"' onmouseout='this.src = music_img_path + \"stop.gif\"' onclick='music_stop(this)'title='停止'/><img src='" + music_img_path + "next.gif' onMouseOver='this.src = music_img_path + \"next_1.gif\"' onmouseout='this.src = music_img_path + \"next.gif\"' /><img src='" + music_img_path + "pre.gif' onMouseOver='this.src = music_img_path + \"pre_1.gif\"' onmouseout='this.src = music_img_path + \"pre.gif\"' /><img src='" + music_img_path + "list.gif' onMouseOver='this.src = music_img_path + \"list_1.gif\"' onmouseout='this.src = music_img_path + \"list.gif\"' /></td> <td width='120' align='left' valign='middle' background='" + music_img_path + "bg_percent.gif' id='" + music_id_prefix + "blockContainer' ><div id='" + music_id_prefix + "block' style='cursor:pointer;border:0px;margin:0px;width:19px;height:8px;background-image:url(" + music_img_path + "block.gif)' onmousedown='blockMouseDown(event,this)'></div></td></tr></table></td> </tr> </table></td><td width='20' background='" + music_img_path + "right.gif'></td> </tr></table>";
}
</script>
</head>
<body style="font-size:12px;">
<div id="musicPosition"></div>
</body></html>
按下鼠标的时候,在移动图片会自动移动位置。
在92行加上
event.srcElement.setCapture()
106行加上
event.srcElement.releaseCapture()
这个试试 (我试了下好象还可以)
这个是IE only
ff 下用captureEvent,releaseEvent (PS:这2个事件我还不会用。。)
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
var sUserAgent = navigator.userAgent;
var isIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1;
var EventUtil = new Object();//事件对像
EventUtil.getEvent = function()//获取事件对像
{
if(window.event)
{
return this.formatEvent(window.event);
}
else
{
return EventUtil.getEvent.caller.arguments[0];
}
}
EventUtil.addEventHandler = function(oTarget,sEventType,fnHandler)//添加事件处理函数
{
if(oTarget.addEventListener)
{
//Firefor浏览器兼容
oTarget.addEventListener(sEventType,fnHandler,false);
}
else if(oTarget.attachEvent)
{
//IE浏览器兼容
oTarget.attachEvent("on" + sEventType,fnHandler);
}
else
{
//其它浏览器兼容
oTarget["on" + sEventType] = fnHandler;
}
}
EventUtil.removeEventHandler = function(oTarget,sEventType,fnHandler)//删除事件处理函数
{
if(oTarget.removeEventListener)
{
//Firefor浏览器兼容
oTarget.removeEventListener(sEventType,fnHandler,false);
}
else if(oTarget.detachEvent)
{
//IE浏览器兼容
oTarget.detachEvent("on" + sEventType,fnHandler);
}
else
{
//其它浏览器兼容
oTarget["on" + sEventType] = null;
}
}
EventUtil.formatEvent = function(oEvent)//格式化事件
{
if(isIE)
{
oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
oEvent.isChar = (oEvent.charCode > 0);//表示按下的按键是否与字符有关
oEvent.pageX = oEvent.clientX + document.body.scrollLeft;//鼠标相对于页面的X坐标
oEvent.pageY = oEvent.clinetY + document.body.scrollTop;//鼠标相对于页面的Y坐标
oEvent.preventDefault = function()//阻止事件的默认行为
{
this.returnValue = false;
}
if(oEvent.type == "mouseout")
{
oEvent.relatedTarget = oEvent.toElement;//鼠标事件中鼠标正在进入的元素
}
else if(oEvent.type == "mouseover")
{
oEvent.relatedTarget = oEvent.fromElement;//鼠标事件中鼠标离开的元素
}
oEvent.stopPropagation = function()
{
this.cancelBubble = true;//阻止冒泡事件发生
}
oEvent.target = oEvent.srcElement;
}
return oEvent;
}
var dragObj;
function blockMouseDown(event)//当滑块按下时候
{
var oEvent = EventUtil.getEvent();
dragObj = event.srcElement;
EventUtil.addEventHandler(dragObj,"mousemove",blockMouseMove);
EventUtil.addEventHandler(dragObj,"mouseup",blockMouseUp);
dragObj.setCapture();
}
function blockMouseMove()//当滑块在容器上移动时候
{
var oEvent = EventUtil.getEvent();
var width = oEvent.clientX - music_percent_left - 8;
if(width > 101)
{
width = 101;
}
else if(width < 0)
{
width = 0;
}
$(music_id_prefix + "block").style.marginLeft = width;
}
function blockMouseUp()//当滑块在容器按键松开的时候
{
var oEvent = EventUtil.getEvent();
EventUtil.removeEventHandler(dragObj,"mousemove",blockMouseMove);
EventUtil.removeEventHandler(dragObj,"mouseup",blockMouseUp);
dragObje.releaseCapture();
dragObj = null;
}
function $(obj)
{
return document.getElementById(obj);
}
var music_id_prefix = "music";
var music_img_path = "images/";
var music_player;//播放器object
var music_timer;//定时器
var music_percent_left=0;//滑块的开始位置
var music_percent_top=0;//滑块的结束位置
window.onload = function()
{
music_inIt();
}
function music_inIt(autoStart)//初始化播放器
{
var temp,music_container;
$("musicPosition").innerHTML = echoMusci_frame();
var obj = $(music_id_prefix + "blockContainer");
while(obj.tagName != "BODY")
{
music_percent_left += obj.offsetLeft;
obj = obj.offsetParent;
}
obj = $(music_id_prefix + "blockContainer");
while(obj.tagName != "BODY")
{
music_percent_top += obj.offsetTop;
obj = obj.offsetParent;
}
temp = '<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" id="music_player" ></object>';
document.body.innerHTML += temp;
music_player = $("music_player");
if(autoStart == 1)
{
music_player.AUTOSTART = 1;
}
else
{
music_player.AUTOSTART = 0;
}
//music_player.url = "宁夏.mp3";
}
function music_play(obj)//播放/暂停
{
var state = music_player.playState;//1:已停止 2:暂停 3:正在播放,
if(state == 3)
{
music_player.controls.pause();
obj.src = music_img_path + "play.gif";
obj.title = "播放";
$("music_gif").src = music_img_path + "music_1.gif";
clearInterval(music_timer);//清除定时器
}
else if(state == 1 || state == 2)
{
music_player.controls.play();
obj.src = music_img_path + "pause.gif";
obj.title = "暂停";
$("music_gif").src = music_img_path + "music.gif";
music_timer = setInterval("music_clock",1500);//开始定时器
}
}
function music_stop(obj)//停止
{
music_player.controls.stop();
$(music_id_prefix + "play").src = music_img_path + "play.gif";
$(music_id_prefix + "play").title = "播放";
clearInterval(music_timer);//清除定时器
}
function music_clock()//循环检查进度格
{
}
function echoMusci_frame()
{
return "<table width='300' border='0' cellspacing='0' cellpadding='0' style='color:#ffffff;'><tr> <td width='20' height='87' background='" + music_img_path + "left.gif'></td> <td width='260' align='left' valign='top' background='" + music_img_path + "center.gif'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td height='50'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr valign='bottom' style='color:#ffffff;'><td width='65%' height='23' align='left'>真的爱你-黄家驹</td> <td width='35%' align='right' valign='bottom'>20:23/20:25</td> </tr> <tr> <td height='25' align='left' valign='middle'><img src='" + music_img_path + "music.gif' width='44' height='9' id='music_gif'/></td> <td align='right' valign='middle'>音量:100 + - </td></tr></table></td></tr> <tr> <td align='left' valign='middle'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td height='26' align='left' valign='bottom' style='cursor:pointer'><img src='" + music_img_path + "pause.gif' onclick='music_play(this)' id='" + music_id_prefix + "play' title='暂停'/><img src='" + music_img_path + "stop.gif' onMouseOver='this.src = music_img_path + \"stop_1.gif\"' onmouseout='this.src = music_img_path + \"stop.gif\"' onclick='music_stop(this)'title='停止'/><img src='" + music_img_path + "next.gif' onMouseOver='this.src = music_img_path + \"next_1.gif\"' onmouseout='this.src = music_img_path + \"next.gif\"' /><img src='" + music_img_path + "pre.gif' onMouseOver='this.src = music_img_path + \"pre_1.gif\"' onmouseout='this.src = music_img_path + \"pre.gif\"' /><img src='" + music_img_path + "list.gif' onMouseOver='this.src = music_img_path + \"list_1.gif\"' onmouseout='this.src = music_img_path + \"list.gif\"' /></td> <td width='120' align='left' valign='middle' background='" + music_img_path + "bg_percent.gif' id='" + music_id_prefix + "blockContainer' ><div id='" + music_id_prefix + "block' style='cursor:pointer;border:0px;margin:0px;width:19px;height:8px;background-image:url(" + music_img_path + "block.gif)' onmousedown='blockMouseDown(event,this)'></div></td></tr></table></td> </tr> </table></td><td width='20' background='" + music_img_path + "right.gif'></td> </tr></table>";
}
</script>
</head>
<body style="font-size:12px;">
<div id="musicPosition"></div>
</body></html>
相对就的分别是 captureEvent, releaseEvent,
用法上稍微有点区别。
需要指明捕捉的事件名。captureEvent(eventType)
eventType如下:
Event.ABORT
Event.BLUR
Event.CHANGE
Event.CLICK
Event.DBLCLICK
Event.DRAGDROP
Event.ERROR
Event.FOCUS
Event.KEYDOWN
Event.KEYPRESS
Event.KEYUP
Event.LOAD
Event.MOUSEDOWN
Event.MOUSUEMOVE
Event.MOUSEOUT
Event.MOUSEOVER
Event.MOUSEUP
Event.MOVE
Event.RESET
Event.RESIZE
Event.SELECT
Event.SUBMIT
Event.UNLOAD
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
var sUserAgent = navigator.userAgent;
var isIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1;
var EventUtil = new Object();//事件对像
EventUtil.getEvent = function()//获取事件对像
{
if(window.event)
{
return this.formatEvent(window.event);
}
else
{
return EventUtil.getEvent.caller.arguments[0];
}
}
EventUtil.addEventHandler = function(oTarget,sEventType,fnHandler)//添加事件处理函数
{
if(oTarget.addEventListener)
{
//Firefor浏览器兼容
oTarget.addEventListener(sEventType,fnHandler,false);
}
else if(oTarget.attachEvent)
{
//IE浏览器兼容
oTarget.attachEvent("on" + sEventType,fnHandler);
}
else
{
//其它浏览器兼容
oTarget["on" + sEventType] = fnHandler;
}
}
EventUtil.removeEventHandler = function(oTarget,sEventType,fnHandler)//删除事件处理函数
{
if(oTarget.removeEventListener)
{
//Firefor浏览器兼容
oTarget.removeEventListener(sEventType,fnHandler,false);
}
else if(oTarget.detachEvent)
{
//IE浏览器兼容
oTarget.detachEvent("on" + sEventType,fnHandler);
}
else
{
//其它浏览器兼容
oTarget["on" + sEventType] = null;
}
}
EventUtil.formatEvent = function(oEvent)//格式化事件
{
if(isIE)
{
oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
oEvent.isChar = (oEvent.charCode > 0);//表示按下的按键是否与字符有关
oEvent.pageX = oEvent.clientX + document.body.scrollLeft;//鼠标相对于页面的X坐标
oEvent.pageY = oEvent.clinetY + document.body.scrollTop;//鼠标相对于页面的Y坐标
oEvent.target = oEvent.srcElement;//鼠标相对于页面的Y坐标
oEvent.captureEvent = function()//捕获
{
(oEvent.target).setCapture();
}
oEvent.releaseEvent = function()//释放捕获
{
(oEvent.target).releaseCapture();
}
oEvent.preventDefault = function()//阻止事件的默认行为
{
this.returnValue = false;
}
if(oEvent.type == "mouseout")
{
oEvent.relatedTarget = oEvent.toElement;//鼠标事件中鼠标正在进入的元素
}
else if(oEvent.type == "mouseover")
{
oEvent.relatedTarget = oEvent.fromElement;//鼠标事件中鼠标离开的元素
}
oEvent.stopPropagation = function()
{
this.cancelBubble = true;//阻止冒泡事件发生
}
}
return oEvent;
}
function blockMouseDown()//当滑块按下时候
{
var oEvent = EventUtil.getEvent();
EventUtil.addEventHandler(oEvent.target,"mousemove",blockMouseMove);
EventUtil.addEventHandler(oEvent.target,"mouseup",blockMouseUp);
oEvent.captureEvent();
}
function blockMouseMove()//当滑块在容器上移动时候
{
var oEvent = EventUtil.getEvent();
var width = oEvent.pageX - music_percent_left - 8;
if(width > 101)
{
width = 101;
}
else if(width < 0)
{
width = 0;
}
$(music_id_prefix + "block").style.marginLeft = width;
}
function blockMouseUp()//当滑块在容器按键松开的时候
{
var oEvent = EventUtil.getEvent();
EventUtil.removeEventHandler(oEvent.target,"mousemove",blockMouseMove);
EventUtil.removeEventHandler(oEvent.target,"mouseup",blockMouseUp);
oEvent.releaseEvent();
}
function $(obj)
{
return document.getElementById(obj);
}
var music_id_prefix = "music";
var music_img_path = "images/";
var music_player;//播放器object
var music_timer;//定时器
var music_percent_left=0;//滑块的开始位置
var music_percent_top=0;//滑块的结束位置
window.onload = function()
{
music_inIt();
}
function music_inIt(autoStart)//初始化播放器
{
var temp,music_container;
$("musicPosition").innerHTML = echoMusci_frame();
var obj = $(music_id_prefix + "blockContainer");
while(obj.tagName != "BODY")
{
music_percent_left += obj.offsetLeft;
obj = obj.offsetParent;
}
obj = $(music_id_prefix + "blockContainer");
while(obj.tagName != "BODY")
{
music_percent_top += obj.offsetTop;
obj = obj.offsetParent;
}
temp = '<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" id="music_player" ></object>';
document.body.innerHTML += temp;
music_player = $("music_player");
if(autoStart == 1)
{
music_player.AUTOSTART = 1;
}
else
{
music_player.AUTOSTART = 0;
}
//music_player.url = "宁夏.mp3";
}
function music_play(obj)//播放/暂停
{
var state = music_player.playState;//1:已停止 2:暂停 3:正在播放,
if(state == 3)
{
music_player.controls.pause();
obj.src = music_img_path + "play.gif";
obj.title = "播放";
$("music_gif").src = music_img_path + "music_1.gif";
clearInterval(music_timer);//清除定时器
}
else if(state == 1 || state == 2)
{
music_player.controls.play();
obj.src = music_img_path + "pause.gif";
obj.title = "暂停";
$("music_gif").src = music_img_path + "music.gif";
music_timer = setInterval("music_clock",1500);//开始定时器
}
}
function music_stop(obj)//停止
{
music_player.controls.stop();
$(music_id_prefix + "play").src = music_img_path + "play.gif";
$(music_id_prefix + "play").title = "播放";
clearInterval(music_timer);//清除定时器
}
function music_clock()//循环检查进度格
{
}
function echoMusci_frame()
{
return "<table width='300' border='0' cellspacing='0' cellpadding='0' style='color:#ffffff;'><tr> <td width='20' height='87' background='" + music_img_path + "left.gif'></td> <td width='260' align='left' valign='top' background='" + music_img_path + "center.gif'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td height='50'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr valign='bottom' style='color:#ffffff;'><td width='65%' height='23' align='left'>真的爱你-黄家驹</td> <td width='35%' align='right' valign='bottom'>20:23/20:25</td> </tr> <tr> <td height='25' align='left' valign='middle'><img src='" + music_img_path + "music.gif' width='44' height='9' id='music_gif'/></td> <td align='right' valign='middle'>音量:100 + - </td></tr></table></td></tr> <tr> <td align='left' valign='middle'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td height='26' align='left' valign='bottom' style='cursor:pointer'><img src='" + music_img_path + "pause.gif' onclick='music_play(this)' id='" + music_id_prefix + "play' title='暂停'/><img src='" + music_img_path + "stop.gif' onMouseOver='this.src = music_img_path + \"stop_1.gif\"' onmouseout='this.src = music_img_path + \"stop.gif\"' onclick='music_stop(this)'title='停止'/><img src='" + music_img_path + "next.gif' onMouseOver='this.src = music_img_path + \"next_1.gif\"' onmouseout='this.src = music_img_path + \"next.gif\"' /><img src='" + music_img_path + "pre.gif' onMouseOver='this.src = music_img_path + \"pre_1.gif\"' onmouseout='this.src = music_img_path + \"pre.gif\"' /><img src='" + music_img_path + "list.gif' onMouseOver='this.src = music_img_path + \"list_1.gif\"' onmouseout='this.src = music_img_path + \"list.gif\"' /></td> <td width='120' align='left' valign='middle' background='" + music_img_path + "bg_percent.gif' id='" + music_id_prefix + "blockContainer' ><div id='" + music_id_prefix + "block' style='cursor:pointer;border:0px;margin:0px;width:19px;height:8px;background-image:url(" + music_img_path + "block.gif)' onmousedown='blockMouseDown()'></div></td></tr></table></td> </tr> </table></td><td width='20' background='" + music_img_path + "right.gif'></td> </tr></table>";
}
</script>
</head>
<body style="font-size:12px;">
<div id="musicPosition"></div>
</body></html>
我测试一下就知道了。
EventUtil.addEventHandler(oEvent.target,"mouseup",blockMouseUp);
oEvent.target 此时他只能等于那个滑块啊? EventUtil.addEventHandler(oEvent.target,"mouseup",blockMouseUp);不可能等于别的元素,所以在执行blockMouseUp()
时候,调用这个函数也只能是:id为:musicblock这个元素
在IE中是释放鼠标时光标所指向的对象。
我没有进行深入的调查,
不知道别的浏览器是不是也是这样的。
这样做其实是比容易实现的方法,
浏览器只要遍历一下DOM树中的对象就可以查到。
如果要查找当前捕获鼠标事件的对象则比较麻烦。
因为window的消息机制并不记录这个。
我又想了一下你的想法有可能不正确啊?
你的想法如下:
mouseup的时候,target指向的是当前光标指向的对象。
这依赖于浏览器的实现机制。
如果是这样的话,在我的服务器上面,原来您没有修改之前,为什么可以释放掉呢?
obj = document.getElemtnById("aa");
EventUtil.addEventHandler(obj,"mouseup",blockMouseUp);
function blockMouseUp(event)//当滑块在容器上移动时候
{
我想此时:event.srcElement.id应当等于:aa你说对吗?可是程序执行结果却不是为什么啊?
}
看看w3cschoold上怎么说的,可能会有些帮助.