0

我创建了一个 jquery 动画,当您单击隐藏的 div 从中滑动的按钮之一left: -650px;left: 0px;单击此处查看示例但是我注意到,当您单击另一个按钮以显示另一个隐藏的 div 时,前一个隐藏的 div 不会返回它的原始位置left: -650px;停留在左边:0;有谁知道我需要添加什么才能实现这一目标......?

HTML:

<div id="wrapper">
    <div class="top-block">
        <ul>
            <li><a id="one" href="#" class="proBtn">block</a>
            </li>
            <li><a id="two" href="#" class="proBtn">test</a>
            </li>
            <li><a id="three" href="#" class="proBtn">test</a>
            </li>
            <li><a id="four" href="#" class="proBtn">Lists</a>
            </li>
            <li><a href="#" class="proBtn">hello</a>
            </li>
            <li><a href="#" class="proBtn">test</a>
            </li>
        </ul>
        <!-- HOME SECTION -->
        <div id="one-bck" class="mid-block fadeInLeft" style="background:green;"></div>
        <div id="two-bck" class="mid-block fadeInLeft" style="background:red;"></div>
        <div id="three-bck" class="mid-block fadeInLeft"></div>
        <div id="four-bck" class="mid-block fadeInLeft"></div>
    </div>
</div>

JS

$(document).ready(function () {
    $('.proBtn').click(function () {
        $('li').removeClass('active');
        $('li a').removeClass('blue');
        $(this).parent("li").addClass('active');
        $(this).addClass('blue');
    });

    $('#one').click(function () {
        $('#two-bck').animate({
            left: '-650px;',
            opacity: 0
        }, 500).removeClass('animated');
        $('#three-bck').animate({
            left: '-650px;',
            opacity: 0
        }, 500).removeClass('animated');
        $('#four-bck').animate({
            left: '-650px;',
            opacity: 0
        }, 500).removeClass('animated');
        $('#one-bck').addClass('animated').animate({
            left: '0px',
            opacity: 1
        }, 500);
    });

    $('#two').click(function () {
        $('#one-bck').animate({
            left: '-650px;',
            opacity: 0
        }, 500).removeClass('animated');
        $('#three-bck').animate({
            left: '-650px;',
            opacity: 0
        }, 500).removeClass('animated');
        $('#four-bck').animate({
            left: '-650px;',
            opacity: 0
        }, 1500).removeClass('animated');
        $('#two-bck').addClass('animated').animate({
            left: '0px',
            opacity: 1
        }, 500);;
    });

    $('#three').click(function () {
        $('#one-bck').animate({
            left: '-650px;',
            opacity: 0
        }, 500).removeClass('animated');
        $('#two-bck').animate({
            left: '-650px;',
            opacity: 0
        }, 500).removeClass('animated');
        $('#four-bck').animate({
            left: '-650px;',
            opacity: 0
        }, 500).removeClass('animated');
        $('#three-bck').addClass('animated').animate({
            left: '-650px;',
            opacity: 0
        }, 500);
    });

});
4

2 回答 2

0

除了修复 css 和 removeClass 问题之外,您真的可以尝试简化这一点。如果不将事件处理程序绑定到每个 id,可能会让人头疼,因为它们都做同样的事情。您可以向锚标记添加数据属性,即data-target="#three-bck"选择它们进行动画处理。它可以全部放在一个事件处理程序中。

HTML:

      <ul>
        <li><a id="one" href="#" class="proBtn" data-target="#one-bck">block</a>

        </li>
        <li><a id="two" href="#" class="proBtn" data-target="#two-bck">test</a>

        </li>
        <li><a id="three" href="#" class="proBtn" data-target="#three-bck">test</a>

        </li>
        <li><a id="four" href="#" class="proBtn" data-target="#four-bck">Lists</a>

        </li>
        <li><a href="#" class="proBtn">hello</a>

        </li>
        <li><a href="#" class="proBtn">test</a>

        </li>
    </ul>

JS:

$(document).ready(function () {
    $('.proBtn').click(function () {
        $('li').removeClass('active').find('a').removeClass('blue'); //chain it
        var $this = $(this); //cache this first
        $this.addClass('blue').closest("li").addClass('active');

        $('.mid-block.animated').animate({ //start animate for reverse in case if there is anything already selected
            left: '-650px',
            opacity: 0
        }, 500, function(){
           $(this).removeClass('animated'); //once done remove its class
        }).promise().done(function () { //once this is done start animating your target by selecting the target element from its own data attribute
            $($this.data('target')).addClass('animated').stop(true, true).animate({
                left: '0px',
                opacity: 1
            }, 500);
        });
    });
});

小提琴

于 2013-09-21T17:09:35.283 回答
0

删除 'px' 后的分号left:'-650px;'

应该只是left:'-650px'

于 2013-09-21T16:45:03.460 回答