如何在addClass之前制作幻灯片和淡入淡出效果

时间:2014-02-16 06:37:01

标签: javascript jquery

我正在尝试基于jQuery创建幻灯片和淡入淡出效果。在添加函数addClass之前,一切正常。但添加此类后,幻灯片和淡入淡出效果消失。

当我在addClass('hide')之后添加FadeSlideToggle.call(box)时,javascript效果会消失。

有人可以帮助我吗?这是代码:

<html>
<head>
    <style type="text/css">
        .box {
            background: red;
            width: 400px;
            position: relative;
            margin-left: auto;
            margin-right: auto;
        }

        .hide {
            display: none;
        }

    </style>
</head>
<body>
    <div class="box">
        <h2>Some Content Here</h2>
        <p>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </p>
    </div>  
    <p><button>FadeSlideToggle</button></p>


    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">

            var box = $('div.box');

            function FadeSlideToggle(){

                return $(this).animate({
                    opacity: 'toggle',
                    'height': 'toggle'  
                }, 2000);
            };

            $('button').on('click', function(){
                FadeSlideToggle.call(box);
            });

    </script>
</body> 
</html><html>
<head>
    <style type="text/css">
        .box {
            background: red;
            width: 400px;
            position: relative;
            margin-left: auto;
            margin-right: auto;
        }

        .hide {
            display: none;
        }

    </style>
</head>
<body>
    <div class="box">
        <h2>Some Content Here</h2>
        <p>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </p>
    </div>  
    <p><button>FadeSlideToggle</button></p>


    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">

            var box = $('div.box');

            function FadeSlideToggle(){

                return $(this).animate({
                    opacity: 'toggle',
                    'height': 'toggle'  
                }, 2000);
            };

            $('button').on('click', function(){
                FadeSlideToggle.call(box);
            });

    </script>
</body> 
</html>

2 个答案:

答案 0 :(得分:0)

使用完整回调

var box = $('div.box');

function FadeSlideToggle(callback){

    return $(this).animate({
        opacity: 'toggle',
        'height': 'toggle'  
    }, 2000, callback);
};

$('button').on('click', function(){
    FadeSlideToggle.call(box, function() {
         //do something you want
    });
});

答案 1 :(得分:0)

感谢LowerClassOverflowian

其中一个解决方案是使用jQuery UI。

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <script type="text/javascript">

            var box = $('div.box');

            function FadeSlideToggle(speed){

                return $(this).animate({
                    'opacity': '0',
                    'height': 0 
                }, speed || 2000);
            };

            $('button').on('click', function(){
                FadeSlideToggle.call(box, 5000).addClass('hide', {
                    queue: true
                });
            });

    </script>
相关问题