jQuery无缝滚动轮播插件

轩陌

分类: 解决问题 5598 4

前言

在平时写特效的时候,喜欢把一些东西封装成一个插件,方便下次使用,不喜欢在特效上边用插件,除非是特别复杂的特效,所以就自己写一下

CDN地址:https://upyun.xuanmo.xin/js/jquery.seamlessBanner.min.js

 

布局说明、结构代码示例

   banner图片列表:使用浮动,js会自动计算图片列表容器的宽度

   banner按钮容器:使用定位设置left值为50%;js自动计算像左边的偏移量

   示例代码


<!--
    html结构说明,就拿下边的结构解释:
        .banner为整个banner区域的容器
        .banner-wrap为图片列表容器,不需要设置宽度,给子级元素设置好浮动js计算宽度
        .banner-list为图片列表
        .banner-btn-wrap按钮切换的容器
        .banner-arrow-left左边切换按钮
        .banner-arrow-right右边切换按钮
-->
<div id="banenr" class="banner">
    <ul class="banner-wrap clearfix">
        <li class="banner-list">
            <img src="http://file.xuanmo.xin/TT.jpg" width="100%" height="100%" alt="">
        </li>
        <li class="banner-list">
            <img src="http://file.xuanmo.xin/T1-lg.jpg" width="100%" height="100%" alt="">
        </li>
        <li class="banner-list">
            <img src="http://file.xuanmo.xin/T2-lg.jpg" width="100%" height="100%" alt="">
        </li>
    </ul>
    <!-- banner按钮容器 -->
    <ul class="banner-btn-wrap"></ul>
    <!-- banner切换箭头 -->
    <i class="arrow-btn banner-arrow-left">&lt;</i>
    <i class="arrow-btn banner-arrow-right">&gt;</i>
</div>

 

js封装插件部分


// 默认参数
var init = {
  // 是否自动切换
  autoBanner: false,
  // 是否开启移动端滑动切换
  touchEvent: true,
  // 自动轮播间隔时间
  time: 5000,
  // 每张图片过渡时间
  transition: 1000,
  // 运动曲线
  easing: 'jswing',
  // 上一张按钮
  arrowLeftBtn(el) {
    return el.siblings('.banner-arrow-left')
  },
  // 下一张按钮
  arrowRightBtn(el) {
    return el.siblings('.banner-arrow-right')
  }
}

 


(function ($) {
  function Banner(el, options) {
    this.bannerWrap = el;
    this.bannerList = el.children();
    this.bannerBtnWrap = options.bannerBtnWrap || el.siblings('.banner-btn-wrap');
    this.bannerBtn = this.bannerBtnWrap.children();
    this.arrowLeftBtn = typeof options.arrowLeftBtn == 'function' ? options.arrowLeftBtn.call(this, el) : options.arrowLeftBtn;
    this.arrowRightBtn = typeof options.arrowRightBtn == 'function' ? options.arrowRightBtn.call(this, el) : options.arrowRightBtn;
    this.nTime = options.time;
    this.nTransitionTime = options.transition;
    this.nWindowWidth = options.wrapWidth || el.parent().width();
    this.bAutoBanner = options.autoBanner;
    this.bTouchEvent = options.touchEvent;
    this.n = 1;
    this.x = 0;
    this._x = 0;
    this.oTimer = null;
    this.easing = options.easing;
  }

  Banner.prototype = {
    constrctor: Banner,

    // 初始化轮播
    init: function init() {
      var This = this;

      // 生成banner按钮
      This.bannerList.each(function () {
        $(this).width(This.nWindowWidth);
        This.bannerBtnWrap.append('
  • '); }); // 拷贝第一张和最后一张分别到第一张前面和最后一张后边 This.bannerList.first().before(This.bannerList.last().clone().addClass('clone')); This.bannerList.last().after(This.bannerList.first().clone().addClass('clone')); // 重新获取banner This.bannerList = This.bannerWrap.children(); // 获取banner按钮 This.bannerBtn = $('li', This.bannerBtnWrap); // banner盒子计算宽度 This.bannerWrap.css({ width: This.bannerList.length * This.nWindowWidth + 'px', marginLeft: This.n * -This.nWindowWidth + 'px' }); // banner按钮盒子计算宽度 This.bannerBtnWrap.css({ width: This.bannerBtn.length * This.bannerBtn.outerWidth(true) + 'px', marginLeft: function marginLeft() { return $(this).width() / -2 + 'px'; } }).children('li:eq(0)').addClass('on'); // 是否自动轮播 This.bAutoBanner && This.autoBanner(this); This.resize(this).move(this).stop(this); }, // 窗口发生改变改变轮播宽度 resize: function resize(This) { $(window).on('resize', function () { This.nWindowWidth = This.bannerWrap.parent().width(); This.bannerList.each(function () { $(this).width(This.nWindowWidth); }); This.bannerWrap.css({ width: This.bannerList.length * This.nWindowWidth + 'px', marginLeft: This.n * -This.nWindowWidth + 'px' }); }); return This; }, // banner切换 move: function move(This) { // 按钮切换 This.bannerBtn.mouseenter(function () { if (!This.bannerWrap.is(':animated')) { This.n = $(this).index() + 1; This.bannerBtn.eq(This.n - 1).addClass('on').siblings().removeClass('on'); This.bannerWrap.animate({ marginLeft: This.n * -This.nWindowWidth + 'px' }, { duration: This.nTransitionTime, easing: This.easing }); } }); // 向左切换 This.arrowLeftBtn.click(function () { if (!This.bannerWrap.is(':animated')) { This.n--; if (This.n < 0) { This.n = This.bannerList.length - 3; This.bannerWrap.css('marginLeft', (This.bannerList.length - 2) * -This.nWindowWidth + 'px'); } This.bannerBtn.eq(This.n - 1).addClass('on').siblings().removeClass('on'); This.bannerWrap.animate({ marginLeft: This.n * -This.nWindowWidth + 'px' }, { duration: This.nTransitionTime, easing: This.easing }); } }); // 向右切换 This.arrowRightBtn.click(function () { if (!This.bannerWrap.is(':animated')) { This.n++; if (This.n >= This.bannerList.length - 1) { This.n = 1; This.bannerWrap.css('marginLeft', (This.n - 1) * -This.nWindowWidth + 'px'); } This.bannerBtn.eq(This.n - 1).addClass('on').siblings().removeClass('on'); This.bannerWrap.animate({ marginLeft: This.n * -This.nWindowWidth + 'px' }, { duration: This.nTransitionTime, easing: This.easing }); } }); // 移动端无缝滚动 This.bTouchEvent && This.bannerWrap.on('touchstart', function (e) { This.x = e.originalEvent.touches[0].pageX; }).on('touchmove', function (e) { // e.preventDefault(); This._x = e.originalEvent.touches[0].pageX; var differ = This.x - This._x > 0 ? 1 : 0; if (differ) { // 向左滑动 if (!$(this).is(':animated')) { This.n++; if (This.n >= This.bannerList.length - 1) { This.n = 1; $(this).css('marginLeft', (This.n - 1) * -This.nWindowWidth + 'px'); } This.bannerBtn.eq(This.n - 1).addClass('on').siblings().removeClass('on'); $(this).animate({ marginLeft: This.n * -This.nWindowWidth + 'px' }, { duration: This.nTransitionTime, easing: This.easing }); } } else { // 向右滑动 if (!$(this).is(':animated')) { This.n--; if (This.n < 0) { This.n = This.bannerList.length - 3; $(this).css('marginLeft', (This.bannerList.length - 2) * -This.nWindowWidth + 'px'); } This.bannerBtn.eq(This.n - 1).addClass('on').siblings().removeClass('on'); $(this).animate({ marginLeft: This.n * -This.nWindowWidth + 'px' }, { duration: This.nTransitionTime, easing: This.easing }); } } }); return this; }, // 自动轮播 autoBanner: function autoBanner(This) { This.oTimer = setInterval(function () { This.arrowRightBtn.click(); }, This.nTime); return this; }, // 鼠标移入停止自动轮播 stop: function stop(This) { This.bannerWrap.parent().hover(function () { clearInterval(This.oTimer); }, function () { This.bAutoBanner && This.autoBanner(This); }); } // 默认参数 };var init = { autoBanner: false, touchEvent: true, time: 5000, transition: 1000, easing: 'jswing', arrowLeftBtn: function arrowLeftBtn(el) { return el.siblings('.banner-arrow-left'); }, arrowRightBtn: function arrowRightBtn(el) { return el.siblings('.banner-arrow-right'); } }; $.fn.seamlessBanner = function (options) { if (this.children().length > 1) new Banner(this, $.extend(init, options)).init(); }; $.easing['jswing'] = $.easing['swing']; $.extend($.easing, { def: 'easeOutQuad', swing: function swing(x, t, b, c, d) { return $.easing[$.easing.def](x, t, b, c, d); }, easeInQuad: function easeInQuad(x, t, b, c, d) { return c * (t /= d) * t + b; }, easeOutQuad: function easeOutQuad(x, t, b, c, d) { return -c * (t /= d) * (t - 2) + b; }, easeInOutQuad: function easeInOutQuad(x, t, b, c, d) { if ((t /= d / 2) < 1) return c / 2 * t * t + b; return -c / 2 * (--t * (t - 2) - 1) + b; }, easeInCubic: function easeInCubic(x, t, b, c, d) { return c * (t /= d) * t * t + b; }, easeOutCubic: function easeOutCubic(x, t, b, c, d) { return c * ((t = t / d - 1) * t * t + 1) + b; }, easeInOutCubic: function easeInOutCubic(x, t, b, c, d) { if ((t /= d / 2) < 1) return c / 2 * t * t * t + b; return c / 2 * ((t -= 2) * t * t + 2) + b; }, easeInQuart: function easeInQuart(x, t, b, c, d) { return c * (t /= d) * t * t * t + b; }, easeOutQuart: function easeOutQuart(x, t, b, c, d) { return -c * ((t = t / d - 1) * t * t * t - 1) + b; }, easeInOutQuart: function easeInOutQuart(x, t, b, c, d) { if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b; return -c / 2 * ((t -= 2) * t * t * t - 2) + b; }, easeInQuint: function easeInQuint(x, t, b, c, d) { return c * (t /= d) * t * t * t * t + b; }, easeOutQuint: function easeOutQuint(x, t, b, c, d) { return c * ((t = t / d - 1) * t * t * t * t + 1) + b; }, easeInOutQuint: function easeInOutQuint(x, t, b, c, d) { if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b; return c / 2 * ((t -= 2) * t * t * t * t + 2) + b; }, easeInSine: function easeInSine(x, t, b, c, d) { return -c * Math.cos(t / d * (Math.PI / 2)) + c + b; }, easeOutSine: function easeOutSine(x, t, b, c, d) { return c * Math.sin(t / d * (Math.PI / 2)) + b; }, easeInOutSine: function easeInOutSine(x, t, b, c, d) { return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b; }, easeInExpo: function easeInExpo(x, t, b, c, d) { return t == 0 ? b : c * Math.pow(2, 10 * (t / d - 1)) + b; }, easeOutExpo: function easeOutExpo(x, t, b, c, d) { return t == d ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b; }, easeInOutExpo: function easeInOutExpo(x, t, b, c, d) { if (t == 0) return b; if (t == d) return b + c; if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b; return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b; }, easeInCirc: function easeInCirc(x, t, b, c, d) { return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b; }, easeOutCirc: function easeOutCirc(x, t, b, c, d) { return c * Math.sqrt(1 - (t = t / d - 1) * t) + b; }, easeInOutCirc: function easeInOutCirc(x, t, b, c, d) { if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b; return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b; }, easeInElastic: function easeInElastic(x, t, b, c, d) { var s = 1.70158; var p = 0; var a = c; if (t == 0) return b; if ((t /= d) == 1) return b + c; if (!p) p = d * .3; if (a < Math.abs(c)) { a = c; var s = p / 4; } else var s = p / (2 * Math.PI) * Math.asin(c / a); return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b; }, easeOutElastic: function easeOutElastic(x, t, b, c, d) { var s = 1.70158; var p = 0; var a = c; if (t == 0) return b; if ((t /= d) == 1) return b + c; if (!p) p = d * .3; if (a < Math.abs(c)) { a = c; var s = p / 4; } else var s = p / (2 * Math.PI) * Math.asin(c / a); return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b; }, easeInOutElastic: function easeInOutElastic(x, t, b, c, d) { var s = 1.70158; var p = 0; var a = c; if (t == 0) return b; if ((t /= d / 2) == 2) return b + c; if (!p) p = d * (.3 * 1.5); if (a < Math.abs(c)) { a = c; var s = p / 4; } else var s = p / (2 * Math.PI) * Math.asin(c / a); if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b; return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b; }, easeInBack: function easeInBack(x, t, b, c, d, s) { if (s == undefined) s = 1.70158; return c * (t /= d) * t * ((s + 1) * t - s) + b; }, easeOutBack: function easeOutBack(x, t, b, c, d, s) { if (s == undefined) s = 1.70158; return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b; }, easeInOutBack: function easeInOutBack(x, t, b, c, d, s) { if (s == undefined) s = 1.70158; if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= 1.525) + 1) * t - s)) + b; return c / 2 * ((t -= 2) * t * (((s *= 1.525) + 1) * t + s) + 2) + b; }, easeInBounce: function easeInBounce(x, t, b, c, d) { return c - $.easing.easeOutBounce(x, d - t, 0, c, d) + b; }, easeOutBounce: function easeOutBounce(x, t, b, c, d) { if ((t /= d) < 1 / 2.75) { return c * (7.5625 * t * t) + b; } else if (t < 2 / 2.75) { return c * (7.5625 * (t -= 1.5 / 2.75) * t + .75) + b; } else if (t < 2.5 / 2.75) { return c * (7.5625 * (t -= 2.25 / 2.75) * t + .9375) + b; } else { return c * (7.5625 * (t -= 2.625 / 2.75) * t + .984375) + b; } }, easeInOutBounce: function easeInOutBounce(x, t, b, c, d) { if (t < d / 2) return $.easing.easeInBounce(x, t * 2, 0, c, d) * .5 + b; return $.easing.easeOutBounce(x, t * 2 - d, 0, c, d) * .5 + c * .5 + b; } }); })(jQuery);

     

    插件调用

    
    $('.banner-wrap').seamlessBanner();
    

     

    DEMO展示
    源码下载

    • 26人 Love
    • 1人 Haha
    • 0人 Wow
    • 0人 Sad
    • 0人 Angry
    jQuery无缝滚动插件、无缝滚动

    作者简介: 轩陌

    打赏

    生命的意义在于折腾,一直努力成长中,期待梦想实现的那刻。

    共 4 条评论关于 “jQuery无缝滚动轮播插件”

    Loading...