---
title: "jQuery自定义动画"
author: "Ydoing"
date: "2015-08-23T17:32:00Z"
tags: []
slug: "jquery"
draft: false
lastmod: "2015-10-11T10:04:35Z"
id: 2248198
url: "https://ydoing.iteye.com/blog/2248198"
---

## 语法结构

```
animate(params, speed, callback);
```

- params： 包含样式属性的映射。
- speed：速度参数，可选。
- callback：在动画完成执行的函数，可选。

## 实例

要实现一个功能，点击元素时，每次元素在300ms内向右移动100px。

点击前：  
![这里写图片描述](http://img.blog.csdn.net/20150823173006378)

点击后：  
![这里写图片描述](http://img.blog.csdn.net/20150823173024288)

jQuery：

```
                $this.children('li').click(function(){
                    $(this).animate({left: "+=100px"},300);
                    console.log("ccc");

                }); 
```

html：

```
                    <ul class="tabs">
                        <li class="tabs-li">
                            <a href="#" class="tab-li-a">欢迎使用</a>
                        </li>
                    </ul> 
```

css:

```
.tabs li {
    position: relative;
    width: 100px;
}
.tabs .tab-li-a {
    display: block;
    margin: 1px;
    float: left;
    text-decoration: none;
    color: white;
    background-color: #0099cc;
    padding: 5px 1em;
    border: 1px solid #a1acb8;
}
```

&lt;script type="text/javascript"&gt; $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\\n').length; var $numbering = $('&lt;ul/&gt;').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i &lt;= lines; i++) { $numbering.append($('&lt;li/&gt;').text(i)); }; $numbering.fadeIn(1700); }); }); &lt;/script&gt;

版权声明：本文为博主原创文章，未经博主允许不得转载。