hexo添加小魔方

效果如该网站右下角所示。

该小魔方可以替代back-to-top按钮。

方法:

具体步骤如下

添加实现代码

在/themes/next/layout/ _custom/ 路径下新建cube-hollow.swig文件

添加如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<style>
/*最外层容器样式*/
.wrap {
width: 0px;
height: 0px;
/*margin: 80px;*/
/*position: relative;*/
position: fixed;
/*显示位置*/
bottom: 150px;
right: 100px;
z-index: 999;
}

/*包裹所有容器样式*/
.cube {
width: 0px;
height: 0px;
margin: 0 auto;
-webkit-transform-style: preserve-3d;
transform-style: preserve-3d;
transform: rotateX(-30deg) rotateY(-80deg);
animation: rotate linear 10s infinite;
}

@-webkit-keyframes rotate {
from {
transform: rotateX(0deg) rotateY(0deg);
}
to {
transform: rotateX(360deg) rotateY(360deg);
}
}

.cube div {
position: absolute;
/*显示大小*/
width: 50px;
height: 50px;
opacity: 0.8;
transition: all .4s;
}

/*定义所有图片样式*/
.pic {
width: 50px;
height: 50px;
}

.cube .out_front {
transform: rotateY(0deg) translateZ(25px);
}

.cube .out_back {
transform: translateZ(-25px) rotateY(180deg);
}

.cube .out_left {
transform: rotateY(-90deg) translateZ(25px);
}

.cube .out_right {
transform: rotateY(90deg) translateZ(25px);
}

.cube .out_top {
transform: rotateX(90deg) translateZ(25px);
}

.cube .out_bottom {
transform: rotateX(-90deg) translateZ(25px);
}

/*定义小正方体样式*/
.cube span {
display: block;
width: 25px;
height: 25px;
position: absolute;
top: 12px;
left: 12px;
}

.cube .in_pic {
width: 25px;
height:25px;
}

.cube .in_front {
transform: rotateY(0deg) translateZ(12px);
}

.cube .in_back {
transform: translateZ(-12px) rotateY(180deg);
}

.cube .in_left {
transform: rotateY(-90deg) translateZ(12px);
}

.cube .in_right {
transform: rotateY(90deg) translateZ(12px);
}

.cube .in_top {
transform: rotateX(90deg) translateZ(12px);
}

.cube .in_bottom {
transform: rotateX(-90deg) translateZ(12px);
}

/*鼠标移入后样式*/
.cube:hover .out_front {
transform: rotateY(0deg) translateZ(50px);
}

.cube:hover .out_back {
transform: translateZ(-50px) rotateY(180deg);
}

.cube:hover .out_left {
transform: rotateY(-90deg) translateZ(50px);
}

.cube:hover .out_right {
transform: rotateY(90deg) translateZ(50px);
}

.cube:hover .out_top {
transform: rotateX(90deg) translateZ(50px);
}

.cube:hover .out_bottom {
transform: rotateX(-90deg) translateZ(50px);
}
</style>

<div class="wrap">

<!--包裹所有元素的容器-->
<div class="cube">
<!--前面图片 -->
<div class="out_front">
<a onclick="back2top()">
<img src="https://raw.githubusercontent.com/Aurthoria/Pictures/master/img/20200309224636.png" class="pic" />
</a>
</div>
<!--后面图片 -->
<div class="out_back">
<a onclick="back2top()">
<img src="https://raw.githubusercontent.com/Aurthoria/Pictures/master/img/20200309224636.png" class="pic" />
</a>
</div>
<!--左面图片 -->
<div class="out_left">
<a onclick="back2top()">
<img src="https://raw.githubusercontent.com/Aurthoria/Pictures/master/img/20200309224636.png" class="pic" />
</a>
</div>
<!--右面图片 -->
<div class="out_right">
<a onclick="back2top()">
<img src="https://raw.githubusercontent.com/Aurthoria/Pictures/master/img/20200309224636.png" class="pic" />
</a>
</div>
<!--上面图片 -->
<div class="out_top">
<a onclick="back2top()">
<img src="https://raw.githubusercontent.com/Aurthoria/Pictures/master/img/20200309224636.png" class="pic" />
</a>
</div>
<!--下面图片 -->
<div class="out_bottom">
<a onclick="back2top()">
<img src="https://raw.githubusercontent.com/Aurthoria/Pictures/master/img/20200309224636.png" class="pic" />
</a>
</div>

<!--小正方体 -->
<span class="in_front">
<img src="https://raw.githubusercontent.com/Aurthoria/Pictures/master/img/20200309224636.png" class="in_pic" />
</span>
<span class="in_back">
<img src="https://raw.githubusercontent.com/Aurthoria/Pictures/master/img/20200309224636.png" class="in_pic" />
</span>
<span class="in_left">
<img src="https://raw.githubusercontent.com/Aurthoria/Pictures/master/img/20200309224636.png" class="in_pic" />
</span>
<span class="in_right">
<img src="https://raw.githubusercontent.com/Aurthoria/Pictures/master/img/20200309224636.png" class="in_pic" />
</span>
<span class="in_top">
<img src="https://raw.githubusercontent.com/Aurthoria/Pictures/master/img/20200309224636.png" class="in_pic" />
</span>
<span class="in_bottom">
<img src="https://raw.githubusercontent.com/Aurthoria/Pictures/master/img/20200309224636.png" class="in_pic" />
</span>
</div>
</div>

<script>
function back2top(){
$('html, body').animate({scrollTop: 0}, 500);
}
</script>

其中照片要放自己的呀!

调用

下面就是在个人主页中调用小魔方辽

  • 在 /themes/next/layout/_custom/custom.swig 文件中引入新建的文件

    1
    2
    3
    4
    5
    6
    7
    8
    <!-- 旋转魔方 -->
    {% if theme.cube.enable %}
    {% if theme.cube.type == "mini" %}
    {% include 'cube-mini.swig' %}
    {% elseif theme.cube.type == "hollow" %}
    {% include 'cube-hollow.swig' %}
    {% endif %}
    {% endif %}
  • 如果 custom.swig 文件不存在,需要手动新建并在布局页面中 body 末尾引入:

    1
    2
    3
    4
    5
    6
    7
    8
    themes\next\layout\_layout.swig      ...
    {% include '_third-party/exturl.swig' %}
    {% include '_third-party/bookmark.swig' %}
    {% include '_third-party/copy-code.swig' %}

    + {% include '_custom/custom.swig' %}
    </body>
    </html>
  • 在主题next中的 _config.yml 中新添加以下动态配置项

    1
    2
    3
    cube:
    enable: true
    type: hollow # mini | hollow
  • 隐藏原来的箭头,注释_layout.swig如下部分

    image-20200310004921406

完成! hexo g -d 即可!