【问题标题】:How can I make js that needs manual input dynamic?如何使需要手动输入的js动态化?
【发布时间】:2010-05-12 16:13:02
【问题描述】:

我真的不知道怎么问这个,所以我在这里写了脚本: http://jsbin.com/acaxi/edit
这很简单,我正在尝试创建滑动面板。

我知道有很多脚本做得很好,说实话太多了。

如果有人认为你可以推荐一个插件而不是我的脚本,那么请分享!

【问题讨论】:

  • 抱歉,您的问题到底是什么?您的演示似乎运行良好。

标签: jquery dynamic slider panels


【解决方案1】:

我仍然不确定您的问题是什么,但我对您的代码进行了一些修改,使其适用于任意数量的提要面板 (updated demo)。

$(document).ready(function(){

  var feeds = $('#feeds div'),
      numFeeds = feeds.length;

  feeds
    .click(function(){
      $(this)
        .animate({"margin-left": "-200px", opacity: 0}, "fast")
        .animate({"margin-left": "200px"}, "fast");
      // add 2 since the id isn't zero based
      var next = ( $(this).index() + 2 > numFeeds ) ? 1 : $(this).index() + 2;
      $('div#feed' + next).animate({"margin-left": 0, opacity: 1}, "fast")
    })
    .nextAll().css({ 'margin-left' : '200px', opacity : 0 });

});

要动态添加提要,您需要将点击功能附加到每个添加的新提要或使用 jQuery .live() 事件处理程序。我选择了以前的方法。这是updated demo,以及代码:

$(document).ready(function(){

  var feeds = $('#feeds .feeds'),
      numFeeds = feeds.length;

  // initialize
  feeds
   .click(function(){ animateFeed(this, numFeeds); })
   .nextAll().css({ 'margin-left' : '200px', opacity : 0 });

  // add another feed
  $('.addFeed').click(function(){
   $('<div id="feed' + ( numFeeds++ +1 ) + '" class="feeds">' + numFeeds +'</div>')
    .click(function(){ animateFeed(this, numFeeds); })
    .css({ 'margin-left' : '200px', opacity : 0 })
    .appendTo(feeds.parent());
   $('#num').text(numFeeds);
  })

});

// animate feed panel
function animateFeed(el, num){
 var indx = $(el).index(),
     next = ( indx + 1 ) % num;
 $('.feeds').removeClass('active');
 $(el)
  .animate({ marginLeft : '-200px', opacity: 0}, 'fast')
  .animate({ marginLeft : '200px'}, 'fast' );
 $('.feeds:eq(' + next + ')').animate({ marginLeft : 0, opacity : 1}, 'fast', function(){ $(this).addClass('active') } );
}

【讨论】:

  • 这正是我一直在寻找的,我的问题没有以最好的方式写出来抱歉。我希望能够添加尽可能多的提要 div,而不必为每个新的手动创建点击功能。因此,如果我在页面上有一个按钮来创建一个名为 feed(n) 的新 div,那么它会自动使其成为滑动功能的一部分。不知道这是否更有意义?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 2022-08-16
  • 2019-01-02
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多