Skip to content

Commit fdbf02e

Browse files
dashnowordssoyaine
authored andcommitted
try to finish the work of day16 (soyaine#20) by dashrun
* try to finish the work of day16 * modify some details to improve the performance of readme.md as asked
1 parent 45617f3 commit fdbf02e

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed

day16 - mouseMoveShadow/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 16 文字阴影的鼠标随动效果
2+
3+
> 本篇作者:©[大史快跑Dashrun](https://github.com/dashrun)——Chinasoft Frontend Web Developer
4+
5+
> 简介:[JavaScript30](https://javascript30.com)[Wes Bos](https://github.com/wesbos) 推出的一个 30 天挑战。项目免费提供了 30 个视频教程、30 个挑战的起始文档和 30 个挑战解决方案源代码。目的是帮助人们用纯 JavaScript 来写东西,不借助框架和库,也不使用编译器和引用。现在你看到的是这系列指南的第 16 篇。完整指南在 [GitHub](https://github.com/soyaine/JavaScript30),喜欢请 Star 哦♪(^∇^*)
6+
7+
> 创建时间:2017-08-20
8+
最后更新:2017-08-21
9+
10+
## 挑战任务
11+
初始文件`index-start.html`中提供了一个包含了`h1`元素的`div`元素,`h1`元素已经设置了`text-Shadow`的样式。本次编程挑战中需要完成的效果是根据用户当前的鼠标位置来操纵文字阴影的位置。
12+
13+
## 实现效果
14+
![结果展示](https://github.com/dashrun/vanilla-javascript-30/blob/master/day16-mouseMoveShadow/effects.png)
15+
16+
## 基本知识
17+
`text-shadow`样式为标准CSS3样式,用于添加**一个或多个**文字阴影,用于其的语法格式为:
18+
```css
19+
text-shadow: h-shadow v-shadow blur color
20+
21+
```
22+
23+
## 过程指南
24+
1.在`script`标签中,我们使用3个变量,一个指向`div`元素,一个指向其子元素`h1`,最后一个变量`factor`用于标记阴影距离`h1`中心的距离和鼠标距离`h1`中心距离的比例,用于计算阴影的具体位置。
25+
26+
2.在`hero`元素上监听鼠标移动事件`mousemove`,并添加事件处理的回调函数`shadowMove`.
27+
```js
28+
hero.addEventListener('mousemove',shadowMove);
29+
```
30+
31+
3.为获得第一个阴影的瞬时位置,需要通过鼠标位置距离`h1`中心的距离乘以`factor`系数来获得,`pos`表示鼠标当前位置的坐标,range指代`hero`元素的宽和高:
32+
```js
33+
var disX = parseInt((pos.x-range.x/2)*factor);
34+
var disY = parseInt((pos.y-range.y/2)*factor);
35+
```
36+
4.从事件发生的event对象中获取需要的值:
37+
```js
38+
var range = {
39+
x:hero.offsetWidth,
40+
y:hero.offsetHeight
41+
}
42+
var pos = {
43+
x:e.target.offsetLeft+e.offsetX,
44+
y:e.target.offsetTop+e.offsetY
45+
}
46+
```
47+
5.计算出`h1`元素第一个阴影位置后,可以以坐标镜像或旋转90°等不同的方式来生成其他阴影,本例中我们采用绕`h1`元素中心旋转90°的方式共生成4个阴影:
48+
```js
49+
text.style.textShadow = `
50+
${xWalk}px ${yWalk}px 0 rgba(255,0,255,0.7),
51+
${xWalk * -1}px ${yWalk}px 0 rgba(0,255,255,0.7),
52+
${yWalk}px ${xWalk * -1}px 0 rgba(0,255,0,0.7),
53+
${yWalk * -1}px ${xWalk}px 0 rgba(0,0,255,0.7)
54+
`;
55+
```

day16 - mouseMoveShadow/effects.png

35.5 KB
Loading
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Mouse Shadow-ES5</title>
6+
</head>
7+
<body>
8+
9+
<div class="hero">
10+
<h1 contenteditable>🔥WOAH!</h1>
11+
</div>
12+
<!--CSS part-->
13+
<style>
14+
html {
15+
color:black;
16+
font-family: sans-serif;
17+
}
18+
19+
body {
20+
margin: 0;
21+
}
22+
23+
.hero {
24+
min-height: 100vh;
25+
display:flex;
26+
justify-content: center;
27+
align-items: center;
28+
color:black;
29+
}
30+
31+
h1 {
32+
text-shadow: 10px 10px 0 rgba(0,0,0,1);
33+
font-size: 100px;
34+
}
35+
</style>
36+
<!--Js part-->
37+
<script>
38+
var hero = document.querySelector('.hero');
39+
var text = hero.querySelector('h1');
40+
var factor = 0.4;//当鼠标移动至显示区域边界时,阴影距离占hero元素宽和高的比例
41+
//在div范围内监听
42+
hero.addEventListener('mousemove',shadowMove);
43+
44+
//文字阴影效果及移动函数
45+
function shadowMove(e){
46+
var range = {
47+
x:hero.offsetWidth,
48+
y:hero.offsetHeight
49+
}
50+
var pos = {
51+
x:e.target.offsetLeft+e.offsetX,
52+
y:e.target.offsetTop+e.offsetY
53+
}
54+
//计算阴影移动距离
55+
var disX = parseInt((pos.x-range.x/2)*factor);
56+
var disY = parseInt((pos.y-range.y/2)*factor);
57+
//修改阴影样式
58+
text.style.textShadow = disX+'px '+disY+'px 0 #3498db,'+(-disX)+'px '+disY+'px 0 #1abc9c,'+disY+'px '+(-disX)+'px 0 #9b59b6,'+(-disY)+'px '+(-disX)+'px 0 #e74c3c';
59+
}
60+
</script>
61+
</body>
62+
</html>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Mouse Shadow</title>
6+
</head>
7+
<body>
8+
9+
<div class="hero">
10+
<h1 contenteditable>🔥WOAH!</h1>
11+
</div>
12+
13+
<style>
14+
html {
15+
color:black;
16+
font-family: sans-serif;
17+
}
18+
19+
body {
20+
margin: 0;
21+
}
22+
23+
.hero {
24+
min-height: 100vh;
25+
display:flex;
26+
justify-content: center;
27+
align-items: center;
28+
color:black;
29+
}
30+
31+
32+
h1 {
33+
text-shadow: 10px 10px 0 rgba(0,0,0,1);
34+
font-size: 100px;
35+
}
36+
</style>
37+
38+
<script>
39+
//高级选择器
40+
const hero = document.querySelector('.hero');
41+
const text = hero.querySelector('h1');
42+
const factor = 0.4; //当鼠标移动至显示区域边界时,阴影距离占hero元素宽和高的比例
43+
44+
function shadowMove(e) {
45+
//解构赋值
46+
const { offsetWidth: width, offsetHeight: height } = hero;
47+
let { offsetX: x, offsetY: y } = e;
48+
49+
//将鼠标位置转换为相对视口左上角的坐标,本例中由于hero元素占满视口故未起实际作用
50+
if (this !== e.target) {
51+
x = x + e.target.offsetLeft;
52+
y = y + e.target.offsetTop;
53+
}
54+
55+
const xWalk = parseInt((x-width/2)*factor);
56+
const yWalk = parseInt((y-height/2)*factor);
57+
58+
//使用模板字符串赋值
59+
text.style.textShadow = `
60+
${xWalk}px ${yWalk}px 0 rgba(255,0,255,0.7),
61+
${xWalk * -1}px ${yWalk}px 0 rgba(0,255,255,0.7),
62+
${yWalk}px ${xWalk * -1}px 0 rgba(0,255,0,0.7),
63+
${yWalk * -1}px ${xWalk}px 0 rgba(0,0,255,0.7)
64+
`;
65+
66+
}
67+
68+
//在hero范围内监听鼠标移动事件
69+
hero.addEventListener('mousemove', shadowMove);
70+
</script>
71+
</body>
72+
</html>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Mouse Shadow</title>
6+
</head>
7+
<body>
8+
9+
<div class="hero">
10+
<h1 contenteditable>🔥WOAH!</h1>
11+
</div>
12+
13+
<style>
14+
html {
15+
color:black;
16+
font-family: sans-serif;
17+
}
18+
19+
body {
20+
margin: 0;
21+
}
22+
23+
.hero {
24+
min-height: 100vh;
25+
display:flex;
26+
justify-content: center;
27+
align-items: center;
28+
color:black;
29+
}
30+
31+
h1 {
32+
text-shadow: 10px 10px 0 rgba(0,0,0,1);
33+
font-size: 100px;
34+
}
35+
</style>
36+
37+
<script>
38+
</script>
39+
</body>
40+
</html>

day16 - mouseMoveShadow/style.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@charset "UTF-8";
2+
/**
3+
* @dashrun (@389399428@qq.com)
4+
* @date 2017-08-20
5+
* @version $1.0
6+
*/

0 commit comments

Comments
 (0)