Skip to content

Commit 4351314

Browse files
committed
jquery-easing.js实现页面锚点平滑滚动
1 parent bc0b6f1 commit 4351314

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
title: Jquery - 使用jquery-easing.js实现页面锚点平滑滚动
3+
date: 2018-08-19 08:48:03
4+
updated: 2018-08-19 08:48:03
5+
url: html-css/jquery-easing-anchor-animation
6+
categories:
7+
- 前端开发
8+
tags:
9+
- Css
10+
- Javascript
11+
- 平滑滚动
12+
---
13+
### 使用jquery-easing.js实现页面锚点平滑滚动
14+
15+
使用[jquery easing.js](http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js "jquery easing.js")可以实现多动动画效果。
16+
jquery easing.js中定义了很多种不同的动画效果,动画速度和时间的关系图可以参考此图:
17+
18+
![](https://cdn.jsdelivr.net/gh/niumoo/cdn-assets/2019/f81d0b4842589fcbd4d085cdd7cd64b1.jpg)
19+
<!-- more -->
20+
21+
22+
使用easeInOutExpo效果示例锚点的平滑滚动效果,直接上代码。
23+
24+
### 效果图
25+
26+
![平滑滚动](https://cdn.jsdelivr.net/gh/niumoo/cdn-assets/2019/4176b134e77e84d73d0fceb55b96b521.gif)
27+
<!-- more -->
28+
### HTML代码
29+
```html
30+
<!DOCTYPE html>
31+
<html lang="zh-CN">
32+
33+
<head>
34+
<meta charset="utf-8">
35+
<title>平滑滚动</title>
36+
</head>
37+
<body>
38+
<p id="p1">1</p>
39+
<p>2</p>
40+
<p>3</p>
41+
<p>4</p>
42+
<p>5</p>
43+
<p>6</p>
44+
<p>7</p>
45+
<p>8</p>
46+
<p>9</p>
47+
<p>10</p>
48+
<p>11</p>
49+
<p>12</p>
50+
<p>13</p>
51+
<p>14</p>
52+
<p>15</p>
53+
<p>16</p>
54+
<p>17</p>
55+
<p>18</p>
56+
<p>19</p>
57+
<p>20</p>
58+
<p>21</p>
59+
<p>22</p>
60+
<p>23</p>
61+
<p>24</p>
62+
<p>25</p>
63+
<p>26</p>
64+
<p>27</p>
65+
<p>28</p>
66+
<p>29</p>
67+
<p>30</p>
68+
<p>31</p>
69+
<a href="#p1">TO P1</a>
70+
71+
<script src="js/jquery.min.js"></script>
72+
<script src="js/Anchor-animation.js"></script>
73+
<script src="js/jquery-easing.js"></script>
74+
</body>
75+
</html>
76+
77+
```
78+
79+
80+
### Anchor-animation.js代码
81+
```javascript
82+
(function($) {
83+
// Smooth scrolling using jQuery easing
84+
$('a[href*="#"]:not([href="#"])').click(function() {
85+
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
86+
var target = $(this.hash);
87+
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
88+
if (target.length) {
89+
$('html, body').animate({
90+
scrollTop: target.offset().top
91+
}, 1000, "easeInOutExpo");
92+
return false;
93+
}
94+
}
95+
});
96+
})(jQuery); // End of use strict
97+
```
98+
99+
### jquery-easing.js代码
100+
此处因为只使用到了easeInOutExpo方法,所以我删除了其他没有用到的方法,只保留了easeInOutExpo方法。
101+
102+
```javascript
103+
jQuery.easing['jswing'] = jQuery.easing['swing'];
104+
105+
jQuery.extend( jQuery.easing,
106+
{
107+
def: 'easeOutQuad',
108+
swing: function (x, t, b, c, d) {
109+
//alert(jQuery.easing.default);
110+
return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
111+
},
112+
113+
easeInOutExpo: function (x, t, b, c, d) {
114+
if (t==0) return b;
115+
if (t==d) return b+c;
116+
if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
117+
return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
118+
}
119+
});
120+
121+
122+
```

0 commit comments

Comments
 (0)