jQuery 常用技巧

来源:互联网 发布:任天堂vb算vr吗 编辑:程序博客网 时间:2024/06/08 13:23
1. 网页打开时,默认让某个指定元素获得焦点,应用场景(登录页面,让焦点在输入框内)
<body>
    <input type="text" name="name">
    <script type="text/javascript">
    $(function() {
        // 该触发器在触发事件的时候,会执行浏览器的默认行为
        $('input').trigger('focus');
        // 如果不想执行浏览器默认行为,使用 triggerHandler来取消
        $('input').triggeHandler('focus');
    });
    </script>
</body>
2.动画里面使用 callback(回调函数)来延迟css的生效时间

<head>
    <style type="text/css">
    #panel {
        position: relative;
        width: 100px;
        height: 100px;
        border: 1px solid #0050D0;
        background: #96E555;
        cursor: pointer;
    }
    </style>
</head>
<body>
    <div id="panel"></div>
    <script type="text/javascript">
    $(function(){
        $('#panel').css('opacity', '0.5');
        $('#panel').click(function(){
            $(this).animate({left: "+=100px", opacity: '1'}, 2000)
            .animate({height: '+=100px'}, 2000)
            .animate({width: '+=100px'}, 2000, function(){
                $(this).css('border', '5px solid blue');
            });
        })
    })
    </script>
</body>

0 0
原创粉丝点击