JS仿marquee無縫滾動實用篇+JS随機生成字元
主要應用于預約挂号滾動等!
JS仿marquee無縫滾動實用篇+JS随機生成字元串

1. 第一種marquee直接應用
别忘了寫overflow:hidden屬性,這個很重要!還有如果頁面不滾動,父div屬性要加上position:relative
demo寬高可根據div大小随意調整!
<span style="font-size:14px;"> <div id=demo style="overflow:hidden; width:128px; height:300px;">
<div id=demo1>
<li> <span>女士</span> <span>無痛人流</span> <span>三分鐘前</span> </li><br/>
<li> <span>女士1</span> <span>無痛人流</span> <span>三分鐘前</span> </li><br/>
<li> <span>女士2</span> <span>無痛人流</span> <span>三分鐘前</span> </li><br/>
<li> <span>女士3</span> <span>無痛人流</span> <span>三分鐘前</span> </li><br/>
<li> <span>女士4</span> <span>無痛人流</span> <span>三分鐘前</span> </li><br/>
<li> <span>女士5</span> <span>無痛人流</span> <span>三分鐘前</span> </li><br/>
<li> <span>女士6</span> <span>無痛人流</span> <span>三分鐘前</span> </li><br/>
<li> <span>女士7</span> <span>無痛人流</span> <span>三分鐘前</span> </li><br/>
<li> <span>女士8</span> <span>無痛人流</span> <span>三分鐘前</span> </li><br/>
</div>
<div id="demo2">
</div>
</div>
<script language="javascript">
var speed = 30;
document.getElementById('demo2').innerHTML = document.getElementById('demo1').innerHTML;
function Marquee(){
//alert(document.getElementById('demo2').offsetTop);
//alert(document.getElementById('demo1').offsetHeight);
if(document.getElementById('demo2').offsetTop-document.getElementById('demo').scrollTop<=0){
document.getElementById('demo').scrollTop-=document.getElementById('demo1').offsetHeight;
}else{
document.getElementById('demo').scrollTop++;
}
}
var MyMar=setInterval('Marquee()',speed);
document.getElementById('demo').οnmοuseοver=function(){clearInterval(MyMar)};
document.getElementById('demo').οnmοuseοut=function(){MyMar=setInterval(Marquee,speed)};
</script></span>
2.scrolltext.js 滾動JS
附上效果圖,滾動中并随機生成時間,随機病種 和姓名
function ScrollText(content,btnPrevious,btnNext,autoStart)
{
this.Delay = 10;
this.LineHeight = 20;
this.Amount = 1;//娉ㄦ剰:LineHeight涓€瀹氳鑳芥暣闄mount.
this.Direction = "up";
this.Timeout = 1500;
this.ScrollContent = this.$(content);
this.ScrollContent.innerHTML += this.ScrollContent.innerHTML;
//this.ScrollContent.scrollTop = 0;
if(btnNext)
{
this.NextButton = this.$(btnNext);
this.NextButton.onclick = this.GetFunction(this,"Next");
this.NextButton.onmouseover = this.GetFunction(this,"Stop");
this.NextButton.onmouseout = this.GetFunction(this,"Start");
}
if(btnPrevious)
{
this.PreviousButton = this.$(btnPrevious);
this.PreviousButton.onclick = this.GetFunction(this,"Previous");
this.PreviousButton.onmouseover = this.GetFunction(this,"Stop");
this.PreviousButton.onmouseout = this.GetFunction(this,"Start");
}
this.ScrollContent.onmouseover = this.GetFunction(this,"Stop");
this.ScrollContent.onmouseout = this.GetFunction(this,"Start");
if(autoStart)
{
this.Start();
}
}
ScrollText.prototype.$ = function(element)
{
return document.getElementById(element);
}
ScrollText.prototype.Previous = function()
{
clearTimeout(this.AutoScrollTimer);
clearTimeout(this.ScrollTimer);
this.Scroll("up");
}
ScrollText.prototype.Next = function()
{
clearTimeout(this.AutoScrollTimer);
clearTimeout(this.ScrollTimer);
this.Scroll("down");
}
ScrollText.prototype.Start = function()
{
clearTimeout(this.AutoScrollTimer);
this.AutoScrollTimer = setTimeout(this.GetFunction(this,"AutoScroll"), this.Timeout);
}
ScrollText.prototype.Stop = function()
{
clearTimeout(this.ScrollTimer);
clearTimeout(this.AutoScrollTimer);
}
ScrollText.prototype.AutoScroll = function()
{
if(this.Direction == "up")
{
if(parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2)
{
this.ScrollContent.scrollTop = 0;
}
this.ScrollContent.scrollTop += this.Amount;
}
else
{
if(parseInt(this.ScrollContent.scrollTop) <= 0)
{
this.ScrollContent.scrollTop = parseInt(this.ScrollContent.scrollHeight) / 2;
}
this.ScrollContent.scrollTop -= this.Amount;
}
if(parseInt(this.ScrollContent.scrollTop) % this.LineHeight != 0)
{
this.ScrollTimer = setTimeout(this.GetFunction(this,"AutoScroll"), this.Delay);
}
else
{
this.AutoScrollTimer = setTimeout(this.GetFunction(this,"AutoScroll"), this.Timeout);
}
}
ScrollText.prototype.Scroll = function(direction)
{
if(direction=="up")
{
if(this.ScrollContent.scrollTop == 0)
{
this.ScrollContent.scrollTop = parseInt(this.ScrollContent.scrollHeight) / 2;
}
this.ScrollContent.scrollTop -= this.Amount;
}
else
{
this.ScrollContent.scrollTop += this.Amount;
}
if(parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2)
{
this.ScrollContent.scrollTop = 0;
}
if(parseInt(this.ScrollContent.scrollTop) % this.LineHeight != 0)
{
this.ScrollTimer = setTimeout(this.GetFunction(this,"Scroll",direction), this.Delay);
}
}
ScrollText.prototype.GetFunction = function(variable,method,param)
{
return function()
{
variable[method](param);
}
}
這個不願意讀的就直接複制好了,很好用!
<script language="javascript" type="text/javascript"> window.onload = function(){ var marquee = new ScrollText("day_yuyue"); marquee.LineHeight = 28; marquee.Amount = 1; marquee.Timeout = 30; marquee.Delay = 30; marquee.Start();} </script>
<div class="day_yy" id="day_yuyue">
<script src="/yy.js" type="text/javascript"></script>
</div> .day_yy {
- width: 263px;
- height: 133px;
- overflow: hidden;
- margin: 80px 50px;
}
3.yy.js
<span style="color:#000000;">name2 = new Array
name2 = new Array
time2[0] = '潘主任'
time2[1] = '潘主任'
time2[2] = '盧主任'
time2[3] = '劉主任'
name2[0] = '李'
name2[1] = '王'
name2[2] = '張'
name2[3] = '劉'
name2[4] = '陳'
name2[5] = '楊'
name2[6] = '黃'
name2[7] = '趙'
name2[8] = '吳'
name2[9] = '周'
name2[10] = '徐'
name2[11] = '孫'
name2[12] = '馬'
name2[13] = '朱'
name2[14] = '胡'
name2[15] = '高'
name2[16] = '林'
name2[17] = '羅'
name2[18] = '鄭'
name2[19] = '梁'
name2[20] = '謝'
name2[21] = '宋'
name2[22] = '唐'
name2[23] = '許'
name2[24] = '韓'
name2[25] = '馮'
name2[26] = '鄧'
name2[27] = '曹'
name2[28] = '彭'
name2[29] = '曾'
name2[30] = '蕭'
name2[31] = '田'
name2[32] = '董'
name2[33] = '袁'
name2[34] = '潘'
name2[35] = '于'
name2[36] = '蔣'
name2[37] = '蔡'
name2[38] = '餘'
name2[39] = '杜'
name2[40] = '葉'
name2[41] = '程'
name2[42] = '蘇'
name2[43] = '魏'
name2[44] = '呂'
name2[45] = '丁'
name2[46] = '任'
name2[47] = '沈'
name2[48] = '姚'
name2[49] = '盧'
name2[50] = '姜'
name2[51] = '崔'
name2[52] = '鐘'
name2[53] = '譚'
name2[54] = '陸'
name2[55] = '汪'
name2[56] = '範'
name2[57] = '金'
name2[58] = '石'
name2[59] = '廖'
name2[60] = '賈'
name2[61] = '夏'
name2[62] = '韋'
name2[63] = '傅'
name2[64] = '方'
name2[65] = '白'
name2[66] = '鄒'
name2[67] = '孟'
name2[68] = '熊'
name2[69] = '秦'
name2[70] = '邱'
name2[71] = '江'
name2[72] = '尹'
name2[73] = '薛'
name2[74] = '閻'
name2[75] = '段'
name2[76] = '雷'
name2[77] = '侯'
name2[78] = '龍'
name2[79] = '史'
name2[80] = '陶'
name2[81] = '黎'
name2[82] = '賀'
name2[83] = '顧'
name2[84] = '毛'
name2[85] = '郝'
name2[86] = '龔'
name2[87] = '邵'
name2[88] = '萬'
name2[89] = '錢'
name2[90] = '嚴'
name2[91] = '覃'
name2[92] = '武'
name2[93] = '戴'
name2[94] = '莫'
name2[95] = '孔'
name2[96] = '向'
name2[97] = '湯'
sex2[0]='**'
sex2[1]='**'
document.write("<p class='p2'><span class='p_mz'>"+name2[parseInt(Math.random()*(97-0)+0)]+sex2[parseInt(Math.random()*(2-0)+0)]+"<\/span><span class='new1'>預約成功</span><span class='p_dz'>"+time2[parseInt(Math.random()*(10-0)+0)]+"<\/span><span class='new2'>"+parseInt(Math.random()*(60-0)+0)+"分鐘前</span><\/p>");</span>
最後的 document.write 别忘記多寫幾行!!!