• Jquery库自带的动画效果方法记录


    1.显示和隐藏hide()和show()

    <script type="text/javascript">
                $(function() {
                    $("input:first").click(function() {
                        $("p").hide(); //隐藏
                    });
                    $("input:last").click(function() {
                        $("p").show(); //显示
                    });
                });
     </script>
            <input type="button" value="Hide">
            <input type="button" value="Show">

    带参数的hide()和show()

    show(duration,[callback]);

    hide(duration,[callback]);

    其中,duration表示动画执行时间的长短,可以表示速度的字符串,包括slow,normal,fast.也可以是表示时间的整数(毫秒)。callback是可选的回调函数。在动画完成之后执行

    <script type="text/javascript">
                $(function() {
                    $("input:first").click(function() {
                        $("p").hide(300); //隐藏
                    });
                    $("input:last").click(function() {
                        $("p").show(500); //显示
                    });
                });
            </script>

    2.显隐fadeIn()和fadeOut()

    动画效果显隐,他们的动画效果类似褪色,语法与slow()和hide()完全相同。

    fadeOut()的相关参数:

    speed,可选。规定元素从当前透明度到指定透明度的速度。可能的值:毫秒 (比如 1500)"slow";"normal";"fast".

    opacity,必需。规定要淡入或淡出的透明度。必须是介于 0.00 与 1.00 之间的数字。
    callback ,可选。fadeTo 函数执行完之后,要执行的函数。除非设置了 speed 参数,否则不能设置该参数

    3.fadeTo()

    fadeTo() 方法将被选元素的不透明度逐渐地改变为指定的值

    <script type="text/javascript">
                $(function() {
                    $("input:eq(0)").click(function() {
                        $("img").fadeOut(1000);
                    });
                    $("input:eq(1)").click(function() {
                        $("img").fadeIn(1000);
                    });
                    $("input:eq(2)").click(function() {
                        $("img").fadeTo(1000, 0.5);
                    });
                    $("input:eq(3)").click(function() {
                        $("img").fadeTo(1000, 0);
                    });
                });
            </script>
    <input type="button" value="FadeOut">
    <input type="button" value="FadeIn">
    <input type="button" value="FadeTo 0.5">
    <input type="button" value="FadeTo 0">

    4.幻灯片效果slideUp()和slideDown()

    slideUp()和slideDown()来模拟PPT中的类似幻灯片拉帘效果

    <script type="text/javascript">
                $(function() {
                    $("input:eq(0)").click(function() {
                        $("div").add("img").slideUp(1000);
                    });
                    $("input:eq(1)").click(function() {
                        $("div").add("img").slideDown(1000);
                    });
                    $("input:eq(2)").click(function() {
                        $("div").add("img").hide(1000);
                    });
                    $("input:eq(3)").click(function() {
                        $("div").add("img").show(1000);
                    });
                });
            </script>  
        <input type="button" value="SlideUp">
        <input type="button" value="SlideDown">
      <div></div><img src="04.jpg">

    4.自定义动画
    考虑到框架的通用性及代码文件的大小,jQuery不能涵盖所有的动画效果,但它提供了animate()方法,能够使开发者自定义动画。本节主要通过介绍animate()方法的两种形式及应用。
    animate()方法给开发者很大的空间。它一共有两种形式。第一种形式比较常用。用法如下
    animate(params,[duration],[easing],[callback])
    其中params为希望进行变幻的css属性列表,以及希望变化到的最终值,duration为可选项,与show()/hide()的参数含义完全相同。easing为可选参数,通常供动画插件使用。 用来控制节奏的变化过程。jQuery中只提供了linear和swing两个值.callback为可选的回调函数。在动画完成后触发。
    需要注意。params中的变量遵循camel命名方式。例如paddingLeft不能写成padding-left.另外,params只能是css中用数值表示的属性。例如width.top.opacity等
    像backgroundColor这样的属性不被animate支持。

    <style>
                div {
                    background-color: #FFFF00;
                    height: 40px;
                     80px;
                    border: 1px solid #000000;
                    margin-top: 5px;
                    padding: 5px;
                    text-align: center;
                }
            </style>
            <script type="text/javascript">
                $(function() {
                    $("button").click(function() {
                        $("#block").animate({
                            opacity: "0.5",
                             "80%",
                            height: "100px",
                            borderWidth: "5px",
                            fontSize: "30px",
                            marginTop: "40px",
                            marginLeft: "20px"
                        }, 2000);
                    });
                });
            </script>
            <button id="go">Go>></button>
            <div id="block">动画!</div>

    在params中,jQuery还可以用“+=”或者"-="来表示相对变化

    <style>
                div {
                    background-color: #FFFF00;
                    height: 40px;
                     80px;
                    border: 1px solid #000000;
                    margin-top: 5px;
                    padding: 5px;
                    text-align: center;
                    position: absolute;
                }
            </style>
            <script type="text/javascript">
                $(function() {
                    $("button:first").click(function() {
                        $("#block").animate({
                            left: "-=80px" //相对左移
                        }, 300);
                    });
                    $("button:last").click(function() {
                        $("#block").animate({
                            left: "+=80px" //相对右移
                        }, 300);
                    });
                });
            </script>
            <button >Go>></button>
            <button >Go>></button>
            <div id="block">动画!</div>

    先将div进行绝对定位,然后使用animate()中的-=和+=分别实现相对左移和相对右移。
    animate()方法还有另外一种形式,如下所示:
    animate(params,options)
    其中,params与第一种形式完全相同,options为动画可选参数列表,主要包括duration,esaing,callback,queue等,其中duration.easing.callback与第一种形式完全一样,queue为布尔值,表示当有多个 animate()组成jQuery时,当前animate()紧接这下一个animate(),是按顺序执行(true)还是同时触发false.
    animate()第二种用法:

        <style>
                div {
                    background-color: #FFFF22;
                     100px;
                    text-align: center;
                    border: 2px solid #000000;
                    margin: 3px;
                    font-size: 13px;
                    font-family: Arial, Helvetica, sans-serif;
                }
                input {
                    border: 1px solid #000033;
                }
            </style>
            <script type="text/javascript">
                $(function() {
                    $("input:eq(0)").click(function() {
                        //第一个animate与第二个animate同时执行,然后再执行第三个
                        $("#block1").animate({
                                 "90%"
                            }, {
                                queue: false,
                                duration: 1500
                            })
                            .animate({
                                fontSize: "24px"
                            }, 1000)
                            .animate({
                                borderRightWidth: "20px"
                            }, 1000);
                    });
                    $("input:eq(1)").click(function() {
                        //依次执行三个animate
                        $("#block2").animate({
                                 "90%"
                            }, 1500)
                            .animate({
                                fontSize: "24px"
                            }, 1000)
                            .animate({
                                borderRightWidth: "20px"
                            }, 1000);
                    });
                    $("input:eq(2)").click(function() {
                        $("input:eq(0)").click();
                        $("input:eq(1)").click();
                    });
                    $("input:eq(3)").click(function() {
                        //恢复默认设置
                        $("div").css({
                             "",
                            fontSize: "",
                            borderWidth: ""
                        });
                    });
                });
            </script>
            <input type="button" id="go1" value="Block1动画">
            <input type="button" id="go2" value="Block2动画">
            <input type="button" id="go3" value="同时动画">
            <input type="button" id="go4" value="重置">
            <div id="block1">Block1</div>
            <div id="block2">Block2</div>

    以上两个div块同时运用了三个动画效果,其中第一个div快的第一个动画添加了queue:false参数,使得前两项两个动画同时执行.

  • 相关阅读:
    Hdu 1257 最少拦截系统
    Hdu 1404 Digital Deletions
    Hdu 1079 Calendar Game
    Hdu 1158 Employment Planning(DP)
    Hdu 1116 Play on Words
    Hdu 1258 Sum It Up
    Hdu 1175 连连看(DFS)
    Hdu 3635 Dragon Balls (并查集)
    Hdu 1829 A Bug's Life
    Hdu 1181 变形课
  • 原文地址:https://www.cnblogs.com/sky-gfan/p/5139633.html
Copyright © 2020-2023  润新知