<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
#scroll {
margin-left: 10px;
margin-top: 10px;
width: 270px;
height: 120px;
overflow: hidden;
}
#scroll li {
width: 270px;
height: 30px;
line-height: 30px;
margin-left: 26px;
}
#scroll li a {
font-size: 14px;
color: #333;
text-decoration: none;
}
#scroll li a:hover {
color: #66CCFF;
}
</style>
</head>
<body>
<div id="scroll">
<ul class="list">
<li><a href="#">《荒野大镖客》重磅来袭</a></li>
<li><a href="#">《星球大战外传》科幻迷不容错过</a></li>
<li><a href="#">《野鹅敢死队》重现战场</a></li>
<li><a href="#">《九死一生》原始丛林探险</a></li>
<li><a href="#">《荒野猎人》莱昂纳多复仇与熊搏斗</a></li>
</ul>
</div>
<script>
//document.getElementById()的最简化应用
function $(element){
if(arguments.length>1){
for(var i=0,length=arguments.length,elements=[];i<length;i++){
elements.push($(arguments[i]));
}
return elements;
}
if(typeof element=="string"){
return document.getElementById(element);
}else{
return element;
}
}
//类创建函数
var Class={
create:function(){
return function(){
this.initialize.apply(this,arguments);
}
}
}
//对象属性方法扩展
Function.prototype.bind=function(object){
var method=this;
return function(){
method.apply(object,arguments);
}
}
var Scroll=Class.create();
Scroll.prototype={
//第一个参数定义要滚动的区域,第二个参数定义每次滚动的高度
initialize:function(element,height,delay){
this.element=$(element);
this.element.innerHTML+=this.element.innerHTML;
this.height=height;
this.delay=delay*1000;
this.maxHeight=this.element.scrollHeight/2;
this.counter=0;
this.scroll();
this.timer="";
this.element.onmouseover=this.stop.bind(this);
this.element.onmouseout=function(){this.timer=setTimeout(this.scroll.bind(this),1000);}.bind(this);
},
scroll:function(){
if(this.element.scrollTop<this.maxHeight){
this.element.scrollTop++;
this.counter++;
}else{
this.element.scrollTop=0;
this.counter=0;
}
if(this.counter<this.height){
this.timer=setTimeout(this.scroll.bind(this),5);
}else{
this.counter=0;
this.timer=setTimeout(this.scroll.bind(this),this.delay);
}
},
stop:function(){
clearTimeout(this.timer);
}
}
new Scroll('a', 22, 2)
</script>
</body>
</html>
js实现li公告栏式上升
最新推荐文章于 2024-07-24 17:16:26 发布