本文实例讲述了基于Marquee.js插件实现的跑马灯效果。分享给大家供大家参考,具体如下:

1、Marquee.js文件
/****************************************************************
- Marquee.js
- 参数:
- ID:滚动对象(必须)
- Direction:滚动方向("top": 0, "up": 0, "bottom": 1, "down": 1, "left": 2, "right": 3)
- Step:步伐
- Width:宽度
- Height:高度
- Timer:影响步伐滚动速度
- DelayTime:延时时间
- WaitTime:初始化时的等待时间
- ScrollStep:卷页步伐
*****************************************************************/
function Marquee(obt) {
if (obt == null || obt == "") {
return;
}
this.ID = document.getElementById(obt.ID);
if (!this.ID) {
alert("初始化错误\r\n请检查标签id设置是否正确!");
this.id = -1;
return;
}
this.Direction = this.Width = this.Height = this.DelayTime = this.WaitTime = this.CTL = this.StartID = this.Stop = this.MouseOver = 0;
this.Step = 1;
this.Timer = 30;
this.DirectionArray = { "top": 0, "up": 0, "bottom": 1, "down": 1, "left": 2, "right": 3 };
if (typeof obt.Direction == "number" && obt.Direction) this.Direction = obt.Direction;
if (typeof obt.Direction == "string" && obt.Direction) this.Direction = this.DirectionArray[obt.Direction.toString().toLowerCase()];
if (typeof obt.Step == "number" && obt.Step) this.Step = obt.Step;
if (typeof obt.Width == "number" && obt.Width) this.Width = obt.Width;
if (typeof obt.Height == "number" && obt.Height) this.Height = obt.Height;
if (typeof obt.Timer == "number" && obt.Timer) this.Timer = obt.Timer;
if (typeof obt.DelayTime == "number" && obt.DelayTime) this.DelayTime = obt.DelayTime;
if (typeof obt.WaitTime == "number" && obt.WaitTime) this.WaitTime = obt.WaitTime;
if (typeof obt.ScrollStep == "number" && obt.ScrollStep) this.ScrollStep = obt.ScrollStep;
this.ID.style.overflow = this.ID.style.overflowX = this.ID.style.overflowY = "hidden";
this.ID.noWrap = true;
this.IsNotOpera = (navigator.userAgent.toLowerCase().indexOf("opera") == -1);
this.Start();
}
Marquee.prototype.Start = function() {
if (this.ID == -1) return;
if (this.Width == 0) this.Width = parseInt(this.ID.style.width);
if (this.Height == 0) this.Height = parseInt(this.ID.style.height);
if (this.Timer < 20) this.Timer = 20;
if (this.WaitTime < 800) this.WaitTime = 800;
this.HalfWidth = Math.round(this.Width / 2);
this.HalfHeight = Math.round(this.Height / 2);
this.BakStep = this.Step;
this.ID.style.width = this.Width + "px";
this.ID.style.height = this.Height + "px";
if (typeof this.ScrollStep != "number") this.ScrollStep = this.Direction > 1 ? this.Width : this.Height;
var templateLeft = "<table cellspacing='0' cellpadding='0' style='border-collapse:collapse;display:inline;'><tr><td noWrap=true style='white-space: nowrap;word-break:keep-all;'>MSCLASS_TEMP_HTML</td><td noWrap=true style='white-space: nowrap;word-break:keep-all;'>MSCLASS_TEMP_HTML</td></tr></table>";
var templateTop = "<table cellspacing='0' cellpadding='0' style='border-collapse:collapse;'><tr><td>MSCLASS_TEMP_HTML</td></tr><tr><td>MSCLASS_TEMP_HTML</td></tr></table>";
var msobj = this;
msobj.tempHTML = msobj.ID.innerHTML;
if (msobj.Direction <= 1) {
msobj.ID.innerHTML = templateTop.replace(/MSCLASS_TEMP_HTML/g, msobj.ID.innerHTML);
}
else {
if (msobj.ScrollStep == 0 && msobj.DelayTime == 0) {
msobj.ID.innerHTML += msobj.ID.innerHTML;
}
else {
msobj.ID.innerHTML = templateLeft.replace(/MSCLASS_TEMP_HTML/g, msobj.ID.innerHTML);
}
}
var timer = this.Timer;
var delaytime = this.DelayTime;
var waittime = this.WaitTime;
msobj.StartID = function() { msobj.Scroll() }
msobj.Continue = function() {
if (msobj.MouseOver == 1) {
setTimeout(msobj.Continue, delaytime);
}
else {
clearInterval(msobj.TimerID);
msobj.CTL = msobj.Stop = 0;
msobj.TimerID = setInterval(msobj.StartID, timer);
}
}
msobj.Pause = function() {
msobj.Stop = 1;
clearInterval(msobj.TimerID);
setTimeout(msobj.Continue, delaytime);
}
msobj.Begin = function() {
msobj.ClientScroll = msobj.Direction > 1 ? msobj.ID.scrollWidth / 2 : msobj.ID.scrollHeight / 2;
if ((msobj.Direction <= 1 && msobj.ClientScroll <= msobj.Height + msobj.Step) || (msobj.Direction > 1 && msobj.ClientScroll <= msobj.Width + msobj.Step)) {
msobj.ID.innerHTML = msobj.tempHTML;
delete (msobj.tempHTML);
return;
}
delete (msobj.tempHTML);
msobj.TimerID = setInterval(msobj.StartID, timer);
if (msobj.ScrollStep < 0) return;
msobj.ID.onmousemove = function(event) {
if (msobj.ScrollStep == 0 && msobj.Direction > 1) {
var event = event || window.event;
if (window.event) {
if (msobj.IsNotOpera) {
msobj.EventLeft = event.srcElement.id == msobj.ID.id ? event.offsetX - msobj.ID.scrollLeft : event.srcElement.offsetLeft - msobj.ID.scrollLeft + event.offsetX;
}
else {
msobj.ScrollStep = null;
return;
}
}
else {
msobj.EventLeft = event.layerX - msobj.ID.scrollLeft;
}
msobj.Direction = msobj.EventLeft > msobj.HalfWidth ? 3 : 2;
msobj.AbsCenter = Math.abs(msobj.HalfWidth - msobj.EventLeft);
msobj.Step = Math.round(msobj.AbsCenter * (msobj.BakStep * 2) / msobj.HalfWidth);
}
}
msobj.ID.onmouseover = function() {
if (msobj.ScrollStep == 0) return;
msobj.MouseOver = 1;
clearInterval(msobj.TimerID);
}
msobj.ID.onmouseout = function() {
if (msobj.ScrollStep == 0) {
if (msobj.Step == 0) msobj.Step = 1;
return;
}
msobj.MouseOver = 0;
if (msobj.Stop == 0) {
clearInterval(msobj.TimerID);
msobj.TimerID = setInterval(msobj.StartID, timer);
}
}
}
setTimeout(msobj.Begin, waittime);
}
Marquee.prototype.Scroll = function() {
switch (this.Direction) {
case 0:
this.CTL += this.Step;
if (this.CTL >= this.ScrollStep && this.DelayTime > 0) {
this.ID.scrollTop += this.ScrollStep + this.Step - this.CTL;
this.Pause();
return;
}
else {
if (this.ID.scrollTop >= this.ClientScroll) {
this.ID.scrollTop -= this.ClientScroll;
}
this.ID.scrollTop += this.Step;
}
break;
case 1:
this.CTL += this.Step;
if (this.CTL >= this.ScrollStep && this.DelayTime > 0) {
this.ID.scrollTop -= this.ScrollStep + this.Step - this.CTL;
this.Pause();
return;
}
else {
if (this.ID.scrollTop <= 0) {
this.ID.scrollTop += this.ClientScroll;
}
this.ID.scrollTop -= this.Step;
}
break;
case 2:
this.CTL += this.Step;
if (this.CTL >= this.ScrollStep && this.DelayTime > 0) {
this.ID.scrollLeft += this.ScrollStep + this.Step - this.CTL;
this.Pause();
return;
}
else {
if (this.ID.scrollLeft >= this.ClientScroll) {
this.ID.scrollLeft -= this.ClientScroll;
}
this.ID.scrollLeft += this.Step;
}
break;
case 3:
this.CTL += this.Step;
if (this.CTL >= this.ScrollStep && this.DelayTime > 0) {
this.ID.scrollLeft -= this.ScrollStep + this.Step - this.CTL;
this.Pause();
return;
}
else {
if (this.ID.scrollLeft <= 0) {
this.ID.scrollLeft += this.ClientScroll;
}
this.ID.scrollLeft -= this.Step;
}
break;
}
}
2、脚本调用
window.onload = function() {
new Marquee({ ID: "scrollNews", Direction: "top", Step: 2, Width: 0, Height: 80, Timer: 50, DelayTime: 1000, WaitTime: 1000, ScrollStep: 80 });
}
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript切换特效与技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
# Marquee.js
# 插件
# 跑马灯
# 原生JS实现跑马灯效果
# js实现文字跑马灯效果
# js跑马灯代码(自写)
# javascript 单行文字向上跑马灯滚动显示
# JS实现状态栏跑马灯文字效果代码
# Javascript实现跑马灯效果的简单实例
# js文本框走动跑马灯效果代码分享
# javascript跑马灯悬停放大效果实现代码
# javascript跑马灯抽奖实例讲解
# 原生JS实现图片跑马灯特效
# 相关内容
# 感兴趣
# 数据结构
# 给大家
# 更多关于
# 所述
# 程序设计
# 是否正确
# 请检查
# 讲述了
# toLowerCase
# toString
# string
# style
# overflowY
# overflowX
# overflow
# amp
# MouseOver
相关文章:
建站主机无法访问?如何排查域名与服务器问题
如何在IIS中新建站点并配置端口与物理路径?
建站之星导航菜单设置与功能模块配置全攻略
测试制作网站有哪些,测试性取向的权威测试或者网站?
内部网站制作流程,如何建立公司内部网站?
已有域名和空间如何快速搭建网站?
建站org新手必看:2024最新搭建流程与模板选择技巧
实惠建站价格推荐:2025年高性价比自助建站套餐解析
建站之星代理如何获取技术支持?
详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)
广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?
公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?
,怎么在广州志愿者网站注册?
建站主机选哪种环境更利于SEO优化?
车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?
个人摄影网站制作流程,摄影爱好者都去什么网站?
婚礼视频制作网站,学习*后期制作的网站有哪些?
攀枝花网站建设,攀枝花营业执照网上怎么年审?
如何选择CMS系统实现快速建站与SEO优化?
建站之星如何快速更换网站模板?
c++怎么实现高并发下的无锁队列_c++ std::atomic原子变量与CAS操作【详解】
Swift中swift中的switch 语句
深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?
网站设计制作公司地址,网站建设比较好的公司都有哪些?
深圳网站制作平台,深圳市做网站好的公司有哪些?
如何注册花生壳免费域名并搭建个人网站?
装修招标网站设计制作流程,装修招标流程?
贸易公司网站制作流程,出口贸易网站设计怎么做?
如何确认建站备案号应放置的具体位置?
如何在阿里云香港服务器快速搭建网站?
香港服务器网站推广:SEO优化与外贸独立站搭建策略
上海网站制作网站建设公司,建筑电工证网上查询系统入口?
高端网站建设与定制开发一站式解决方案 中企动力
广州营销型建站服务商推荐:技术优势与SEO优化解析
移民网站制作流程,怎么看加拿大移民官网?
移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?
制作网站公司那家好,网络公司是做什么的?
mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?
PHP 500报错的快速解决方法
武汉外贸网站制作公司,现在武汉外贸前景怎么样啊?
C++如何将C风格字符串(char*)转换为std::string?(代码示例)
Android自定义控件实现温度旋转按钮效果
网站好制作吗知乎,网站开发好学吗?有什么技巧?
黑客如何通过漏洞一步步攻陷网站服务器?
北京网站制作网页,网站升级改版需要多久?
如何选择适配移动端的WAP自助建站平台?
浅谈Javascript中的Label语句
,制作一个手机app网站要多少钱?
制作网站外包平台,自动化接单网站有哪些?
建站之星安装提示数据库无法连接如何解决?
*请认真填写需求信息,我们会在24小时内与您取得联系。