基于jQuery实现一个marquee无缝滚动的插件


Posted in Javascript onMarch 09, 2017

基于jQuery,实现一个marquee无缝滚动的插件,已经发布到 git.oschina.net,演示稍后更新(更新到 http://git.oschina.net/mqycn/jQueryMarquee )。

代码如下:

/**
 * 类库名称:jQuery.marquee
 * 实现功能:基于 jquery 实现的 marquee 无缝滚动插件
 * 作者主页:http://www.miaoqiyuan.cn/
 * 联系邮箱:mqycn@126.com
 * 使用说明:http://www.miaoqiyuan.cn/p/jquery-marquee
 * 最新版本:http://git.oschina.net/mqycn/jQueryMarquee
*/
jQuery.fn.extend({
  marquee : function(opt, callback){
    opt = opt || {};
    opt.speed = opt.speed || 30;
    opt.direction = opt.direction || 'left';
    opt.pixels = opt.pixels || 2;
    switch( opt.direction ){
      case "left":
      case "right":
        opt.weight = "width";
        opt.margin = "margin-left";
        opt.tpl = '<table><tr><td>[TABLE]</td><td>[TABLE]</td></tr></table>';
        break;
      case "top":
      case "bottom":
        opt.weight = "height";
        opt.margin = "margin-top";
        opt.tpl = '<table><tr><td>[TABLE]</td></tr></tr><td>[TABLE]</td></tr></table>';
        break;
      default:
        throw Error("[jQuery.marquee.js] Options.direction Error!");
    }
    switch( opt.direction ){
      case "left":
      case "top":
        opt.addon = -1;
        break;
      case "right":
      case "bottom":
        opt.addon = 1;
        break;
      default:
        throw Error("[jQuery.marquee.js] Options.direction Error!");
    }
    callback = typeof callback == "function" ? callback : function(){};
    //设置宽度
    $(this).each(function(){
      if( this.control ){
        clearInterval(this.control);
      } else {
        //如果第一次执行,初始化代码
        $(this)
          .data(opt.weight, opt.weight == 'width' ? $(this).find("table").width() : $(this).find("table").height())
          .width($(this).data(opt.weight) * 2)
          .html(opt.tpl.replace(/\[TABLE\]/ig, $(this).html()))
          .mouseover(function(){
            $(this).data("pause", true);
          }).mouseout(function(){
            $(this).data("pause", false);
          });
      }
      this.control = setInterval((function(){
        if( $(this).data("pause") ){
          return;
        }
        var _margin = parseInt($(this).css(opt.margin)) + opt.addon * opt.pixels;
        if( opt.addon == -1 && _margin + $(this).data(opt.weight) < 0 ){
          _margin = 0;
        }else if( opt.addon == 1, _margin > 0 ){
          console.log(_margin < 0,$(this).data(opt.weight));
          _margin = -1 * $(this).data(opt.weight);
        }
        $(this).css(opt.margin, _margin + "px");
        callback.bind(this)();
      }).bind(this), opt.speed);
    });
    return $(this);
  }
});

如果在IE9以下使用,还需要在之前增加如下代码:

/**
 * IE8插件(解决 function 不支持 bind 的问题),非原创
*/
if (!Function.prototype.bind) {
  Function.prototype.bind = function(oThis) {
    if (typeof this !== "function") {
      throw new TypeError("[jQuery.marquee.ie8] Caller is not a function");
    }
    var aArgs = Array.prototype.slice.call(arguments, 1),
      fToBind = this,
      fNOP = function() {},
      fBound = function() {
        return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)));
      };
    fNOP.prototype = this.prototype;
    fBound.prototype = new fNOP();
    return fBound;
  };
}

一共有三个可选参数,一个回调方法。

direction,移动方向:支持 左:left 右:right 上:top 下:bottom;

pixels,每次移动的像素数

speed,两次移动之前的间隔时间数(毫秒)

调用方法如下:

$("scroll-a").marquee();
$("scroll-b").marquee({direction:'top'});
$("scroll-c").marquee({direction:'top',pixels:2,speed:30});
$("scroll-d").marquee({direction:"top",pixels:2,speed:30}, function(){
  console.log("执行了一次");
});

以上所述是小编给大家介绍的基于jQuery实现一个marquee无缝滚动的插件,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
漂亮的widgets,支持换肤和后期开发新皮肤
Apr 23 Javascript
基于jquery的文本框与autocomplete结合使用(asp.net+json)
May 30 Javascript
jquery实现点击TreeView文本父节点展开/折叠子节点
Jan 10 Javascript
javascript实现TreeView 无刷新展开的实例代码
Jul 13 Javascript
js保留两位小数使用toFixed实现
Jul 29 Javascript
jquery实现点击向下展开菜单项(伸缩导航)效果
Aug 22 Javascript
Javascript技术栈中的四种依赖注入详解
Feb 23 Javascript
javascript验证内容为数字以及长度为10的简单实例
Aug 20 Javascript
整理一些最近经常遇到的前端面试题
Apr 25 Javascript
微信小程序城市定位的实现实例(获取当前所在国家城市信息)
May 17 Javascript
vue-music关于Player播放器组件详解
Nov 28 Javascript
JS浮点数运算结果不精确的Bug解决
Aug 01 Javascript
jQuery插件HighCharts绘制2D圆环图效果示例【附demo源码下载】
Mar 09 #Javascript
js实现显示手机号码效果
Mar 09 #Javascript
jQuery插件HighCharts绘制2D半圆环图效果示例【附demo源码下载】
Mar 09 #Javascript
javascript 秒表计时器实现代码
Mar 09 #Javascript
react实现pure render时bind(this)隐患需注意!
Mar 09 #Javascript
使用bootstrap-paginator.js 分页来进行ajax 异步分页请求示例
Mar 09 #Javascript
jQuery插件HighCharts绘制2D金字塔图效果示例【附demo源码下载】
Mar 09 #Javascript
You might like
通过对php一些服务器端特性的配置加强php的安全
2006/10/09 PHP
一个程序下载的管理程序(一)
2006/10/09 PHP
如何用phpmyadmin设置mysql数据库用户的权限
2012/01/09 PHP
PHP开发微信支付的代码分享
2014/05/25 PHP
PHP二维数组排序简单实现方法
2016/02/14 PHP
浅谈thinkphp的nginx配置,以及重写隐藏index.php入口文件方法
2019/10/12 PHP
使用js 设置url参数
2013/07/08 Javascript
JavaScript实现两个Table固定表头根据页面大小自行调整
2014/01/03 Javascript
js中style.display=&quot;&quot;无效的解决方法
2014/10/30 Javascript
jQuery中insertAfter()方法用法实例
2015/01/08 Javascript
JavaScript编程中window的location与history对象详解
2015/10/26 Javascript
jQuery中inArray方法注意事项分析
2016/01/25 Javascript
利用JS判断字符串是否含有数字与特殊字符的方法小结
2016/11/25 Javascript
jquery+ajax实现省市区三级联动效果简单示例
2017/01/04 Javascript
React Native日期时间选择组件的示例代码
2018/04/27 Javascript
从零到一详聊创建Vue工程及遇到的常见问题
2019/04/25 Javascript
python实现在无须过多援引的情况下创建字典的方法
2014/09/25 Python
web.py在SAE中的Session问题解决方法(使用mysql存储)
2015/06/24 Python
Python使用matplotlib绘图无法显示中文问题的解决方法
2018/03/14 Python
IntelliJ IDEA安装运行python插件方法
2018/12/10 Python
Python匿名函数/排序函数/过滤函数/映射函数/递归/二分法
2019/06/05 Python
Python编译为二进制so可执行文件实例
2019/12/23 Python
利用CSS3实现圆角的outline效果的教程
2015/06/05 HTML / CSS
详解CSS3中nth-child与nth-of-type的区别
2017/01/05 HTML / CSS
CSS3+JavaScript实现炫酷呼吸效果的示例代码
2020/06/15 HTML / CSS
Urban Outfitters英国官网:美国平价服饰品牌
2016/11/25 全球购物
ProBikeKit德国:在线公路自行车专家
2018/06/03 全球购物
开放系统互连参考模型
2016/06/29 面试题
2014年平安创建工作总结
2014/11/24 职场文书
先进单位申报材料
2014/12/25 职场文书
乌镇导游词
2015/02/02 职场文书
自荐信怎么写
2015/03/04 职场文书
签证工作证明模板
2015/06/15 职场文书
温馨祝福晨语:美丽的一天从我的问候开始
2019/11/28 职场文书
深入理解以DEBUG方式线程的底层运行原理
2021/06/21 Java/Android
Nginx本地配置SSL访问的实例教程
2022/05/30 Servers