【问题标题】:jQuery slider snippet, convert to pluginjQuery 滑块片段,转换为插件
【发布时间】:2012-05-21 00:49:41
【问题描述】:

我发现了这个很棒的小 jquery 图像滑块 sn-p:

var x = 2;
// function to switch the images.
function slideSwitch() {
          var m = 5;
           x += 1;
          if (x > m) {
            x = 1
            }

   $(".slideshow ul li:nth-child(n)").animate({opacity:0});
   $(".slideshow ul li:nth-child(" + (x) + ")").animate({opacity:1});
}

$(document).ready(function() {
      setInterval( "slideSwitch()", 5000 );
});

并且一直在尝试将其转换为插件以便能够多次使用它,但我没有任何运气......请帮助。

我对插件的尝试:(不起作用)

(function ($) {
  $.fn.slidr = function (opts) {
    var def = {
      imgs : 4,
      sid  : 'slidr'
    },
    opts = $.extend({}, def, opts);

    var nxt = 2;

    this.each(function () {

var nxt += 1;

if (nxt > opts.imgs) {
     nxt = 1;
}

$('.'+ opts.sid + ' ul li:nth-child(n)').animate({opacity:0});
$('.'+ opts.sid + '  ul li:nth-child(' + (nxt) + ')').animate({opacity:1});

    });
    return this;
  }
})(jQuery);


$(document).ready(function() {

setInterval( $('#content').slidr(), 2000);

});

原始sn-p:http://jsfiddle.net/8T7nX 插件尝试不起作用:http://jsfiddle.net/8T7nX/1

【问题讨论】:

  • 一个 JSFiddle 示例在这里会有所帮助。
  • 这个setInterval( $('#content').slidr(), 2000) 不起作用。你需要一个匿名函数setInterval( function() { $('#content').slidr() }, 2000)
  • @Will Demaine:原始 sn-p:jsfiddle.net/8T7nX ...插件尝试不起作用:jsfiddle.net/8T7nX/1
  • @RoKo:谢谢,但原来的 sn-p 工作正常,我只是想把它变成一个插件,以便更好地多用。

标签: jquery jquery-plugins slider


【解决方案1】:

似乎你会重新发明轮子: http://jquery.malsup.com/cycle/ 这个插件正是你所需要的!

我还调整了你的小提琴:http://jsfiddle.net/8T7nX/4/

当您只想向它添加一个时,您正在覆盖您的 nxt var(从它前面删除了 var)

(function ($) {
  $.fn.slidr = function (opts) {
    var def = {
      imgs : 4,
      sid  : 'slide'
    },
    opts = $.extend({}, def, opts);

    var nxt = 2;

    this.each(function () {

  nxt += 1;

if (nxt > opts.imgs) {
     nxt = 1;
}

$('.'+ opts.sid + ' ul li:nth-child(n)').animate({opacity:0});
$('.'+ opts.sid + '  ul li:nth-child(' + (nxt) + ')').animate({opacity:1});

    });
    return this;
  }
})(jQuery);


$(document).ready(function() {

setInterval( function() { $('#content').slidr() }, 2000);

});

【讨论】:

  • 这并不意味着没有人可以构建一些有趣/学习的东西
  • 重新发明 ...也许吧,但就像 Roko 所说的“并不意味着没有人可以构建一些有趣/学习的东西”,我正在做的,我从我发现的 sn-p 创建了一个手风琴插件不久前,我想我也会这样做,也感谢您指出我的错误。
【解决方案2】:

如果您对原始代码出错的地方感兴趣,请查看以下内容:

http://jsfiddle.net/Willyham/JmgFV/

虽然我建议重用一个更全面的解决方案,因为此代码的结构还有很多不足之处。

(function($) {
    $.fn.slidr = function(opts) {
        var def = {
            imgs: 4,
            timeout: 2000
        },
            opts = $.extend({}, def, opts);

        var nxt = 2;
        var el = $(this);

        function animate() {
            if (nxt > opts.imgs) nxt = 1;
            $('ul li:nth-child(n)', el).animate({
                opacity: 0
            });
            $('ul li:nth-child(' + (nxt) + ')', el).animate({
                opacity: 1
            });
            nxt++;
        }

        return this.each(function() {
            setInterval(animate, opts.timeout);
        });
    }
})(jQuery);


$(document).ready(function() {
    $('#n1').slidr();
    $('#n2').slidr({
        timeout: 5000
    });
});​

【讨论】:

  • 感谢您加入 setInterval
【解决方案3】:

我看不出那个脚本有什么毛病。看起来你可以做得更简单。这样你就不需要告诉插件你有多少张图片了。

;(function($){
  $.fn.slider = function(){
    return this.each(function(){
      var $li = $(this).find('li'),
          i = 1,
          slide
      $li.hide().first().show()
      slide = function(){
        $li.hide()
        if (i < $li.length) {
          $li.eq(i).fadeIn()
          i++
        } else {
          $li.first().fadeIn()
          i = 1
        }
      }
      setInterval(slide, 1000)
    })
  }
}(jQuery))

演示: http://jsfiddle.net/elclanrs/8T7nX/5/

【讨论】:

猜你喜欢
  • 2021-07-17
  • 2012-01-23
  • 2016-05-05
  • 2011-07-22
  • 1970-01-01
  • 2020-03-26
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多