新闻滚动条求助..
function addLoadListener(fn){
if(typeof window.addEventListener !='undefined')
{
window.addEventListener('load',fn,false);
}
else if(typeof document.addEventListener !='undefined')
{
document.addEventListener('load',fn,false);
}
else if(typeof window.attachEvent !='undefined')
{
window.attachEvent('onload',fn);
}
else
{
var old=window.onload;
if(window.onload !='function')
{
window.onload=fn;
}
else
{
window.onload=function()
{
old();
fn();
};
}
}
}
function attachEventListener(target,eventType,functionRef,capture)
{
if(typeof target.addEventListener !="undefined")
{
target.addEventListener(eventType,functionRef,capture);
}
else if(typeof target.attachEvent !="undefined")
{
target.attachEvent("on"+eventType,functionRef);
}
else
{
var eventType="on"+eventType;
if(typeof target[eventType] == "function")
{
var old=target[eventType];
target[eventType]=function()
{
old();
return functionRef();
};
}
else
{
target[eventType]=functionRef;
}
}
}
function retrieveComputedStyle(target,styleProperty)
{
var elementStyle=null;
if(typeof target.currentStyle !="undefined")
{
elementStyle=target.currentStyle;
}
else
{
elementStyle=document.defaultView.getComputedStyle(target,null);
}
return elementStyle[styleProperty];
}
addLoadListener(initNewTicker);
function initNewTicker()
{
var newScroller=document.getElementById("newScroller");
newScroller.style.top=0;
newScroller.bool=true;
attachEventListener(newScroller.parentNode,"mouseover",gundong,false);
attachEventListener(newScroller.parentNode,"mouseout",yikai,false);
if(retrieveComputedStyle(newScroller,"position") =="relative")
{
var parentW=newScroller.offsetHeight;
newScroller.style.position="absolute";
newScroller.realwidth=newScroller.offsetHeight;
if(parentW>newScroller.realwidth)
{
newScroller.realwidth=parentW;
}
newScroller.style.position="relative";
}
else
{
newScroller.realwidth=newScroller.clientHeight;
}
moveLeft();
return true;
}
function moveLeft()
{
var speed=5;
var newScroller=document.getElementById("newScroller");
var currentLeft=parseInt(newScroller.style.top);
if(currentLeft<newScroller.realwidth*-1)
{
newScroller.style.top=newScroller.parentNode.offsetHeight+"px";
}
else
{
newScroller.style.top=(currentLeft-speed)+"px";
}
newScroller.targetTime=setTimeout("moveLeft()",100);
return true;
}
function getEventTarget(event)
{
var targetElement=null;
if(typeof event.target !="undefined")
{
targetElement=event.target;
}
else if(typeof event.srcElement !="undefined")
{
targetElement=event.srcElement;
}
while(targetElement.nodeType==3 && targetElement.parentNode !=null)
{
targetElement=targetElement.parentNode;
}
return targetElement;
}
function stopEvent(event)
{
if(typeof event.stopPropagation !="undefined")
{
event.stopPropagation()
}
else{
cancelBubble=true;
}
}
function gundong(event)
{
if(typeof event=="undefined")
{
event=window.event
}
target=getEventTarget(event);
while(target.nodeName.toLowerCase() !="div")
{
target=target.parentNode;
}
clearTimeout(document.getElementById("newScroller").targetTime);
stopEvent(event);
return true;
}
function yikai(event)
{if(typeof event=="undefined")
{
event=window.event
}
target=getEventTarget(event);
while(target.nodeName.toLowerCase() !="div")
{
target=target.parentNode;
}
if(target.id == "newScroller")
{
moveLeft();
}
stopEvent(event);
}
HTML代码;
Css:
#newTicker
{
position:relative;
width:300px;
height:35px;
overflow:hidden;
}
#newScroller
{
position:absolute;
position/**/:relative;
height:35px;
line-height:35px;
white-space:nowrap;
}
<body>
<div id=newTicker>
<div id=newScroller>
dddddddddddddddddddddddddfdddddddddddddddd12333322221232dfdfffffdfdfdfdfd
</div>
</div>
</body>