2

我正在构建一个 jQuery 驱动的推子,我想在一个页面上有多个实例。

目标:我正在尝试在没有插件帮助的情况下从头开始构建它。

我试过的
1. 使用这个关键字/选择器来创建所需的变量,但没有得到我需要的。
2. 我还设置了一个带有争论的函数,以找出 jQuery 似乎不会以这种方式传递变量的困难方式。无论如何,大多数课程都是最好的学习方法。

问题:我在本地进行了几次迭代,结果不同。我目前遇到了太多递归错误,尽管它确实以某种形式在本地工作。

想法:我的下一步将是对象表示法,但害怕用另一个错误的解决方案旋转我的轮子。

感谢大家

JSFiddle:http: //jsfiddle.net/UQmc3/

遵循
HTML的代码示例:

<div>
    <div class="w-fader">
        <ul class="w-slides">
            <li class="current"><img src="http://littleredplanedesign.com/labs/rotator/rotator-images/fader-img-1.png" /></li>
            <li class="next"><img src="http://littleredplanedesign.com/labs/rotator/rotator-images/fader-img-2.png" /></li>
            <li><img src="http://littleredplanedesign.com/labs/rotator/rotator-images/fader-img-3.png" /></li>      
        </ul>
    </div>

    <div class="w-fader">
        <ul class="w-slides">
            <li class="current"><img src="http://littleredplanedesign.com/labs/rotator/rotator-images/fader-img-1.png" /></li>
            <li class="next"><img src="http://littleredplanedesign.com/labs/rotator/rotator-images/fader-img-2.png" /></li>
            <li><img src="http://littleredplanedesign.com/labs/rotator/rotator-images/fader-img-3.png" /></li>
            <li><img src="http://littleredplanedesign.com/labs/rotator/rotator-images/fader-img-1.png" /></li>
            <li><img src="http://littleredplanedesign.com/labs/rotator/rotator-images/fader-img-2.png" /></li>
            <li><img src="http://littleredplanedesign.com/labs/rotator/rotator-images/fader-img-3.png" /></li>              
        </ul>
    </div>

</div>

jQuery/js

var speed = 2000;
var name = 'fader';
var num = 1;

function setupUnique(){
$('.w-slides').each(function(){
    var panelId = $(this).attr('id');
    //alert(panelId);
    var nextItem = $(this).find('li.next');
    //alert(nextItem);
    var currentItem = $(this).find('li.current');
    var wrapHelperItem = $(this).find('.w-slides li.current+li');   
    var firstSwapItem = $(this).find('li:first');   
    var lastSwapItem = $(this).find('li:last'); 

    fadePanels();
    function fadePanels(){
    $('#'+panelId).each(function(){
        switcher();
        function switcher(){
        $(nextItem).animate({opacity:1}, speed, 'linear', function()
            {
                $(nextItem).removeClass('current');
                $(this).addClass('current');
                $(nextItem).addClass('next');
                $(this).removeClass('next');
                $(nextItem).css('opacity',0);
            });
        checker();
        }       

        //checker();

        function checker(){
        if($(lastSwapItem).hasClass('current'))
        {
            $(firstSwapItem).addClass('next');
            $(firstSwapItem).addClass('next').css('opacity',0);
        }
        else if($(firstSwapItem).hasClass('next') && $(lastSwapItem).hasClass('current'))
        {
            $(firstSwapItem).addClass('current');       
            $(wrapHelperItem).addClass('next');
            $(firstSwapItem).removeClass('next');
        }
    }

    setupUnique();
    });
    }
});
}

function makeUnique(){
$('.w-slides').each(function(){
    $(this).attr('id',name+num);
    num++;
    setupUnique();
    });
}

makeUnique();
4

2 回答 2

0

鉴于您要完成的工作,创建自己的 jQuery 插件可能是最好的方法。但是,使用您的一般方法和您的开始,这里有一些东西可以让您朝着正确的方向前进:

http://jsfiddle.net/UQmc3/2/

$(document).ready(function() {
    // Wait for the document to be ready
    makeUnique();
});

function makeUnique() {
    var name = 'fader'; // scope these variables to the function
    var num = 1;
    $('.w-slides').each(function() {
        $(this).attr('id', name + num);
        num++;
    });
    setupUnique();
}

function setupUnique() {
    var animateSpeed = 2000; // scope these variables to the function
    var timerInterval = 4000; // this is the timer interval

    $('.w-slides').each(function(i) {
            var $this = $(this); //refers to the current .w-slides in the .each()
            var $nextItem = $this.find('li.next');
            var $currentItem = $this.find('li.current');
            var $firstSwapItem = $this.find('li:first');
            var $lastSwapItem = $this.find('li:last');
            var $wrapHelperItem = $this.find('li.current + li');
            // create new instance of SlideSwaper
            var slideSwap = new SlideSwaper($this, $nextItem, $currentItem, 
                                            $firstSwapItem, $lastSwapItem, 
                                            $wrapHelperItem);
    });

    function SlideSwaper($this, $nextItem, $currentItem, $firstSwapItem, 
                         $lastSwapItem, $wrapHelperItem) {
        this.$this = $this;
        this.$nextItem = $nextItem;
        this.$currentItem = $currentItem;
        this.$firstSwapItem = $firstSwapItem;
        this.$lastSwapItem = $lastSwapItem;
        this.$wrapHelperItem = $wrapHelperItem;

        var fadeTimer = setInterval(function() {
            // Hide/Show the appropriate <li>
            // Everything commented out below should be combined, 
            // don't hide/show your li and then check after that to 
            // see if everything is ok - do your check first, and 
            // then as you check show/hide the approriate <li>
            /*$nextItem.animate({opacity:1}, speed, 'linear', function() {
                $currentItem.removeClass('current').css('opacity',0);
                        $nextItem.addClass('current').removeClass('next');
                        $wrapHelperItem.addClass('next').css('opacity',0);'
                        console.log('checker');
                        checker();
            });

            if($lastSwapItem.hasClass('current')) {
                        $firstSwapItem.addClass('next').css('opacity',0);
            } else if($firstSwapItem.hasClass('next') && $lastSwapItem.hasClass('current')) {
                        $firstSwapItem.addClass('current').removeClass('next');     
                        $wrapHelperItem.addClass('next');
            }*/
        }, timerInterval);

    }

}

基本上,您需要创建对象的新实例以保持对适当引用的<ul>完整性。还:

  • 在文件准备好之前,这一切都不应该开始
  • $(this)多次引用时使用变量来引用
  • 您没有计时器功能(我假设您希望幻灯片每秒钟更改一次x
  • 变量应该限定在它们的函数范围内(在你的情况下,它们都可以而且应该是)
  • 您的某些函数名称不明确,可以改进(例如,makeUnique可能是initializeSwapper等)。

你必须做一些工作才能让幻灯片改变你想要的方式。现在,匿名函数设置为fadeTimer每 4 秒执行一次,此时您可以根据需要更改幻灯片。

如果您不使用Firebug,我建议您检查一下

于 2013-04-29T17:18:44.453 回答
0

我可能读错了你的大括号——但你是setupUnique()setupUnique内部打电话吗?

那会引起问题。

于 2013-04-29T14:43:31.017 回答