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
|
<!-- 底部走动的桌宠 -->
<img id="yanglikeu" src="/bc.gif">
<style>
#yanglikeu{
position: fixed;
z-index: 999999;
bottom:0;
left:-100px;
opacity: 0.9;
transform:scaleX(-1);
animation-direction: alternate;
animation-timing-function: linear;
}
</style>
<style id="zcCss"></style>
<script type="text/javascript">
(function () {
var zc,zcCss,w1,w2,leftStart,leftEnd,speed,time,fx,isfirst; //预设变量
document.addEventListener("DOMContentLoaded", function () {// 初始化
zc=document.querySelector("#yanglikeu"),
zcCss=document.querySelector("#zcCss"),
w1=document.body.scrollWidth, //页面宽度
w2=86, //桌宠宽度
leftStart=0-w2, //左边界
leftEnd=w1-w2, //右边界
speed=100, //每秒移动多少像素
time=parseInt(w1/speed), //动画时长
fx="r", //桌宠方向
isfirst=true; //是否是第一次执行
zcmove(fx); //启动
zc.addEventListener("animationend", function () { //动画播放完毕后再启动
isfirst=false;
fx==="r"?fx="l":fx="r";
zcmove(fx);
});
});
function zcmove(fx) { // 要注意两次的动画名要有区别,不然第二次就不正常了
if (!isfirst) { //如果已经不是第一次执行了,则把左边界设置为0(即不走出屏幕外)
leftStart=0;
}
if (fx==="r") { //向右移动
zc.style.transform="scaleX(-1)";
zcCss.innerHTML='@keyframes zcmove{' +
'from {left: '+leftStart+'px}' +
'to {left: '+leftEnd+'px}' +
'}';
zc.style.animationName="zcmove";
zc.style.animationDuration=time+"s";
}else if (fx==="l") { //向左移动
zc.style.transform="scaleX(1)";
zcCss.innerHTML='@keyframes zcmove2{' +
'from {left: '+leftEnd+'px}' +
'to {left: '+leftStart+'px}' +
'}';
zc.style.animationName="zcmove2";
zc.style.animationDuration=time+"s";
}
}
})();
</script>
|