基于Marquee.js插件实现的跑马灯效果示例


Posted in Javascript onJanuary 25, 2017

本文实例讲述了基于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实现的分页函数
Dec 22 Javascript
Prototype Object对象 学习
Jul 12 Javascript
js获取ajax返回值代码
Apr 30 Javascript
JavaScript导航脚本判断当前导航
Jul 12 Javascript
jQuery给表格添加分页效果
Mar 02 Javascript
EasyUI在Panel上动态添加LinkButton按钮
Aug 11 Javascript
BootStrap模态框不垂直居中的解决方法
Oct 19 Javascript
JS实现的抛物线运动效果示例
Jan 30 Javascript
解决layer.confirm快速点击会重复触发事件的问题
Sep 23 Javascript
用vue 实现手机触屏滑动功能
May 28 Javascript
vue中移动端调取本地的复制的文本方式
Jul 18 Javascript
基于react项目打包css引用路径错误解决方案
Oct 28 Javascript
angular和BootStrap3实现购物车功能
Jan 25 #Javascript
jquery 实现复选框的全选操作实例代码
Jan 24 #Javascript
谈谈JavaScript数组常用方法总结
Jan 24 #Javascript
js实现日历的简单算法
Jan 24 #Javascript
JSON键值对序列化和反序列化解析
Jan 24 #Javascript
js自制图片放大镜功能
Jan 24 #Javascript
JS中绑定事件顺序(事件冒泡与事件捕获区别)
Jan 24 #Javascript
You might like
PHP编程中的常见漏洞和代码实例
2014/08/06 PHP
PHP5.3连接Oracle客户端及PDO_OCI模块的安装方法
2016/05/13 PHP
NodeJS框架Express的模板视图机制分析
2011/07/19 NodeJs
如何制作浮动广告 JavaScript制作浮动广告代码
2012/12/30 Javascript
jQuery 设置 CSS 属性示例介绍
2014/01/16 Javascript
ff chrome和ie下全局动态定位的异同及全局高度的取法
2014/06/30 Javascript
jQuery中clone()方法用法实例
2015/01/16 Javascript
javascript原始值和对象引用实例分析
2015/04/25 Javascript
javascript实现倒计时(精确到秒)
2015/06/26 Javascript
每天一篇javascript学习小结(属性定义方法)
2015/11/19 Javascript
JavaScript获取当前cpu使用率的方法
2015/12/15 Javascript
基于Javascript实现弹出页面效果
2016/01/01 Javascript
浅谈Jquery中Ajax异步请求中的async参数的作用
2016/06/06 Javascript
AngularJS入门教程之 XMLHttpRequest实例讲解
2016/07/27 Javascript
引用jquery框架后出错的解决方法
2016/08/09 Javascript
基于jQuery实现瀑布流页面
2017/04/11 jQuery
微信小程序引用公共js里的方法的实例详解
2017/08/17 Javascript
基于Bootstrap和JQuery实现动态打开和关闭tab页的实例代码
2019/06/10 jQuery
原生js代码能实现call和bind吗
2019/07/31 Javascript
Angular6项目打包优化的实现方法
2019/12/15 Javascript
vue任意关系组件通信与跨组件监听状态vue-communication
2020/10/18 Javascript
antd design table更改某行数据的样式操作
2020/10/31 Javascript
python和C语言混合编程实例
2014/06/04 Python
Python字符串处理函数简明总结
2015/04/13 Python
python的else子句使用指南
2016/02/27 Python
Django的信号机制详解
2017/05/05 Python
Python字符串处理实例详解
2017/05/18 Python
Python实现判断给定列表是否有重复元素的方法
2018/04/11 Python
Django视图扩展类知识点详解
2019/10/25 Python
python turtle工具绘制四叶草的实例分享
2020/02/14 Python
python查询MySQL将数据写入Excel
2020/10/29 Python
钉钉企业内部H5微应用开发详解
2020/05/12 HTML / CSS
简历中个人自我评价分享
2014/03/15 职场文书
关于保护环境的标语
2014/06/09 职场文书
承诺函格式模板
2015/01/21 职场文书
详解Python flask的前后端交互
2022/03/31 Python